Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function of a minus sign in fron of a variable definition in PHP?

Tags:

php

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.

like image 439
NZD Avatar asked Apr 30 '15 02:04

NZD


People also ask

How do you make a value negative in PHP?

PHP abs() Function echo(abs(6.7) . "<br>"); echo(abs(-6.7) . "<br>");

What does & in front of variable mean PHP?

To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).

What is the mean of @symbol in PHP?

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.

What is variable variable in PHP?

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'; ?>


1 Answers

-$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.

like image 136
zerkms Avatar answered Nov 15 '22 13:11

zerkms