You are here: Re: E_ALL: notice with list() « PHP Language « IT news, forums, messages
Re: E_ALL: notice with list()

Posted by Thomas Mlynarczyk on 10/14/07 13:21

Also sprach Marc Bauer:

> what does this "@" do?

It suppresses error messages. But it's usually a bad idea to use it. Better
perform the necessary checks first to make sure whatever you do will not
cause an error.

$a = array();
$b = $a['foo']; // generates an undefined index notice
$b = @$a['foo']; // suppresses the notice ( and assigns null )
$b = isSet( $a['foo'] ) ? $a['foo'] : null; // avoids the problem

> Aside - what is the cause of this error?
> As i remember the notice doesn't come from "explode". The error comes
> from the list...

Yes. Therefore it won't help to use @ on the explode().

> I'm not sure, but it look like explode does not
> generate two array members with explode if the string isn't found and
> the list now tries to use two, but only have one array member...

That is exactly what happens. And therefore list() does not know what to
assign to the second variable and generates a notice. Your code


list($first, $second) = explode("Second part match string", $text);

is equivalent to

$tmp = explode("Second part match string", $text);
$first = $tmp[0];
$second = $tmp[1];

So you must make sure that list() gets an array with at least two elements.


> I don't know how to init the vars to be save.

Not the vars, the array. You could try this:


list( $first, $second ) = array_pad( explode( $separator, $text ), 2, '' );

The last parameter of array_pad() is the default value you would like to
have in case the $separator is not found.

Greetings,
Thomas

--
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация