Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this PHP syntax mean?

Tags:

syntax

php

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 ?

like image 547
Zacky112 Avatar asked Jul 08 '10 12:07

Zacky112


People also ask

What do you mean by PHP syntax?

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.

What does this -> mean in PHP?

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.

What does the tag <?= Signify in PHP?

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.

What does this mean ->?

=> 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).


2 Answers

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

like image 109
silent Avatar answered Sep 21 '22 17:09

silent


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
like image 25
Artefacto Avatar answered Sep 23 '22 17:09

Artefacto