What is the difference between "notice" and "warning" in PHP?

9

I always see programmers saying, "Ah, if you do, you'll get a notice , but if you do, you get a warning ", so I got the debt:

What's the difference between them?

    
asked by anonymous 23.02.2017 / 20:58

4 answers

5

In PHP there are four common "" error types:

  • Parse Error / Syntax Error
  • Fatal Error
  • Warning Error
  • Notice Error

Parse Error :

Parse Error is triggered when a code is literally broken, with syntax errors, eg without ; at the end of some line or with {} missing, for example:

<?php

$mensagem = 'uma coisa'

if(strlen($mensagem) > 10){
}

In case a ; would be expected at the end of 'uma coisa' , the result is this " Parse error: syntax error, unexpected 'if' (T_IF)

Parse Error prevents your code from working from the start, it just will not work at all.

Fatal Error :

The Fatal Error is when the code has no syntax errors, however it can not be executed for some reason. One of the main reasons is when you try to call a non-existent function, for example:

echo funcao_inexistente();

This situation is all "ok", there is no syntax error, the problem is that there is no function funcao_inexistente(); , it is impossible to execute echo funcao_inexistente(); , resulting in "Fatal error: Uncaught Error: Call to undefined function function_init (); "

One caveat is that your code can work until the non-existent function is called, for example:

echo 'Antes do IF';

if( random_int(0,1) ){
   funcao_inexistente(); 
}else{
   echo 'Aqui dá certo';
}

Although funcao_inexistente() does not exist, it does not matter until it is called. You will ALWAYS have Antes do IF . Also, you have a chance to receive a Aqui dá certo without any error or receive a fatal error.

Warning Error :

Warning is because something is potentially wrong, but PHP still allows it to continue. The most famous is Headers already sent , but to give another example (and also common) is not to inform all arguments / parameters of the function:

function funcao($s, $i){
    return 'texto';
}

echo funcao('aaa');

PHP by default will not return an error for not reporting the $i , but will return a Warning , " Missing argument 2 for function () " to inform you that something is expected but is still able to continue.

  

If you use funcao(string $s, string $i){} it would return a Fatal Error if the specified type did not match the one defined!

Notice Error :

This is by far the most common of all other types. Normally it indicates that a variable, array, constant can not be accessed. But implicitly if it's there it's because something is not being handled correctly, for example:

<form>
<input name="usuario">
<!-- .... -->
</form>

You would have the only belief that usuario was a string , however it might send an array , ( usuario['muito_louco']=valor ), then:

>
echo $_POST['usuario'];

It will issue an "Notice: Array to string conversion". Also, you can not even usuario generate a Notice: Undefined index .

The% of% by itself indicates a problem greater than itself, if it can say so, usually if it exists is because some data entered (or left) in a way that was not predicted by its system. However, for PHP, your code would be able to support this without harm.

Obviously there are other types of errors, such as Notice , for example.

    
23.02.2017 / 23:50
9

A notice is a warning that means you probably should not be doing what you're doing, but I'll allow it anyway.

A warning is a message stating that you are doing something wrong and it is likely that this will cause errors, you better fix it!

Both will not stop the execution of the script, but it is better to analyze and correct to have no warning or notice in your application.

Source: here .

    
23.02.2017 / 21:04
7

Although it may not seem warning is an error. It is something that does not impede the execution of the code, it can eventually produce the expected result, but it should not be done that way. It should be treated as an error.

notice is information that can be useful for the programmer to evaluate if he needs to improve something there, usually he needs it.

Only XGH programmers ignore warnings and do not do at least an investigation into notices .

As a matter of curiosity, I already got software from 120,000 lines that had more than 600,000 warnings . This is tragic, the software worked by pure coincidence, you messed up something started giving problem in the whole software.

    
23.02.2017 / 21:05
6

One of the things that change is the constant value, 2 for warning and 8 for notice. Basically they are different 'levels of information'. They indicate that the code executes some statement that may eventually generate an error and stop the script.

The interpreter says something like "look at this statement is strange, please check if that's what you want to programmer."

Retrieved from manual

Warning:

  

2 - Run-time warnings (non-fatal errors). Execution of the script is not halted.

Notice:

  

8 - Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running script.

    
23.02.2017 / 21:05