1

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##';

Mr. Berzerk
  • 33
  • 1
  • 7
  • Related: http://stackoverflow.com/questions/15813490/php-type-juggling-and-strict-greater-lesser-than-comparisons/15820372#15820372 – CSᵠ Sep 28 '13 at 11:55

2 Answers2

0

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.

sosaisapunk
  • 185
  • 6
  • Ok, thanks makes sense, but why would it be injecting 0 as the key for $this->_status[0]['status'] There is no comparison going on in that case. Note that my php notice is saying that index 0 is undefined when it should be saying index 'w32' is undefined. (Actually is is defined, but that is just for arguments sake.) – Mr. Berzerk Sep 27 '13 at 22:11
  • The rule can't always hold true, becuase the productcode 'cat2011' never = 0. – Mr. Berzerk Sep 27 '13 at 22:25
-1

"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.);

Jouni Aro
  • 2,099
  • 14
  • 30
  • [Please, don't link to or use w3schools. It's a horrible reference full of incorrect information that sometimes even leads to security holes.](http://w3fools.com) For example, they don't know bobby tables... – ThiefMaster Oct 13 '13 at 13:30
  • OK, thanks for the tip. I was not aware of that. However, I find in this case they explain the difference between the array types in a concise way, which I do not find anywhere else. The manual page http://php.net/manual/en/language.types.array.php, for example fails to define this difference in any useful manner. – Jouni Aro Oct 13 '13 at 19:32
  • The problem is that they do have a few pages that might be good. But a PHP beginner going there will eventually end up on `http://www.w3schools.com/php/php_mysql_insert.asp` - and write code with gaping sql injection holes – ThiefMaster Oct 13 '13 at 22:06
  • OK, I removed the link and copied the relevant text here. Still, I consider this is the correct answer to the original problem. – Jouni Aro Oct 14 '13 at 07:49