php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6118 Can not supress runtime warnings on foreach using @ operator
Submitted: 2000-08-12 02:29 UTC Modified: 2013-10-27 15:14 UTC
Votes:17
Avg. Score:3.9 ± 1.3
Reproduced:12 of 14 (85.7%)
Same Version:2 (16.7%)
Same OS:3 (25.0%)
From: ronen at greyzone dot com Assigned: krakjoe (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ronen at greyzone dot com
New email:
PHP Version: OS:

 

 [2000-08-12 02:29 UTC] ronen at greyzone dot com
@foreach($items as $key => $val) {}

does not work:

<?
$test[123]="one hundred twenty three";
$test[13]="thirteen";
$test[11]="eleven";


// try to supress warnings on foreach
@foreach($test as $key => $value) {
        echo "TEST[$key]=$value<br>";
}

/***** THE OUTPUT IS


Parse error: parse error in /home/httpd/brocadedocs/ronen/secure/testtest.html on line 10
****/

?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-12 02:40 UTC] derick@php.net
At the moment it seems that parse errors can not be suppressed, but I think it is not possible to do it, and it is absolutely not useful.

From the manual:
"All PHP expressions can also be called with the "@" prefix, which turns off error reporting for that particular expression"

But if PHP can see there is an expression (because of an parse error), there is no way to disable warnings from that statement.
 [2000-08-12 03:01 UTC] sterling@php.net
Actually its not that...
   foreach is a statement, you can't suppress warnings for statements.
 [2000-08-12 03:12 UTC] stas@php.net
Well, it's still a valid feature request. Why to be overzealous to close it, maybe somewhere in PHP 6.5 we decide to implement it? :)
 [2008-03-04 23:10 UTC] slight_ at hotmail dot com
I'd just like to add a vote for this as a new feature. I have countless foreach blocks wrapped in if( is_array( $x ) ){} where the only reason to check is to avoid raising a warning.
 [2010-11-19 00:09 UTC] jani@php.net
-Summary: Can not supress runtime warnings on foreach +Summary: Can not supress runtime warnings on foreach using @ operator -Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Redhat Linux 6.2 +Operating System: * -PHP Version: 4.0.1pl2 +PHP Version: *
 [2012-02-23 00:29 UTC] sanibelmatt7 at aol dot com
just as 'slight_ at hotmail dot com ' had put it, they best way to get rid of it is a simple if/else loop, say 

 if(!$x) {
echo "error" 
} 
else {
//code//
}
it worked on my the website i am developing so hopefully it'll work for you
 [2012-02-23 00:31 UTC] sanibelmatt7 at aol dot com
with $x of course being the returned array of a query
 [2013-10-27 15:14 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-10-27 15:14 UTC] krakjoe@php.net
The foreach construct allows you to use suppression operator preceding the variable being traversed.

That is as much use as you can reasonably get from the suppression operator in the foreach construct.

Closing the bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC