if statement parameter for an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

if statement parameter for an array

Post by JackD »

The following code is working on our website:

$EAN_List = (array)$parsed_xml->Items->Item->ItemAttributes->EAN;
if (is_array($EAN_List) and $EAN_List[0])
{ foreach ($EAN_List as $i=>$Item_Data)
{ echo $Item_Data; }
}

What does the $EAN_List[0] parameter do? Why could I not just check to see if it is an array and not include the $EAN_List[0]?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: if statement parameter for an array

Post by cpetercarter »

Code: Select all

 
if ($EAN_list[0])
 
is a way of writing

Code: Select all

 
if ($EAN_list[0] == true)
 
$EAN_list[0 ]will evaluate to true if it contains a non-zero value.So, the 'if' test in your code is asking:

is $EAN_list an array? And does the first element of the array contain a non-zero value?
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: if statement parameter for an array

Post by JackD »

Thank you very much!! :mrgreen:
Post Reply