Using Curly Braces with $_REQUEST to retrieve its indexes
I've been revi开发者_运维知识库ewing a sample code to learn PHP and what I saw made me confused in the following code:
$in = $_REQUEST;
require_once 'utils.php';
require_once 'utils_user.php';
require_once 'utils_email.php';
require_once 'utils_affiliate.php';
#Load Settings
$globalSettings = parse_ini_file("settings.ini", true);
if(isInDevelMode())
{
$system = "devel";
}
if(strlen($in{'system'} > 0))
{
$system = strip_tags($in{'system'});
}
Here a variable called $in
was created and $_REQUEST
was assigned to it and then later on in the second if-statement it was written like if(strlen($in{'system'} > 0))
and I don't quite understand what $in{'system'}
here. It doesn't look like an array but $_REQUEST is an array-like to retrieve its content. Can somebody tell me what these curly braces are used for after $in
variable...
Thanks.
The curly braces can be used to access array keys instead of the square ones.
$array{'a'} === $array['a']
The syntax is exactly the same with the only difference that curly brackets can't be used for pushing values:
$array{} = 'x'; // doesn't work
$array[] = 'x'; // works
This alternative syntax was originally intended for use when accessing string offsets. But now it is just an old, obsolete syntax, which is likely to be deprecated. (Actually it already was deprecated some time while PHP 5.3 was developed.)
Never saw that notation. But other than array it can't be nothing.
Also I think there is an error here:
strlen($in{'system'} > 0)
should be
strlen($in{'system'}) > 0
Dunno who wrote that code but it's pretty a bad code, maybe someone coming from other languages
This is an alternative variable syntax and variable interpolation. All of the following are the same thing.
$in{'system'}
$in['system']
$_REQUEST{'system'}
$_REQUEST['system']
The ${''}
syntax is not common in PHP and more or less legacy syntax from Perl. Also, don't use $_REQUEST
. It's a security risk.
UPDATE
My answer assumes that the user intended to use variable interpolation for system
but was confused by the alternate syntax.
I believe what your intended code is $in[$system]
versus $in{'system'}
.
精彩评论