8

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

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Sate Wedos
  • 539
  • 8
  • 20

7 Answers7

13

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

Why not use unary operator

int $my_int = $myboolean=='true' ? 1 : 0;
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • thanks for the answer. I think all the right answers are just different ways. and sorry I can only determine a correct answer that is the way that I think is most efficient. I appreciate all the answers – Sate Wedos Oct 23 '17 at 01:58
1

the string "false" is truthy. That's why (int) (boolean) "false" return 1 instead of 0. If you want to get a false boolean value from a string, you can pass an empty string (int) (boolean) "". More info here.

Andy Aldo
  • 890
  • 1
  • 9
  • 24
0

PHP

$int = 5 - strlen($myboolean);

// 5 - strlen('true')  => 1
// 5 - strlen('false') => 0

JAVASCRIPT

// ---- fonction ----

function b2i(b){return 5 - b.length}



//------------ debug -----------

for(b of ['true','True','TRUE','false','False','FALSE'])
{
  console.log("b2i('"+b+"') = ", b2i(b));
}
0
$variable = true;

if ($variable) {
     $convert = 1;
}
else {
    $convert = 0;
}

echo $convert
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
levi
  • 1,566
  • 3
  • 21
  • 37
  • thanks for the answer. I think all the right answers are just different ways. and sorry I can only determine a correct answer that is the way that I think is most efficient. I appreciate all the answers – Sate Wedos Oct 23 '17 at 01:58
  • The question, I believe, was how to convert a string to a 1 or 0, not a boolean value. – RufusVS Feb 20 '19 at 21:00
  • cast a string to a int. `$a = "1";` to cast do `$b = (int)$a` then do your if statement on the variable `$b` – levi Mar 04 '19 at 03:31
0

Try this:

echo (int) (boolean) '';

Ashish Tiwari
  • 1,919
  • 1
  • 14
  • 26
  • thanks for the answer. I think all the right answers are just different ways. and sorry I can only determine a correct answer that is the way that I think is most efficient. I appreciate all the answers – Sate Wedos Oct 23 '17 at 01:58
-1

Normally just $Int = (int)$Boolean would work fine, or you could just add a + in front of your variable, like this:

$Boolean = true; 

var_dump(+$Boolean);

ouputs: int(1);

also, (int)(bool)'true' should work too

TheRealOrange
  • 115
  • 1
  • 1
  • 9