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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.