Sunday, December 23, 2018

php - Convert String 'true' or 'false' to interger '1' or '0'




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' ?



I know this question has been asked. but I have not found the answer


Answer



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);


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...