I want to convert String variable 'true' or 'false' to int '1' or '0'.
To achieve this I'm trying like this
(int) (boolean) 'true' //gives 1
(int) (boolean) 'false' //gives 1 but i need 0 here
I now I can using array like array('false','true');
or using if($myboolean=='true'){$int=1;}
But this way is less efficient.
Is there another more efficient way like this (int) (boolean) 'true'
?
Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP.
Depending on your needs, you should consider using filter_var() with the FILTER_VALIDATE_BOOLEAN flag.
(int)filter_var('true', FILTER_VALIDATE_BOOLEAN);
(int)filter_var('false', FILTER_VALIDATE_BOOLEAN);
Why not use unary operator
int $my_int = $myboolean=='true' ? 1 : 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With