What Is the Use of the @ Symbol in PHP

What is the use of the @ symbol in PHP?

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

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

What is the purposes symbol @ in PHP $variable at blade laravel 5?

The @ is used as error control operator at php. Php will not throw any error generated from the exception.

{{ old('title_id', @$result->title_id) }}.
The purpose to use the @ symbol here is to prevent the program from throw exception like "try to call title_id from an empty object". Instead it goes with define the variable with null value. It sometimes usefull when you want to do the edit / create on the same blade file.

If you dont use @ symbol, you must handle if the variable is empty or not first and you ended up with code like

{{ old('title_id', $result ? $result->title_id : '') }}

You can read more detail at php documentation.

@ symbol before php function

It's the error suppression operator, normally not a good idea to use it as you should be trapping errors cleanly rather than simply hiding them

What does = mean in PHP?

=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass.

Example:

$user_list = array(
'dave' => 'apassword',
'steve' => 'secr3t'
);

foreach ($user_list as $user => $pass) {
echo "{$user}'s pass is: {$pass}\n";
}
// Prints:
// "dave's pass is: apassword"
// "steve's pass is: secr3t"

Note that this can be used for numerically indexed arrays too.

Example:

$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw

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.



Related Topics



Leave a reply



Submit