Solved

can you explain this php behavior

Posted on 2011-09-08
1
182 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now