string - PHP Syntax ${"field"} -
this question has answer here:
- curly braces in string in php 5 answers
i don't recognize php syntax ${"item_$i"}; item_$i can item_1 through item_6. ${ } indicate in assignment? can't find example in searches.
if($cl_name=='a') $data = ${"item_$i"};
thanks help.
it's called complex (curly) syntax
this isn't called complex because syntax complex, because allows use of complex expressions.
any scalar variable, array element or object property string representation can included via syntax. write expression same way appear outside string, , wrap in { , }. since { can not escaped, syntax recognized when $ follows {. use {\$ literal {$. examples make clear:
<?php // show errors error_reporting(e_all); $great = 'fantastic'; // won't work, outputs: { fantastic} echo "this { $great}"; // works, outputs: fantastic echo "this {$great}"; echo "this ${great}"; // works echo "this square {$square->width}00 centimeters broad."; // works, quoted keys work using curly brace syntax echo "this works: {$arr['key']}"; // works echo "this works: {$arr[4][3]}"; // wrong same reason $foo[bar] wrong outside string. // in other words, still work, because php first looks // constant named foo; error of level e_notice (undefined constant) // thrown. echo "this wrong: {$arr[foo][3]}"; // works. when using multi-dimensional arrays, use braces around arrays // when inside of strings echo "this works: {$arr['foo'][3]}"; // works. echo "this works: " . $arr['foo'][3]; echo "this works too: {$obj->values[3]->name}"; echo "this value of var named $name: {${$name}}"; echo "this value of var named return value of getname(): {${getname()}}"; echo "this value of var named return value of \$object->getname(): {${$object->getname()}}"; // won't work, outputs: return value of getname(): {getname()} echo "this return value of getname(): {getname()}"; ?>
Comments
Post a Comment