At' Symbol Before Variable Name in PHP: @$_Post

At' symbol before variable name in PHP: @$_POST

The @ is the error suppression operator in PHP.

PHP supports one error control
operator: the at sign (@). When
prepended to an expression in PHP, any
error messages that might be generated
by that expression will be ignored.

See:

  • Error Control Operators
  • Bad uses of the @ operator

Update:

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

Note that you can also put this line on top of your script to avoid an E_NOTICE error:

error_reporting(E_ALL ^ E_NOTICE);

difference between $variable and @$variable in php

A @ before a function call means "suppress warnings".

So, @$doc->LoadHTML($html); suppresses warnings from the method call (LoadHTML()).

In general this is a bad idea, because the warnings mean you are doing something wrong, and you would better fix that instead of playing deaf.

PHP: What does a & in front of a variable name mean?

It passes a reference to the variable so when any variable assigned the reference is edited, the original variable is changed. They are really useful when making functions which update an existing variable. Instead of hard coding which variable is updated, you can simply pass a reference to the function instead.

Example

<?php
$number = 3;
$pointer = &$number; // Sets $pointer to a reference to $number
echo $number."<br/>"; // Outputs '3' and a line break
$pointer = 24; // Sets $number to 24
echo $number; // Outputs '24'
?>

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

uknown condition for if statement '@'

@ before the variable is used to suppress the warning generated for that variable. This is also relevant to 'At' symbol before variable name in PHP: @$_POST.

Symbol in PHP I've never come across before

It is used for passing values by reference rather than by value which is default in php.

Can we use @ symbol in variable name php

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.

Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?

Here's PHP.net's explanation of why it does it:

Dots in incoming variable names

Typically, PHP does not alter the
names of variables when they are
passed into a script. However, it
should be noted that the dot (period,
full stop) is not a valid character in
a PHP variable name. For the reason,
look at it:

<?php
$varname.ext; /* invalid variable name */
?>

Now, what
the parser sees is a variable named
$varname, followed by the string
concatenation operator, followed by
the barestring (i.e. unquoted string
which doesn't match any known key or
reserved words) 'ext'. Obviously, this
doesn't have the intended result.

For this reason, it is important to
note that PHP will automatically
replace any dots in incoming variable
names with underscores.

That's from http://ca.php.net/variables.external.

Also, according to this comment these other characters are converted to underscores:

The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):

  • chr(32) ( ) (space)
  • chr(46) (.) (dot)
  • chr(91) ([) (open square bracket)
  • chr(128) - chr(159) (various)

So it looks like you're stuck with it, so you'll have to convert the underscores back to dots in your script using dawnerd's suggestion (I'd just use str_replace though.)

What is the use of the @ symbol in PHP?

It suppresses error messages — see Error Control Operators in the PHP manual.



Related Topics



Leave a reply



Submit