Solved

can you explain this php behavior

Posted on 2011-09-08
1
201 Views
Last Modified: 2012-05-12
$a = 'SOME TEXT HERE'; if ($a == 0) echo "$a is zero"; else echo "$a is NOT zero"; if (!$a) echo ' AND not false.'; else echo ' BUT not false?';

Open in new window

0
Comment
Question by:rationalboss
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36506557
>>if ($a == 0)
The $a is casted to numerical context while that if condition is evaluated, so it evaluates to true.

>>if (!$a)
in this case, since $a is not an empty string it evaluates to true. The "!" inverts the logic, so the else clause is printed.


Refer to:
http://www.php.net/manual/en/language.operators.comparison.php

Specifically the "Comparison with Various Types" will help you understand the first "if" clause better.

Regards,
Hielo
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question