Consider the code:
$a = "foobar";
echo $a{3}; // prints b
I know $a[3]
is 'b'
but how come using {}
in place of []
produec the same result ?
The structure which defines PHP computer language is called PHP syntax. The PHP script is executed on the server and the HTML result is sent to the browser. It can normally have HTML and PHP tags.
The object operator, -> , is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator.
The <= tag is called short open tag in PHP. To use the short tags, one must have to enable it from settings in the PHP. ini file. First of all ensure that short tags are not disabled, To check it, go into php.
=> is referred to as double arrow operator. It is an assignment operator used in associative arrays to assign values to the key-value pairs when creating arrays. It is placed in between the key and the value and assigns what is on its right(value) to what is on its left(key).
You can read here in official documentation about braces:
String s may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
EDIT This thread may interest you: "dropping curly braces"
It's just an alternative syntax; the two forms compile to exactly the same bytecode:
<?php
$str = "aaab";
echo $str{3};
echo $str[3];
number of ops: 9 compiled vars: !0 = $str line # * op fetch ext return operands --------------------------------------------------------------------------------- 2 0 > EXT_STMT 1 ASSIGN !0, 'aaab' 3 2 EXT_STMT 3 FETCH_DIM_R $1 !0, 3 4 ECHO $1 4 5 EXT_STMT 6 FETCH_DIM_R $2 !0, 3 7 ECHO $2 8 > RETURN 1
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