string 'w32' == 0 evaluates to true in php. huh?

So I was getting a notice in my php while creating a google product feed. The notice was "The following php notice has occurred 4989 times on the _ site today: PHP Notice: Undefined index: 0 in /xxx/Status.php on line 583"

This was the code in that class

public function inStockLocally($productcode)
{
    if($this->_status[$productcode]['status'] == self::IN_STOCK) {
        return $this->_status[$productcode]['in_stock_local'];
    }
    return false;
}

The function was getting a $productcode = 0, but the productcode was infact 'w32', so the key didn't exist.

up the stack where the function was being called I put this in, in order to break on the troublesome product.

    if ($productcode == 0) {
        $test = 'breakhere';
    }

Using netbeans and firebug, it broke on the line when $productcode = 'w32'

So my question is why does 'w32' == 0 evaluate to true? It is also evaluating to true with other similar structure codes like 'h94'.

Any help would be appreciated as no one in the department can figure out why this is happening.

I guess I didn't put enough info in the q. Two things going on. 1. 'w32' converted to a number = 0 for some reason. 2. [0] is being inserted as my key in the array when the productcode has the structure 'x##';


I'm a little new here, so pardon if this isn't the answer you were expecting, but PHP does a lot of automatic type conversion. So any string that doesn't start with a numeric character (0..9, +, -, etc) will evaluate to zero.


"If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. "

http://php.net/manual/en/language.operators.comparison.php

Additionally, I suppose you have an indexed array, although you expect it to be an associative array:

The array() function is used to create an array.

In PHP, there are three types of arrays:

Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays Syntax Syntax for indexed arrays:

array(value1,value2,value3,etc.);

Syntax for associative arrays:

array(key=>value,key=>value,key=>value,etc.);

链接地址: http://www.djcxy.com/p/58518.html

上一篇: 我如何使用PHP获取当前年度?

下一篇: 字符串'w32'== 0在php中评估为true。 是吧?