Confused on this variable declaration in PHP. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Confused on this variable declaration in PHP.

So in a recent challenge for PHP I came across this line: $var = !null == 1 || (5 ==9 % 3); The variable $var equates to 1 after this declaration. I had thought the answer would equate to a boolean, seeing as how the it's checking to see whether two different conditions are true. I'm clearly incorrect, but I still don't know why.

31st Jul 2017, 12:18 AM
Christian Barraza
Christian Barraza - avatar
1 Answer
+ 2
Never mind. I finally figured it out. http://php.net/manual/en/language.types.boolean.php PHP automatically converts any boolean to a value of '1' or ''; For example: $var = 2 == 2; echo $var; // This would print '1' $var = 2 == 3; echo $var; // This would any empty string. This is not the case in something like python, where the first example would print 'True' and the second example would print 'False'. Now I know.
31st Jul 2017, 12:56 AM
Christian Barraza
Christian Barraza - avatar