Convert boolean to integer value php

PhpIntegerBooleanType Conversion

Php Problem Overview


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 !

Php Solutions


Solution 1 - Php

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

$myInt = (int)$myBoolean should work

Solution 2 - Php

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

$myBool = true; 

var_dump(+$myBool);

ouputs: int(1);

Solution 3 - Php

// cast to Integer
echo (int)true;         // 1
echo (int)false;        // 0

// get the integer value from argument
echo intval(true);      // 1
echo intval(false);     // 0

// simply echoing true returns 1
echo true;              // 1

// you can even add those values
echo true + true;       // 2

Solution 4 - Php

If you are unsure of the data type see the example below, it works on strings, integers and booleans.

<?php

$options = [ TRUE, FALSE, 'true', 'false', 1, 0, '1', '0', 'on', 'off', 'yes', 'no' ];

foreach ( $options as $option ) {
    $bool = filter_var( $option, FILTER_VALIDATE_BOOLEAN ); // TRUE or FALSE
    print (int) $bool . ' '; // 1 or 0
} 
// Outputs: 1 0 1 0 1 0 1 0 1 0 1 0 

?>

filter_var( $var, FILTER_VALIDATE_BOOLEAN ) will return bool(true) or bool(false) then simply cast as integer.

filter_var()

(PHP 5 >= 5.2.0, PHP 7)

filter_var — Filters a variable with a specified filter

Solution 5 - Php

If you are getting your value in from JSON I.E. Via an AJAX POST, the value will come in as a string (As you have found). A way around this is to compare the true/false string and then cast the bool to an int

$requestBool = $_REQUEST['bool_value']; //This is 'true' or 'false'

$myInt = (int)($requestBool === 'true');
echo $myInt;

//returns: 0 or 1

Solution 6 - Php

You can do multiple castings in a single go:

$isTest = 'true';

(int)(bool)$isTest

echo $isTest; // this outputs 1

Solution 7 - Php

Use type casting (bool):

var_dump((bool) 1);         // bool(true)
var_dump((bool) 0);         // bool(false)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2990361View Question on Stackoverflow
Solution 1 - PhpJérémy DutheilView Answer on Stackoverflow
Solution 2 - PhpDawaView Answer on Stackoverflow
Solution 3 - PhpSamuel CookView Answer on Stackoverflow
Solution 4 - PhpNathan DuckettView Answer on Stackoverflow
Solution 5 - PhpSam BoyneView Answer on Stackoverflow
Solution 6 - Phpuser1976610View Answer on Stackoverflow
Solution 7 - PhpJokeriusView Answer on Stackoverflow