I was looking at some PHP code:
<?php
-$username = "admin";
-$password = "secret";
-$database = "mystore";
mysql_connect("localhost", $username, $password);
mysql_select_db($database);
What is the function of the minus-sign in front of the variable names?
If I do the same in the PHP interpreter, it results in valid code and there seems to be no difference:
$ php -a
Interactive shell
php > $a=1;echo $a;
1
php > -$a=2;echo $a;
2
I asked Google but she couldn't help me.
PHP abs() Function echo(abs(6.7) . "<br>"); echo(abs(-6.7) . "<br>");
To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).
Hello @kartik, The @ symbol is the error control operator ("silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.
A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e. $$a = 'world'; ?>
-$username = "admin";
is evaluated as - ($username = "admin");
That is, a prefix unary operator -
applied to the expression.
The expression consists of an assignment only.
So a string is assigned to a variable, then as per php syntax the expression returns the same value which is implicitly converted to a number and negated. Then the result is thrown away.
So there is no special meaning here, someone put it there accidentally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With