7

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
  • 24,837
  • 11
  • 67
  • 96
sate wedos
  • 489
  • 8
  • 19

7 Answers7

10

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
  • 29,945
  • 41
  • 114
  • 168
  • 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
  • 880
  • 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,446
  • 8
  • 30
  • 57
kray
  • 1,336
  • 2
  • 16
  • 34
  • 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` – kray Mar 04 '19 at 03:31
0

Try this:

echo (int) (boolean) '';

Ashish Tiwari
  • 1,499
  • 1
  • 12
  • 23
  • 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
  • 9