Jump to content

Don't perform a loop when value is empty?


completeamateur

Recommended Posts

Hi,

 

I am running a loop to generate the meta tags for a page, but if the database contains no meta tags, php throws a warning...

 

Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/villa4life/application/layouts/layout.phtml on line 6

 

foreach($this->metadata as $metadata)

 

Is there anyway to prevent this? TIA

Link to comment
https://forums.phpfreaks.com/topic/128271-dont-perform-a-loop-when-value-is-empty/
Share on other sites

you can check prior to the loop, by comparing $this->metadata to NULL, for example:

 

if ($this->metadata != null) {

    //Do your foreach

    //etc

 

You can also turn down the errorlevel so that it doesn't include warnings, if there's no particular logic concern.  Of course on a production server you should not have error reporting on anyways, as you will want to log errors instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.