Can we use @ symbol in php variable name

I had an old set of codes from another developer, and I configure it on my server, there I saw a line

<?php @$Days = $_POST['Days']; ?> 

This code works well on my local setup, but as soon as I uploaded it to the server, it didn’t work and returned a network error, and all the / HTML code after this code also didn’t work.

Although I debugged this problem and removed it. In addition, I know that we use the @ symbol to handle errors, and I also read this question

My request is that there was an error in the above case, why it was not shown, if I want to check the error, then what should I do.

For error reporting, I will tell you that I have already used the code below

 <?php ini_set("display_errors", "1"); error_reporting(E_ALL); ?> 

So, please tell me why my code could not get past this statement, since I have about 100 such blocks of code. Are there any settings in php that could help me overcome this.

+5
source share
1 answer

@ - the error suppression operator in PHP, see the documentation .

In your example, it is used before the variable name to avoid the E_NOTICE error. If the Days key is not set in the $ _POST array, it will generate an E_NOTICE message, but @ is used there to avoid this E_NOTICE.

The reason that the code is not running on the server is probably due to disabling the scream.enabled directive in your php.ini configuration .

Disabling scream should fix the problem.

Change the directive in php.ini, for example:

 scream.enabled = 0 

If you want to disable it at run time, you can use ini_set as indicated in the manual:

 ini_set('scream.enabled', false); 

Edit

Someone in the comments indicated that I did not thoroughly understand my answer. I will try to change my error in this edit here :).

The reason for screaming (and disabling @) may / will break the code because the variable does not matter. If the rest of the code tries to use a variable, it throws an error.

In addition, E_NOTICE may throw an error if you attach an error handler to it. Quote from another question :

The above code will throw an ErrorException at any time E_NOTICE or E_WARNING is raised, effectively ending the script output (if the exception is not caught). PHP error exception exceptions combined with a parallel exception handling strategy (set_exception_handler) to gracefully stop production environments.

+4
source

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


All Articles