0

I just ran into an application problem where a query returns a string, but sometimes returns the value "0" as a legitimate string. This broke the logic in my application, and I can't find a fix. I found in the manual why it happens: http://www.php.net/manual/en/language.types.boolean.php

I'm using PHP 5.5.10 and normally all string values will cast bool(true) .... I found in the manual where you can explicitly cast a variable as a string. I put in some logic to do this.. but it also failed.

<?php
    $str1 = "0";
    //Below explicitly casts the string but it will still bool(false)
    $str2 = (string) "0";
    var_dump($str1);
    var_dump((bool) $str1);
    var_dump($str2);
    var_dump((bool) $str2);
?>

The above test will show you that even explicit casting will not keep this string 'TRUE' in a boolean since. Below is where my problem is...

<?php
    if($str)
        {
        //String is initialized, CODE to rip string is here
        } else {
        //Code for variable not initialized: NOTE: "0" or '0' will execute here!
        }
?>

Can I somehow force PHP to see the string '0' or "0" to cast TRUE in a Boolean fashion?

I completely understand that an integer value 0 is false but the string thing threw me for a curve...

Blue Ice
  • 7,888
  • 6
  • 32
  • 52
Zoul007
  • 593
  • 1
  • 4
  • 6
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2382490/how-does-true-false-work-in-php – RiggsFolly Mar 18 '14 at 21:59
  • I understand the rules, my question was about explicit casting. I did a search for that but hadn't found it yet. – Zoul007 Mar 18 '14 at 22:01
  • [Strict comparison](http://stackoverflow.com/a/589558/1064767): `$zero = "0"; if($zero) { /* wrong */ }; if($zero === "0") { /* right */ };` I think this is a case of the [XY problem](http://meta.stackexchange.com/a/66378/220891). What is the task that you are *actually* trying to accomplish? – Sammitch Mar 18 '14 at 22:07
  • My Question is Still: Can I explicitly force PHP 5.5.10 to see the string value '0' or "0" as true when used inside a logic operator? These values cast as strings but inside logic operators return false when any other string will return true. – Zoul007 Mar 18 '14 at 22:13

3 Answers3

1

If you want to check only the type of the variable (including empty values​​), you can use the function is_string()

$a = '0';
var_dump(is_string($a)); //return TRUE


$b = "0";
var_dump(is_string($b)); //return TRUE

$c = 0;
var_dump(is_string($c)); //return FALSE

$d = null;
var_dump(is_string($d)); //return FALSE
pr0head
  • 71
  • 4
  • This is the best answer I've found. My program logic only needs to know if the string is initialized and is not set to NULL. Thanks this is the best answer I've seen. – Zoul007 Mar 18 '14 at 22:32
0

Try if( strlen($str)) - in this case, "0" passes the test because its length is one.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • This works! - My workaround had been basically an or statement if($str OR ($str == "0")) {} .... The workaround works as well. – Zoul007 Mar 18 '14 at 22:07
0

The answer is no.

Even if the string is explicitly cast as a string like the following:

$str = (string) 0;
$str2 = (string) '0';
// Alternate Method
settype($str, "string");

Since PHP is loosely typed when it comes to all varaibles, PHP will always loosely undo any explicit type conversion of the exception variables like '0' for example.

Thanks for the responses

Zoul007
  • 593
  • 1
  • 4
  • 6