[php] What does '<?=' mean in PHP?

<?php

$a=1;

?>
<?=$a;?>

What does <?= mean exactly?

This question is related to php syntax

The answer is


<?=$a; ?>

is a shortcut for:

<?php echo $a; ?>

It's a shorthand for this:

<?php echo $a; ?>

They're called short tags; see example #2 in the documentation.


As of PHP 5.4.0, <?= ?> are always available even without the short_open_tag set in php.ini.

Furthermore, as of PHP 7.0, The ASP tags: <%, %> and the script tag <script language="php"> are removed from PHP.


It means assign the key to $user and the variable to $pass

When you assign an array, you do it like this

$array = array("key" => "value");

It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

According to the PHP Manual, the '=>' created key/value pairs.

Also, Equal or Greater than is the opposite way: '>='. In PHP the greater or less than sign always goes first: '>=', '<='.

And just as a side note, excluding the second value does not work like you think it would. Instead of only giving you the key, It actually only gives you a value:

$array = array("test" => "foo");

foreach($array as $key => $value)
{
    echo $key . " : " . $value; // Echoes "test : foo"
}

foreach($array as $value)
{
    echo $value; // Echoes "foo"
}

Arrays in PHP are associative arrays (otherwise known as dictionaries or hashes) by default. If you don't explicitly assign a key to a value, the interpreter will silently do that for you. So, the expression you've got up there iterates through $user_list, making the key available as $user and the value available as $pass as local variables in the body of the foreach.


I hope it doesn't get deprecated. While writing <? blah code ?> is fairly unnecessary and confusable with XHTML, <?= isn't, for obvious reasons. Unfortunately I don't use it, because short_open_tag seems to be disabled more and more.

Update: I do use <?= again now, because it is enabled by default with PHP 5.4.0. See http://php.net/manual/en/language.basic-syntax.phptags.php


=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass.

Example:

$user_list = array(
    'dave' => 'apassword',
    'steve' => 'secr3t'
);

foreach ($user_list as $user => $pass) {
    echo "{$user}'s pass is: {$pass}\n";
}
// Prints: 
// "dave's pass is: apassword"
// "steve's pass is: secr3t"

Note that this can be used for numerically indexed arrays too.

Example:

$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
    echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw

$user_list is an array of data which when looped through can be split into it's name and value.

In this case it's name is $user and it's value is $pass.


<?= $a ?> is the same as <? echo $a; ?>, just shorthand for convenience.


Code like "a => b" means, for an associative array (some languages, like Perl, if I remember correctly, call those "hash"), that 'a' is a key, and 'b' a value.

You might want to take a look at the documentations of, at least:

Here, you are having an array, called $user_list, and you will iterate over it, getting, for each line, the key of the line in $user, and the corresponding value in $pass.

For instance, this code:

$user_list = array(
    'user1' => 'password1',
    'user2' => 'password2',
);

foreach ($user_list as $user => $pass)
{
    var_dump("user = $user and password = $pass");
}

Will get you this output:

string 'user = user1 and password = password1' (length=37)
string 'user = user2 and password = password2' (length=37)

(I'm using var_dump to generate a nice output, that facilitates debuging; to get a normal output, you'd use echo)


"Equal or greater" is the other way arround: "greater or equals", which is written, in PHP, like this; ">="
The Same thing for most languages derived from C: C++, JAVA, PHP, ...


As a piece of advice: If you are just starting with PHP, you should definitely spend some time (maybe a couple of hours, maybe even half a day or even a whole day) going through some parts of the manual :-)
It'd help you much!


It's a shortcut for <?php echo $a; ?> if short_open_tags are enabled. Ref: http://php.net/manual/en/ini.core.php


An array in PHP is a map of keys to values:

$array = array();
$array["yellow"] = 3;
$array["green"] = 4;

If you want to do something with each key-value-pair in your array, you can use the foreach control structure:

foreach ($array as $key => $value)

The $array variable is the array you will be using. The $key and $value variables will contain a key-value-pair in every iteration of the foreach loop. In this example, they will first contain "yellow" and 3, then "green" and 4.

You can use an alternative notation if you don't care about the keys:

foreach ($array as $value)

Since it wouldn't add any value to repeat that it means echo, I thought you'd like to see what means in PHP exactly:

Array
(
    [0] => Array
        (
            [0] => 368 // T_OPEN_TAG_WITH_ECHO
            [1] => <?=
            [2] => 1
        )
    [1] => Array
        (
            [0] => 309 // T_VARIABLE
            [1] => $a
            [2] => 1
        )
    [2] => ; // UNKNOWN (because it is optional (ignored))
    [3] => Array
        (
            [0] => 369 // T_CLOSE_TAG
            [1] => ?>
            [2] => 1
        )
)

You can use this code to test it yourself:

$tokens = token_get_all('<?=$a;?>');
print_r($tokens);
foreach($tokens as $token){
    echo token_name((int) $token[0]), PHP_EOL;
}

From the List of Parser Tokens, here is what T_OPEN_TAG_WITH_ECHO links to.