⬆️ ⬇️

PHP Is the dog evil?

I wondered when the use of β€œ@” (dogs) for error suppression is justified, which allows to suppress errors not only for operators, but also to replace calls for checks using β€œisset” (if (@ $ _ GET ['param']) ...)





To overcome the display of errors in several ways. For example:



1. Operator "@":

$ a = @ (57/0);

In this case, we bypass the check of division by zero. A quick solution for a lazy coder. I note that this quick solution is a little slower, and debugging of the code is also complicated.

')

2. catch by operators try ... catch

try {operators where errors can occur} catch (Exception $ e) {react to an error}



3. Set the desired alert level.

If the variables are not initialized (do not exist), the necessary error_reporting and display_errors are often set to zero and do not bother with checks:



$ b + = (int) $ _ GET ['missing variable'];



instead:



if (isset ($ _ GET ['variable'])) {

$ b + = (int) $ _ GET ['variable']);

}



Do you think the use of "@" indicates a programmer's incompetence? Or is the use of this operator justified in some cases (fopen, mysql_connect ...)?



I would also like to give an example of a simple function that I often use to check for the presence of indices in an array. If the element exists, it is returned by the function.



function chk ($ array, $ i) {

return isset ($ array [$ i])? $ array [$ i]: null;

}



Looks like that:

if ($ var = (int) chk ($ _GET, 'index')) {

$ b + = $ var

}



And you can also check for the presence of the passed variable in the array:

if (in_array ($ var = chk ($ _ GET, 'index'), array ('hello', 'world'))) {

... $ var variable is not empty and is present in the array ...

}



I would be glad if someone come in handy (:

Source: https://habr.com/ru/post/30030/



All Articles