-1

I am trying to study php and I came across a if statement which has a single variable as a conditional like this:

<?php if($a){
//do something
}
?>;

I know some C++ and javascript, but in those languages, this if statement would ben invalid. Could anyone tell me what this if statement means?

AmyShmil
  • 84
  • 7
  • 1
    if `$a` has a trutly value in it the if statement is true ([**RTM**](http://php.net/manual/en/control-structures.if.php)) – Rizier123 Apr 09 '15 at 23:47
  • Those other languages have similar mechanisms for evaluating a single variable as truthy or falsey, that is for variables which are not booleans. Each language has its own specific rules on what evaluates to true or false. – John McMahon Apr 09 '15 at 23:52

1 Answers1

0

As Rizier123 said above, the conditional is checking for "Truthy" values in the variable $a. In a loosely-typed language like PHP, a truthy value is anything that is a non-empty, non-NULL, or non-zero value.

See this great answer to a similar question:
https://stackoverflow.com/a/6693908/2796449
or the PHP docs:
http://php.net/manual/en/language.types.boolean.php

Boolean logic always reduces down the entire statement down to one value, either TRUE or FALSE. So executing something like "echo (3==4);" will show FALSE. Similarly if your variable returns a truthy value, it will be evaluated as TRUE.

These 3 are all comparable statements:

$a = 1;
if ( $a )

if ( 1 )

if ( TRUE )

Community
  • 1
  • 1
jjroman
  • 30
  • 3