Solved

can you explain this php behavior

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

808 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