Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize bool with a logical expression?

Tags:

php

$variable = (0 > 0)

For some reason, when I try to print $variable, it doesn't print anything. No null, nothing. But

$variable = (1 > 0)

sets $variable to true. Why is that, and what is the best way to do what I'm trying?

like image 514
Tyler Durden Avatar asked Apr 21 '26 21:04

Tyler Durden


1 Answers

$variable is being set correctly -- you can see this with var_dump($variable).

The issue is that in PHP trying to straight print false and null values prints the empty string, so nothing appears to happen. var_dump is one of the ways to check what's really going on; when dealing with booleans, a more convenient way is to cast to int first:

echo (int)$variable; // prints "0" or "1"
like image 186
Jon Avatar answered Apr 23 '26 10:04

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!