Do not display Warning de foreach

Asked

Viewed 263 times

2

Example, I have 2 warnings:

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

Warning: ksort() expects Parameter 1 to be array, integer Given in C: www teste.php on line 15 Warning: Invalid argument supplied for foreach() in C: www test.php on line 30

Not to print the Warning of ksort(), simply add "@" (getting @ksort()) and fix me.

But in the foreach, if I do this, @foreach, he returns error:

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


  • Why does this happen?
  • What would be the options to not print Warning of a foreach specific?
  • 3

    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.

  • @Andersoncarloswoss I know, but I just want to know if there is the option! I don’t need to treat the error.

  • 1

    to avoid this Warning I always put one if()

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

  • 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 the if would already be a treatment, and I did not want so... I really did not print the Warning! If I use a if, still I will do "nothing", ie, same thing as the "@".

  • @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

  • kkkkkk... is that you are very advanced, there goes the automatic focus on the "don’t do it, it’s wrong, for love!"...

  • Dear @Rbz edited reply, see if now meets your doubts

  • Solved Dr.! rs

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

  • 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 the foreach (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"...

Show 7 more comments

3 answers

2


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:

  • Guilherme, I know the errors, my array comes empty and causes Warning in the 2. What I would like to know is why @ works in ksort and not in foreach. And what is the option of masking Warning in foreach.

  • That, sorry... comes query error!

  • What a mistake I made! Thank you for clarifying.

  • @Guilhermenascimento the question is @in foreach... I posted in the question: Why does this happen? What would be the options of not printing the Warning of a specific foreach? ... I still don’t understand your answer

  • I know! But I just want to hide! rs ... has a purpose! swear!

  • Damn it, guys, I’m not stupid enough to patch, I just want to know, simply! (and yet, in my case)

Show 1 more comment

2

In addition to all problems associated with the use of @already cited in the reply of @Guilhermenascimento, PHP documentation is clear where the error control operator can be used @:

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

That is, it is simply something of the language. It cannot be used @ with conditional structures, so this represents a syntax error for the parser of PHP.

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

  • Very well completed! Thanks nunks! + 1

-3

Despite old topic, I will leave my solution to the problem presented. It has already been explained why of the error and etc etc etc

The solution to the problem of not generating the warning :

<?php

error_reporting(0);

Source: https://www.php.net/manual/en/function.error-reporting

  • Hello Fabio, I must disagree with your answer, although error_reporting seems to bring the same effect of display_errors=Off when using the value as zero, what actually happens internally is much more, I recommend reading: https://answall.com/q/106562/3635, out that the question problem here is about using @ in an incorrect place, turning off all errors will not prevent the script from stopping, only it will not display the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.