76

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 !

user2990361
  • 817
  • 1
  • 6
  • 9

7 Answers7

106

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

$myInt = (int)$myBoolean should work

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Jérémy Dutheil
  • 6,099
  • 7
  • 37
  • 52
30

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

$myBool = true; 

var_dump(+$myBool);

ouputs: int(1);
Dawa
  • 896
  • 10
  • 7
19
// 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
Tyrmos
  • 387
  • 4
  • 13
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • 26
    While this does get the point across...it wouldn't hurt to add a sentence or two to explain. – Pete Mar 26 '15 at 18:56
  • 2
    but `echo false;` doesn't return `0` – Flash Thunder Nov 03 '21 at 14:11
  • @FlashThunder it does not – Samuel Cook Nov 05 '21 at 12:54
  • in case you're thoroughly confused like I was - check your data types! see some of the other comments - ENV vars can get processed as strings, which results in eg `echo (int)"true"; // 0` & `echo (int)(bool)'false'; // 1` - fix this by using eg `0` or `1` instead of true/false – Sandra Sep 27 '22 at 16:10
8

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

Community
  • 1
  • 1
5

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
Sam Boyne
  • 71
  • 1
  • 6
  • Similar way to do it: `$myInt = ($requestBool === 'true') ? 1 : 0;` – Ricardo Gonçalves Jun 14 '16 at 13:57
  • Your answer is very simplicated, first, to receive JSON one should parse POST BODY (eg here https://stackoverflow.com/a/8945912/340142). Second JSON by itself can hold values `true`, `false` and `null`. Just to see yourself run this in browser's console `JSON.stringify({one:true, two:false,three:null})`. – Marecky Feb 26 '18 at 22:19
1

You can do multiple castings in a single go:

$isTest = 'true';

(int)(bool)$isTest

echo $isTest; // this outputs 1
David
  • 2,298
  • 6
  • 22
  • 56
-3

Use type casting (bool):

var_dump((bool) 1);         // bool(true)
var_dump((bool) 0);         // bool(false)
Jokerius
  • 1,310
  • 1
  • 14
  • 22