Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${ } mean in PHP syntax?

Tags:

syntax

php

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

like image 878
murvinlai Avatar asked Apr 06 '11 19:04

murvinlai


People also ask

What does this symbol mean in PHP?

This operator allows for simpler three-way comparison between left-hand and right-hand operands. The operator results in an integer expression of: 0 when both operands are equal. Less than 0 when the left-hand operand is less than the right-hand operand.

What does &$ mean in PHP?

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this ...

What is PHP syntax code?

Basic PHP Syntax A PHP script can be placed anywhere in the document. The default file extension for PHP files is " . php ". A PHP file normally contains HTML tags, and some PHP scripting code.

What does => mean in PHP array?

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.


2 Answers

${ } (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. 
like image 60
celiker Avatar answered Sep 21 '22 20:09

celiker


It's an embedded variable, so it knows where to stop looking for the end of the variable identifier.

${username} in a string means $username outside of a string. That way, it doesn't think $u is the variable identifier.

It's useful in cases like the URL that you gave, because then it doesn't need a space after the identifier.

See the php.net section about it.

like image 26
Cyclone Avatar answered Sep 18 '22 20:09

Cyclone