Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach with unexisting array 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys :)


Just one quick question : how could you get rid of the if() condition in the following code in order to make it shorter?
Because when $array doesn't exist, foreach throws errors.

Code:
if (is_array($array)) {

    foreach ($array as $key => $value) {
    
    // do something
        
    }

}

Thanks! :)
 
you could suppress the error but far better would be to know in advance whether the array exists; ie through your code design.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top