开发者

php considers numeric value as empty string (weird behavior)

开发者 https://www.devze.com 2023-04-12 23:32 出处:网络
This is my own code that has confused for more than 2 hours. $int_length_1 = $data[0][\'length_1\'];// length_1 is mysql integer field

This is my own code that has confused for more than 2 hours.

$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' =>开发者_StackOverflow社区; $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>';
        }       
    }
}

expected result:

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

but somehow, the code only prints the first 2 lines.

is there something wrong with the code? does this have anything to do with data type comparison?

thanks in advance for any suggestion.


Try type casting and see if the results are the same

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


This is a really old question but I just happened to come across it today and decided to see what's up with it.

Code I was running:

<?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 you change this line:

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

to do a !== instead of just !=, the code works as expected and returns all three lines of expected output.

This is how 0 and an empty string relate:

$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";

And the output:

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


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

So.. basically... OP should have used a === to make sure everything was working as he expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消