The @
in the foreach
issues the error:
Parse error: syntax error, Unexpected 'foreach' (T_FOREACH) in C: www test.php on line 30
for that is a "syntax error", i.e., in the parse of the language, see that even in a right script the error occurs:
The foreach
is not a function, it is part of the syntax of the language, for the interpreter this has no sense of what you are doing, ie after the @
something else was expected but he 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);
});
Behold: https://ideone.com/fdcxoU - the same error is issued
The foreach
is part of the instruction of language, as while
, switch
, for
and if
, see an example with if:
<?php
@if ($foo) echo 1;
This causes a similar error:
PHP Parse error: syntax error, Unexpected 'if' (T_IF)
On suppressing warnings and errors with arroba @
Keep in mind that function ksort
does not return iterable (does not return an array or eternal object) it returns boolean, as documented http://php.net/manual/en/function.ksort.php:
bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Another thing, don’t print on the screen the Warning does not mean that it did not occur, the @
it’s just like putting up a curtain so no one can see what you’re doing behind it, the behavior doesn’t change at all internally, it’s just hidden, but still the script is flawed and will trigger errors.
Namely the @
is only to suppress, but does not mean that it does not occur, in fact it is quite useless in my opinion, for many cases display_errors=Off
in the php.ini
would solve already, recommend you read these links in order:
Option not to print the Warning is to solve the problem. If there is no problem, there will be no Warning. Take as a rule for life: always try to solve the problem, not just omit it.
– Woss
@Andersoncarloswoss I know, but I just want to know if there is the option! I don’t need to treat the error.
– rbz
to avoid this Warning I always put one
if()
– Wees Smith
No one thinks you’re stupid, but for sure, you think you "serve your case", but in fact it’s your mistake, because it just "seems to serve", in fact this is a mistake and we’re just trying to guide you.
– Guilherme Nascimento
I don’t need to treat, otherwise I’d just make a
if
, but I don’t want to treat, it’s on purpose. I understand 100% of the "patch," and I’m sure of it. I just wanted to know why it doesn’t work in foreach, and what options you have. That’s it. In this case theif
would already be a treatment, and I did not want so... I really did not print the Warning! If I use aif
, still I will do "nothing", ie, same thing as the "@".– rbz
@Rbz Ahhhh, I get it, I’ll summarize that part in the answer. Anyway to explain this you could have used an example without errors, so it wouldn’t lead us to another understanding right? But now I’ll try to guide you through it
– Guilherme Nascimento
kkkkkk... is that you are very advanced, there goes the automatic focus on the "don’t do it, it’s wrong, for love!"...
– rbz
Dear @Rbz edited reply, see if now meets your doubts
– Guilherme Nascimento
Solved Dr.! rs
– rbz
The question would no longer be how the operator
@
works ? It is that it generated a lot of confusion with the code that has and it seemed to me that the focus of the question was just this– Isac
@Isac so is, the title was what led to confusion of understanding, whether something like,
"por que @ não funciona com foreach?"
instead of"Não exibir Warning de foreach"
, since actually @foreach doesn’t even have Warning what it has is "Parse error", which is a totally different kind of error.– Guilherme Nascimento
It is that I thought like this: "Do not display Warning of foreach" which is related to the 2nd question "What would be the options of not printing the Warning of a specific foreach?" ... Then I wanted to give an example, working on
ksort
and not in theforeach
(remembering that it is related to Warning), then it would be the 1st question "Why does this happen?" (does not work the @noforeach) .... I think the confusion was more because in fact "you should not use this (the '@') even if there is this function" rs ... analogy: "Even if you can jump out the car window, it is not correct to do this"...– rbz