Why PHP Variables Start with a $ Sign Symbol

Why PHP variables start with a $ sign symbol?

Because PHP was based on Perl which used $, though the symbols Perl used were meaningful and plenty used to indicate the data type, ( such as @ used to indicate an array ) PHP just has $.

PHP in its early stages was a simplistic version of Perl but over time incorporated more of Perl's features, though one may argue PHP was for a long time a simplistic primitive version of Perl since before PHP 5.3 it did not include features that have been around in other languages such as closures/namespacing.

http://en.wikipedia.org/wiki/PHP

Larry Wall, the creator of Perl, was inspired to use $ from shell scripting: http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29

What is the purpose of PHP having a symbol (the dollar sign) in front of each variable?

Having a symbol to denote variables makes string interpolation simple and clear. Shell, Perl and PHP grew out of the need for quick and easy string manipulation, including interpolation, so I imagine using a variable prefix seemed like a good idea.

I.e. in PHP:

$var = 'val';
$strVar = "The var is $var";

Compare to typical string formatting:

var = 'val'
strVal = 'The var is %s' %(var)

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);

Why does PHP have a $ sign in front of variables?

It prevents variable names conflicting with language keywords, and allows them to be interpolated in strings.

What does the & sign mean in PHP?

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

$a = 1;

function inc(&$input)
{
$input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

If you like to handle a copy over to a function, use

clone $object;

Then, the original object is not altered, eg:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

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.



Related Topics



Leave a reply



Submit