开发者

php {$var} meaning

HEllo,

What is the meaning of {$var} in PHP?

Example:

$query = 开发者_Python百科"UPDATE table SET field = '{$var}'";

Thx.


{$var} "shields" the variable name from surrounding characters. For example:

$root = "stick";
echo "{$root}y"; # Adjectify!

Will output "sticky", where:

$root = "stick";
echo "$rooty"; # Adjectify! No, just kidding.

Will output nothing at all, since the variable $rooty doesn't exist.

It also allows you to use expressions that are more than just variable names, like array indexing or property access.


The general purpose i've found with using {$var} is that it tells PHP to explicitly reference what is inside as a variable. It's most used when doing something like

$qry = "UPDATE table SET field = '{$var['myvar']}'"; or
$qry = "UPDATE table SET field = '{$var->property}'";

Without the {} around my variable inside the string, it wouldn't know when the variable began and ended and usually causes PHP to throw an error.


It also allows you to do variable variables

$var = "Sup";
$sup = "Hello!";

echo ${$var};    // outputs Hello! 


$var is a variable, a symbolic name associated with a value and whose associated value may be changed the {} makes sure it gets handled as one in it's context all 4 of these are valid and mean the same thing...

$query = "UPDATE table SET field = '{$var}'";
$query = "UPDATE table SET field = '$var'";
$query = "UPDATE table SET field = '".$var."'";
$query = 'UPDATE table SET field = \''.$var.'\'';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜