Viewed   57 times

I have used PHP for a long time, but I just saw something like,

${  } 

To be precise, I saw this in a PHP Mongo page:

$m = new Mongo("mongodb://${username}:${password}@host");

So, what does ${ } do? It is quite hard to search with Google or in the PHP documentation for characters like $, { and }.

 Answers

5

${ } (dollar sign curly bracket) is known as Simple syntax.

It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.

<?php
$juice = "apple";

echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s.";
?>

The above example will output:

He drank some apple juice.
He drank some juice made of .
He drank some juice made of apples.
Thursday, October 13, 2022
2

It passes by reference. Meaning that it won't create a copy of the value passed.

See: http://php.net/manual/en/language.references.php (See Adam's Answer)

Usually, if you pass something like this:

$a = 5;
$b = $a;
$b = 3;

echo $a; // 5
echo $b; // 3

The original variable ($a) won't be modified if you change the second variable ($b) . If you pass by reference:

$a = 5;
$b =& $a;
$b = 3;

echo $a; // 3
echo $b; // 3

The original is changed as well.

Which is useless when passing around objects, because they will be passed by reference by default.

Friday, August 26, 2022
 
4

50 in binary is 11 0010, shift right by 4 yields 11 which is equal to 3.

See PHP documentation and Wikipedia.

Friday, September 23, 2022
5

It's a shorthand for <?php echo $a; ?>.

It's enabled by default since 5.4 regardless of php.ini settings.

Wednesday, October 5, 2022
 
alex_a.
 
5

=> 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
Sunday, November 6, 2022
2

It's a bitwise operator.

Example:

"hallo" ^ "hello"

It outputs the ASCII values #0 #4 #0 #0 #0 ('a' ^ 'e' = #4).

Monday, August 1, 2022
 
5

It's called an Alternative Syntax For Control Structures. You should have an endwhile; somewhere after that. Basically, it allows you to omit braces {} from a while to make it look "prettier"...

As far as your edit, it's called the Ternary Operator (it's the third section). Basically it's an assignment shorthand.

$foo = $first ? $second : $third;

is the same as saying (Just shorter):

if ($first) {
    $foo = $second;
} else {
    $foo = $third;
}
Saturday, October 8, 2022
1

global is a keyword that should be used by itself. It must not be combined with an assignment. So, chop it:

global $x;
$x = 42;

Also, as Zenham mentions, global is used inside functions, to access variables in an outer scope. So the use of global as it is presented makes little sense.

Another tip (though it will not really help you with syntax errors): add the following line to the top of the main file, to help debugging (documentation):

error_reporting(E_ALL);
Friday, August 19, 2022
3

That style of syntax is more commonly used when embedding in HTML, especially for template/display logic. When embedded this way, it's a little easier to read than the curly braces syntax.

<div>
<? if ($condition): ?>
  <ul>
    <? foreach($foo as $bar): ?>
        <li><?= $bar ?></li>
    <? endforeach ?>
  </ul>
<? endif ?>
</div>

Versus:

<div>
<? if ($condition) { ?>
  <ul>
    <? foreach($foo as $bar) { ?>
      <li><?= $bar ?></li>
    <? } ?>
  </ul>
<? } ?>

The verbose end tags make it a little easier to keep track of nested code blocks, although it's still mostly personal preference.

Friday, November 4, 2022
 
1

http://www.ietf.org/rfc/rfc1738.txt

3.3. HTTP

An HTTP URL takes the form:

  http://<host>:<port>/<path>?<searchpart>

...

   Within the <path> and <searchpart> components, "/", ";", "?" are
   reserved.  The "/" character may be used within HTTP to designate a
   hierarchical structure.

So as others mentioned it is only a reserved separator there.

This document updates the above (as DaveRandom mentions):

http://tools.ietf.org/html/rfc3986#section-3.4

But I think the point is the same, and it is harder to quote from this newer version. :)

Friday, September 30, 2022
 
kermit
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :