Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

can you explain this php behavior

Posted on 2011-09-08
1
Medium Priority
?
227 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

704 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