未定义的变量错误php

why am I getting an undefined variable error? I am defining it inside the IF statement and the condition is always, since there are ni performance issue, any suggestions on how to fix this?

    if(in_array($row['billing'],$list)){
    $bills = array_search($row['billing'], $list);
    }

    echo $bills; // <-- undefined variable $bills on this line

It really seems like that value ($row['billing']) is not in the array ($list), so in_array() returns false. So $bills is never defined, because the code inside the if never runs.

To be sure, define it beforehands with a default value of your choice (null, '', etc.):

$bills = 'no billing information';
if(in_array($row['billing'],$list)){
    $bills = array_search($row['billing'], $list);
}

On the other hand, you don't really need that check. If the value is not in the array, array_search() will return false anyways:

Returns the key for needle if it is found in the array, FALSE otherwise.

So you can simplify things:

$bills = array_search($row['billing'], $list);
echo $bills === false ? 'no billing information' : $bills;

You have to use === false because the function might return 0 as well (if the element is at the first index).

The $bills variable exists only in the scope of the if statement, i.e. between the open and closing braces.

If you want to use the value outside the if statement, define $bills earlier. For example:

   $bills = '';
   if(in_array($row['billing'],$list))
   {
        $bills = array_search($row['billing'], $list);
   }

    echo $bills;

Or, you could just echo out the value in the if statement scope.

EDIT: I had it wrong, PHP does not have block scope. Use the following:

 if(in_array($row['billing'],$list))
    {
        $bills = array_search($row['billing'], $list);
        echo $bills;
   }
if(isset($bills))
 echo $bills

The if statement if(in_array($row['billing'],$list)) is not holding true. Hence, the $bills variable is not being set/defined. You either need to define it before the if statement or make sure the if statement passes, before the echo.

Alternatively, you can also check if its set before echo with,

if(isset($bills))
  echo $bills;
if(isset($bills)){echo $bills;}