p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Warning message: Undefined offset: 5, etc


Message #1 by "Laurie Tsakiris" <laurietsakiris@h...> on Fri, 17 May 2002 10:37:41 -0700
Ok so Ive got this script that generates buttons:
  for($i = 0; $i < 3; $i++ )
  {
    if($button[($i*5)])
    {
      echo '<tr>';
      echo '<td >';
      for($j = 0; $j < 5; $j++)
      {
        echo '<td >';
        if ($button[($i*5+$j)])
          display_button($button[$i*5+$j], $extra_parameters);
        else
          display_spacer();
        echo '</td>';
      }
      echo '</tr>';
    }
  }
  echo '</table>';
}
and on the pages I get the buttons Im spose to get, but I also get a variety
of errors like the one in the subject line in spaces on the page where it
looks like it wants to put more but cant. Im not really sure wtf is going on
here, or even how to correct it. Does anyone have any idea ???

Thanks in advance.....

Laurie
Message #2 by Peter Fusco <beowulf_ct@y...> on Fri, 17 May 2002 10:53:01 -0700 (PDT)
did you try using double quotes?

Wahzoo
 
--- Laurie Tsakiris <laurietsakiris@h...>
wrote:
> Ok so Ive got this script that generates buttons:
>   for($i = 0; $i < 3; $i++ )
>   {
>     if($button[($i*5)])
>     {
>       echo '<tr>';
>       echo '<td >';
>       for($j = 0; $j < 5; $j++)
>       {
>         echo '<td >';
>         if ($button[($i*5+$j)])
>           display_button($button[$i*5+$j],
> $extra_parameters);
>         else
>           display_spacer();
>         echo '</td>';
>       }
>       echo '</tr>';
>     }
>   }
>   echo '</table>';
> }
> and on the pages I get the buttons Im spose to get,
> but I also get a variety
> of errors like the one in the subject line in spaces
> on the page where it
> looks like it wants to put more but cant. Im not
> really sure wtf is going on
> here, or even how to correct it. Does anyone have
> any idea ???
> 
> Thanks in advance.....
> 
> Laurie
> 
> 
> 
> 


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
Message #3 by "Nikolai Devereaux" <yomama@u...> on Fri, 17 May 2002 10:54:02 -0700
Well, the warning message is being displayed because you're testing the
value of an array index as a boolean.  If a value doesn't exist at the index
you're testing, you're actually letting PHP create a value for that index
with a null (or false) value for that is then used to evaluate the
contidional expression within the if() parens.

That's all well and good, but if you are going to develop code that executes
cleanly under error_reporting = E_ALL, then you need to be a lot more
careful with how you use variables and arrays.

For instance, use isset() inside the if() block.
  http://www.php.net/isset

if( isset($button[$i * 5]) ) will NOT generate the warning that you're
accessing an index that doesn't exist in an array.

Another possibility would be to use array_key_exists()
  http://www.php.net/array_key_exists

if( array_key_exists($i * 5, $button) )


Hope this helps,

nik

Message #4 by "Juliet May" <jmay@s...> on Fri, 17 May 2002 13:22:40 -0600
Which is preferable

> For instance, use isset() inside the if() block.
>   http://www.php.net/isset
>
> if( isset($button[$i * 5]) ) will NOT generate the warning that you're
> accessing an index that doesn't exist in an array.
>
> Another possibility would be to use array_key_exists()
>   http://www.php.net/array_key_exists
>
 I've mostly been using if(isset()) to check the existence of variables
within my array.

How much do the warnings slow down the processing? And how critical is it to
change code to remove the possibility of encountering warnings?

Julie

Message #5 by "Laurie Tsakiris" <laurietsakiris@h...> on Fri, 17 May 2002 12:43:55 -0700
Thanks very much guys. :o)

Laurie

----- Original Message -----
From: "Juliet May" <jmay@s...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, May 17, 2002 12:22 PM
Subject: [beginning_php] RE: Warning message: Undefined offset: 5, etc


> Which is preferable
>
> > For instance, use isset() inside the if() block.
> >   http://www.php.net/isset
> >
> > if( isset($button[$i * 5]) ) will NOT generate the warning that you're
> > accessing an index that doesn't exist in an array.
> >
> > Another possibility would be to use array_key_exists()
> >   http://www.php.net/array_key_exists
> >
>  I've mostly been using if(isset()) to check the existence of variables
> within my array.
>
> How much do the warnings slow down the processing? And how critical is it
to
> change code to remove the possibility of encountering warnings?
>
> Julie
>
>
>
Message #6 by "Nikolai Devereaux" <yomama@u...> on Fri, 17 May 2002 12:41:34 -0700
> Which is preferable
 <snip />
>  I've mostly been using if(isset()) to check the existence of
> variables within my array.

It's neither here nor there, really.  I havn't run any performance tests on
those two functions so I can't tell you which is faster, but I'd guess that
they're about the same -- with a difference so small that it doesn't make
much difference.

> How much do the warnings slow down the processing? And how
> critical is it to
> change code to remove the possibility of encountering warnings?

Well, warnings themselves don't slow the processing as much as having PHP
look for and display warnings when it finds them.  The benefit of writing
code under E_ALL error reporting is that it somewhat forces you to write
cleaner code and not accidentally introduce some bug that's hard to track.

For instance, if you have this code:

$logindata = $_GET['login_form'];
 ...
if($login_data['password'] == get_password($login_data['user_name']))
{
   ...
}

(notice that logindata shouldn't have an underscore in the name)

PHP will generate a warning or two that $login_data is an uninitialized
variable and that 'password' isn't a valid index.

These warnings help you find typos and bugs like this early on, and
preventing you from combing through all your code when you test your site
and can't figure out why all your username/password combos are being
rejected.

A great approach is to develop and deploy your scripts on different
settings.  Run your site on a server that reports ALL error messages.
Deploy your site on a server that reports NONE of them (or at least
suppresses warnings) because it makes your site look less professional when
something bad happens and a generic error message is displayed to your
users.


nik

Message #7 by "Nikolai Devereaux" <yomama@u...> on Fri, 17 May 2002 12:51:31 -0700
> Thanks very much guys. :o)

parakalo

nikos

  Return to Index