Solved

can you explain this php behavior

Posted on 2011-09-08
1
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I allow for non standard CSV file 9 47
Insert PHP into HTML page. 7 52
sitemap.xml 3 32
How would I break down this array? 3 20
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to dynamically set the form action using jQuery.
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).

751 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