8
I always see programmers saying : "Oh if you do this you will receive one notice
, but if you do that you get a warning
", then I came up with the debt :
What’s the difference between them?
8
I always see programmers saying : "Oh if you do this you will receive one notice
, but if you do that you get a warning
", then I came up with the debt :
What’s the difference between them?
9
A notice
is a warning that means probably you shouldn’t be doing what you’re doing, but I’ll allow it anyway.
A warning
is a message stating that you is doing something wrong and it is likely that this will cause errors, you better fix!
Both will not stop the execution of the script, but it is better to analyze and fix to have no Warning nor notice in your application.
Source: here.
7
Though it may not seem Warning is an error. It is something that does not prevent the execution of the code, it may eventually produce the expected result, but it should not be done that way. Must be treated as a mistake.
Already the notice is an information that can be useful the programmer to give an assessment if you need to improve something there, usually need.
Only XGH programmers ignore warnings and do not at least investigate us notices.
As a matter of curiosity I already took a software of 120 thousand lines that had more than 600 thousand warnings. This is tragic, the software was working by sheer coincidence, you were messing with something started giving problem in the whole software.
6
One of the things that change is the value of the constant, 2 for Warning and 8 for notice. Basically they’re different information levels. Indicate that the code executes some instruction that can eventually generate an error and stop the script.
The interpreter says something like "look this instruction is strange, please check if this is really what you want programmer".
Taken 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 a script.
To what constant you refer?
@Magichat which is on the response link in case 2 and 8.
Hummm....Errors and Logging
....OK...Tks
5
In PHP there are four types of errors "common":
Parse Error:
The Parse Error is triggered when a code is literally broken, with syntax errors, for example without the ;
at the end of any line or with {}
missing, for example:
<?php
$mensagem = 'uma coisa'
if(strlen($mensagem) > 10){
}
In case one would be expected ;
at the end of 'uma coisa'
, the result is this "Parse error: syntax error, Unexpected 'if' (T_IF)".
The Parse Error prevents your code from working from the start, it simply won’t work at all.
Fatal Error:
The Fatal Error is when the code has no syntax errors, but it cannot be executed for some reason. One of the main reasons is when trying 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();
, is impossible to execute the echo funcao_inexistente();
, resulting in "Fatal error: Uncaught Error: Call to Undefined Function funcao_inexistente();"
A caveat is that your code may 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 the funcao_inexistente()
does not exist it makes no difference until it is called. You will get as a result ALWAYS the Antes do IF
. Also, you have chances to receive one Aqui dá certo
without any error or receiving a fatal error.
Warning Error:
The 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 (and also common) example 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 by not informing $i
, but will return a Warning, "Missing argument 2 for funcao()" to inform that something was expected but is still able to proceed.
If use
funcao(string $s, string $i){}
would return a Fatal Error if the specified type did not correspond to what is defined!
Notice Error:
This is by far the most common of all other types. Usually it indicates that a variable, array, constant cannot be accessed. However, implicitly if it is there is because something is not being treated correctly, for example:
<form>
<input name="usuario">
<!-- .... -->
</form>
You would have the unique belief that usuario
were a string, but may send a array, (usuario['muito_louco']=valor
), soon:
echo $_POST['usuario'];
Will issue a "Notice: Array to string Conversion". Besides there may not even be usuario
then generating a "Notice: Undefined index".
The Notice
by itself indicates a bigger problem than himself, if you can say so, usually if it exists is because some data entered (or left) in a way that was not predicted by his system. However, for PHP, your code would be able to support this without damage.
Obviously there are other types of errors, such as Deprecated
, for example.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
"Although it may not seem Warning is a mistake." That’s an irony?
– MagicHat
@Magichat why would it be?
– Maniero
Ah I think that an alert has a direct relation with the error, so there is no reason not to seem.
– MagicHat