Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use @ symbol in variable name php

Tags:

I had an old codesetup from some other developer and I am setting up the same on my server, there I saw a line

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

This code runs well on my local setup but once I uploaded it on server this did not worked and returned a network error and all the code/HTML after this code also did not work.

Although, I debugged this issue and have removed this. Also, I know that to handle the errors we use the @ symbol, and also I have read this question

My query is that what was the error in the above case, why was it not shown, if I want to check the error then what shall I do.

For error reporting I shall tell you that I already used the below code

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

So please tell me why was my code unable to get past this statement, as I have around 100's of such code blocks. Is there any setting in php which could help me to get over this.

like image 948
Rohit Ailani Avatar asked Oct 06 '16 05:10

Rohit Ailani


1 Answers

The @ is the error suppression operator in PHP, have a look at the documentation.

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

The cause of the code not working on the server is probably due to the scream.enabled directive in your php.ini configuration being disabled.

Disabling scream should fix the issue.

Change the directive in your php.ini, like so:

scream.enabled = 0

If you want to disable it during run-time, then you can use ini_set as stated in the manual:

ini_set('scream.enabled', false);

Edit

Someone in the comments pointed out I haven't been thorough enough with my answer. I will try to amend my mistake in this here edit :).

The reason scream (and disabling the @) can / will break the code is due to the fact that the variable doesn't have a value. If the remainder of the code tries to use the variable it will throw an error.

Also, an E_NOTICE can throw an error if you attach an error handler to it. A quote from another question:

The above code will throw an ErrorException any time an E_NOTICE or E_WARNING is raised, effectively terminating script output (if the exception isn't caught). Throwing exceptions on PHP errors is best combined with a parallel exception handling strategy (set_exception_handler) to gracefully terminate in production environments.

like image 140
Rick van Lieshout Avatar answered Oct 13 '22 20:10

Rick van Lieshout