Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert boolean to integer value php

Is there any builtin function for PHP that will take a boolean value and return its integer equivalent? 0 for FALSE, 1 for TRUE? Of course you can easily create a function to do so, I'm just asking if there is a builtin function inside of PHP. I already tried intval() and casting it to (int) but they didnt work, they return 0 in both cases of TRUE and FALSE.

Edit : the problem was that it wasnt really a boolean, it was a string "false", "true", php didnt detect the jquery post that it passed a boolean. problem solved, thanks !

like image 405
user2990361 Avatar asked Nov 19 '13 14:11

user2990361


People also ask

Is bool true 1 or 0?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

Is 0 True or false PHP?

0 is the integer value of zero, and false is the boolean value of, well, false.

How can check boolean value in if condition in PHP?

The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

Is 1 true in PHP?

The boolean values are called true and false in php. In the case of true , the output is 1 . While with the false , it does not show any output. It is worth noting that the browser always renders these values in strings.


2 Answers

Just add a "+" before your variable like this :

$myBool = true; 

var_dump(+$myBool);

ouputs: int(1);
like image 28
Dawa Avatar answered Oct 19 '22 17:10

Dawa


http://www.php.net/manual/en/language.types.integer.php

$myInt = (int)$myBoolean should work

like image 170
Jérémy Dutheil Avatar answered Oct 19 '22 17:10

Jérémy Dutheil