php认为数值为空字符串(奇怪的行为)

时间:2011-10-12 10:01:29

标签: php variables

这是我自己的代码,混淆了2个多小时。

$int_length_1 = $data[0]['length_1'];   // length_1 is mysql integer field

print_result(array('difference' => $data[0]['length_1'], 'object' => 'line'));

$int_length_2 = $data[0]['length_2'];   // length_2 is mysql integer field

print_result(array('difference' => $int_length_2, 'object' => 'line'));

print_result(array('difference' => ($data[0]['length_1'] - $data[0]['length_2']), 'object' => 'line'));

function print_result($data) {
    if (is_array($data)) {
        if (isset($data['difference']) && $data['difference'] != '') {
            echo 'Current length of the '.$data['object'].' is '.$data['difference'].'<br>';
        }       
    }
}

预期结果:

Current length of the line is 10
Current length of the line is 7
Current length of the line is 3

但不知何故,代码只打印前两行。

代码有问题吗?这与数据类型比较有什么关系吗?

提前感谢任何建议。

2 个答案:

答案 0 :(得分:0)

尝试输入类型并查看结果是否相同

$int_length_1 = (int) $data[0]['length_1'];

答案 1 :(得分:0)

这是一个非常古老的问题,但我今天碰巧碰到了它,并决定看看它是什么。

我正在运行的代码:

<?php

$data = array(array('length_1'=>10, 'length_2'=>10)); 

/*  OUTPUT for array(array('length_1'=>10, 'length_2'=>7));   

      Current length of the line is 10
      Current length of the line is 7
      Current length of the line is 3 
*/

/*  OUTPUT for array(array('length_1'=>10, 'length_2'=>10));   

      Current length of the line is 10
      Current length of the line is 10
      difference == ''
*/

$int_length_1 = $data[0]['length_1'];   // length_1 is mysql integer field

print_result(array('difference' => $data[0]['length_1'], 'object' => 'line'));

$int_length_2 = $data[0]['length_2'];   // length_2 is mysql integer field

print_result(array('difference' => $int_length_2, 'object' => 'line')); 

// this is where weird things start to happen 
// if ($data[0]['length_1'] - $data[0]['length_2']) === 0 
print_result(array('difference' => ($data[0]['length_1'] - $data[0]['length_2']), 'object' => 'line'));

function print_result($data) 
{
    if (is_array($data)) 
    {
        //  I added some conditional statements to see why OP 
        //   wasn't getting that third output. 
        if (isset($data['difference']) && $data['difference'] != '') {
            echo 'Current length of the '.$data['object'].' is '.$data['difference'].'<br>';
        }   
        else if (!isset($data['difference'])) {
            print "difference is not set"; 
        }
        else if ( $data['difference'] == '') {
            print "difference == ''"; 
        }
    }
}

?>

如果你改变这一行:

if (isset($data['difference']) && $data['difference'] != '') {

执行!==而非!=,代码按预期工作并返回预期输出的所有三行。

这是0和空字符串相关的方式:

$zero = 0;

if (empty($zero)) print "empty(0) \n"; else print "!empty(0) \n";
if ($zero=='')    print "0=='' \n";    else print "0!='' \n";
if ($zero==='')   print "0==='' \n";   else print "0!=='' \n";
if ($zero==0)     print "0==0 \n";     else print "0!=0 \n";
if ($zero===0)    print "0===0 \n";    else print "0!==0 \n";
if (strlen($zero)==0) print "strlen(0)==0 \n"; else print "strlen(0)!=0 \n";

print "\n\n"; 
$string = '';

if (empty($string)) print "empty('') \n"; else print "!empty('') \n";
if ($string=='')    print "''=='' \n";    else print "''!='' \n";
if ($string==='')   print "''==='' \n";   else print "''!=='' \n";
if ($string==0)     print "''==0 \n";     else print "''!=0 \n";
if ($string===0)    print "''===0 \n";    else print "''!==0 \n";
if (strlen($string)==0) print "strlen('')==0 \n"; else print "strlen('')!=0 \n";

输出:

empty(0) 
0=='' 
0!=='' 
0==0 
0===0 
strlen(0)!=0 


empty('') 
''=='' 
''==='' 
''==0 
''!==0 
strlen('')==0        

所以......基本上...... OP应该使用===来确保一切正常。