Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:可以将字符串“;0“;是否显式转换为字符串?_Php_String_Casting_Boolean - Fatal编程技术网

PHP:可以将字符串“;0“;是否显式转换为字符串?

PHP:可以将字符串“;0“;是否显式转换为字符串?,php,string,casting,boolean,Php,String,Casting,Boolean,我刚刚遇到一个应用程序问题,其中查询返回字符串,但有时返回值“0”作为合法字符串。这打破了我应用程序中的逻辑,我找不到修复程序。我在手册中找到了它发生的原因: 我使用的是PHP5.5.10,通常所有的字符串值都会转换为bool(true)。。。。我在手册中发现,可以将变量显式转换为字符串。我加入了一些逻辑来做这件事。。但它也失败了 <?php $str1 = "0"; //Below explicitly casts the string but it will still

我刚刚遇到一个应用程序问题,其中查询返回字符串,但有时返回值“0”作为合法字符串。这打破了我应用程序中的逻辑,我找不到修复程序。我在手册中找到了它发生的原因:

我使用的是PHP5.5.10,通常所有的字符串值都会转换为bool(true)。。。。我在手册中发现,可以将变量显式转换为字符串。我加入了一些逻辑来做这件事。。但它也失败了

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

上面的测试将告诉您,即使显式转换也不会在布尔值中保持该字符串“TRUE”。下面是我的问题所在

<?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!
        }
?>

我是否可以强制PHP以布尔方式将字符串“0”或“0”转换为TRUE


我完全理解整数值0是错误的,但是字符串的事情把我抛出了一条曲线…

尝试
如果(strlen($str))
-在这种情况下,
“0”
通过了测试,因为它的长度是1。

如果您只想检查变量的类型(包括空值)​​), 您可以使用函数is_string()

答案是否定的

即使字符串显式转换为字符串,如下所示:

$str = (string) 0;
$str2 = (string) '0';
// Alternate Method
settype($str, "string");
由于PHP在处理所有变量时都是松散类型的,所以PHP总是松散地撤消异常变量(例如“0”)的任何显式类型转换


感谢您的回复

我理解规则的可能重复,我的问题是关于显式强制转换。我搜索了一下,但还没有找到。:
$zero=“0”;if($zero){/*error*/};if($zero==“0”){/*right*/};
我认为这是一个例子。您实际要完成的任务是什么?我的问题仍然是:我是否可以显式强制PHP5.5.10查看字符串值“0”或“0”当在逻辑运算符内使用时为true?这些值转换为字符串,但当任何其他字符串返回true时,逻辑运算符内的值返回false。这很有效!-我的解决方法基本上是使用or语句if($str或($str==“0”){..解决方法也可以。这是我找到的最佳答案。我的程序逻辑只需要知道字符串是否已初始化且未设置为NULL。谢谢,这是我看到的最佳答案。
$str = (string) 0;
$str2 = (string) '0';
// Alternate Method
settype($str, "string");