开发者

how to hide foreach wrong?

开发者 https://www.devze.com 2023-03-19 07:02 出处:网络
i want to hide foreach wrong. For example error: Warning: Invalid argument supplied for f开发者_运维技巧oreach() in C:\\AppServ\\www\\ebook\\ara.php on line 65

i want to hide foreach wrong. For example error:

Warning: Invalid argument supplied for f开发者_运维技巧oreach() in C:\AppServ\www\ebook\ara.php on line 65

how i can hide?


Never hide errors. Errors mean that there is something wrong, you should try to fix the underlying problem rather than hide it

Also, we need to see some code so we know what you're passing to the foreach. It much be an array


There are several ways:

1) Check the variable first

  if(isset($rows) && is_array($rows))
  {
      foreach($rows as $row) { ... }
  } 

2) Use the error suppression operator (don't do this please...)

  foreach(@$rows as $row) { ... }\

3) Turn off all errors (better not do this or you will be sorry!)

  error_reporting(E_NONE);


make sure that foreach() has count

if (count($array))
{
   foreach($array as $value)
   {
      //do stuff
   }
}


Make sure the variable you pass to it is an array/exists.

Please post some code and we can help you further.

And as other's are saying here, you don't want to hide errors.. They'll cause your script to break, it's bad practice and it get's you nowhere.

You can however supress errors by putting an @ symbol before the function.


You can set error reporting to false in your php.ini but this is not recommended. It looks like you should fix the error before trying to figure out how to hide it.

error_reporting(0);

PHP Error Reporting


As BoltClock said, you should definitely fix this problem. If you don't, it could lead you to serious trouble later.

The following are a couple of ways to hide errors. I put them here for educational purposes... you shouldn't use them without good reason (and that is almost never!).

You can put @ in front of a function.

You can change display_errors.

You can change error_reporting.


// Turn off all error reporting
error_reporting(0);


Place an @ symbol in front of the line of code that is causing the error. The @ symbol causes PHP to suppress errors that occur on the line. This is not usually recommended unless you have a good reason for it. It is better to fix errors.


You cannot use the @ symbol for a foreach

PHP Manual: foreach does not support the ability to suppress error messages using '@'.

0

精彩评论

暂无评论...
验证码 换一张
取 消