Do not display Warning of foreach

3

Example, I have 2 warnings :

ksort($var);
foreach($var as $v)...;
  

Warning: ksort () expects parameter 1 to be array, integer given in   Warning: Invalid argument supplied for C: \ www \ test.php   foreach () in C: \ www \ test.php on line 30

Not to print Warning from ksort() , I simply add " @ " (getting @ksort() ) and resolve it.

But foreach , if I do, @foreach , it returns error :

  

Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in   C: \ www \ test.php on line 30

  • Why is this happening?
  • What would be the options not to print Warning from a foreach specific ?
asked by anonymous 01.08.2018 / 13:18

2 answers

2

The @ in foreach issues the error:

  

Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C: \ www \ test.php on line 30

This is a syntax error , ie in the parse of the language, see that even in a certain script the error occurs:

% is not a function , it is part of the language syntax, so the interpreter has no sense of what you are doing, ie after foreach was expected thing but it came across the foreach and the message says "foreach not expected (T_FOREACH)"

For example, this would also be wrong for the syntax:

(foreach (range(1, 10) as $value) {
     var_dump($value);
});

See: link - the same error is issued

@ is part of the language statement, such as foreach , while , switch and for , see an example with if:

<?php
@if ($foo) echo 1;

This causes a similar error:

  

PHP Parse error: syntax error, unexpected 'if' (T_IF)

About suppressing warnings and errors with at @ @

Note that the if function does not return iterable (it does not return an array or iterable object) it returns boolean , as documentation link :

  

bool ksort (array & $ array [ int $ sort_flags = SORT_REGULAR])

Another thing, do not print on the screen warning does not mean that it did not occur, ksort is just like putting a curtain on anyone to see what you are doing behind it, behavior does not change at all internally, it is only hidden, but still the script is flawed and will trigger errors.

In other words, the @ is only to suppress, but does not mean that it does not occur, in fact it is very useless in my opinion, for many cases @ in display_errors=Off would solve already, I recommend that you read those links in order:

01.08.2018 / 13:38
2

In addition to all the problems associated with using @ already mentioned in @GuilhermeNascimento's response, documentation of PHP is clear as to where the error control operator @ can be used:

  

Note: The @ operator works only on expressions . A rule   simple way to remember this: if you can get the value of some   thing, you can prefix this with the @. So you can prefix   variable calls, functions, and includes , constants and the like.    You can not prefix function or class definitions, conditional structures like if, foreach, and so on. (emphasis mine)

That is, it is simply something of the language. You can not use @ with conditional structures, so this represents a syntax error for PHP's parser .

As a "contour" (a gambiarra, go), I imagine you can quit your foreach in a function and then call the function with @ . The foreach syntax will be correct, and since the function call is an expression, any errors contained in it should be suppressed.

    
01.08.2018 / 14:16