This question already has answers here:
Curly braces in string in PHP
(4 answers)
Closed 6 years ago.
Sometimes you need to make clear to PHP what is actually the variable name. I have discovered that a colleague and I had been doing it slightly differently.
Say you had a variable $foo and wanted to output that with _constant_string appended
I had been using
return "<input type='hidden' name='${foo}_constant_string' value='true' />";
whereas my colleague is using
return "<input type='hidden' name='{$foo}_constant_string' value='true' />";
(slightly contrived example to simplify it).
My quick tests don't reveal an obvious difference, but I am curious: Is there a difference? Is there a reason to prefer one over the other?
Edit:
My example above used strings but my question was more general - I should have explicitly said so. I knew you could use curly braces for escaping, but hadn't found the specific point of if there was (in any situations) differences between the two ways of using them. I got the answer: there isn't for strings (which is what the "duplicate" post is about) but is for arrays and objects (thanks #dragoste).
It seems, there is no difference in any PHP version
$foo = 'test';
var_dump("$foo");
var_dump("{$foo}");
var_dump("${foo}");
Test: https://3v4l.org/vMO2D
Anyway I do prefer "{$foo}" since I think it's more readable and works in many other cases where other syntax doesn't.
As an example let's try with object property accessing:
var_dump("$foo->bar"); //syntax error
var_dump("{$foo->bar}"); // works great
var_dump("${foo->bar}"); //syntax error
The same case are arrays.
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
No, there is no differentce.
// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
Php manual
Answer on stackoverflow
Another way use it for variable:
$foo = 'test';
$test = 'foo';
var_dump("{${$foo}}"); //string(3) "foo"
Or for array:
$foo = ['foo','test'];
var_dump("{$foo[0]}"); //string(3) "foo"
var_dump("${foo[1]}"); //string(4) "test"
There is no difference between the following statement -
echo "This is {$great}";
echo "This is ${great}";
The output of both statements will be same.Please check following example-
$great = 'answer';
echo "This is {$great}"."\n";
echo "This is ${great}";
Output:-
This is answer
This is answer
Related
This question already has answers here:
Should I use curly brackets or concatenate variables within strings?
(3 answers)
Closed 9 years ago.
I often see programmers on Youtube concatenating like: .$example.
Small question, I would like to know what the difference is between .$name.
and "$name" because they give the same output.
<?php
$name = 'Todd';
echo "Hello $name!";
echo "Hello " .$name. "!";
?>
When you use variables directly in a string literal, it is hard to read. You (usually) lose the benefit of your IDE showing you with different colors what is what. You can see this in the StackOverflow formatting of the code in your question.
If you're just using echo, consider using a list of strings instead:
echo 'Hello ', $name, '!';
No concatenation is needed, and each string is copied to the output buffer. In theory this is faster, but for typical small strings you certainly won't notice any speed difference.
Yeah both produces the same output.
In the below example, Variables inside the double quotes are interpreted.
$name = 'Todd';
echo "Hello $name!"; // prints "Hello Todd!"
See the same example when you need to show the same using single quotes.
$name = 'Todd';
echo 'Hello $name!'; // prints "Hello $name!"
In the above case , the concatenate operator comes to rescue. So you can echo 'Hello '.$name;// prints "Hello Todd!"
The concatenate operator has its own specialities , i just explained it for your context.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parse error on explode('-','foo-bar')[0] (for instance)
In PHP there are functions that return an array, for example:
$a = parse_url("https://stackoverflow.com/q/9461027/87015");
echo $a["host"]; // stackoverflow.com
My question is how to combine the above two statements into a single statement. This (is something that works in JavaScript but) does not work:
echo parse_url("https://stackoverflow.com/q/9461027/87015")["host"];
Note: function array dereferencing is available since PHP 5.4; the above code works as-is.
You can do a little trick instead, write a simple function
function readArr($array, $index) {
return $array[$index];
}
Then use it like this
echo readArr(parse_url("http://stackoverflow.com/questions/9458303/how-can-i-change-the-color-white-from-a-uiimage-to-transparent"),"host");
Honestly, the best way of writing your above code is:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a["scheme"];
echo $a["host"];
Isn't that what I originally posted?
Yes. Depending on context you may want a better name than $a (perhaps $url), but that is the best way to write it. Adding a function is not an attractive option because when you revisit your code or when someone reads your code, they have to find the obscure function and figure out why on earth it exists. Leave it in the native language in this case.
Alternate code:
You can, however, combine the echo statements:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a['scheme'], $a['host'];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
curly braces in string
I still don't know what it's called. Like:
$name = 'xxx';
echo "This is a string {$name}";
What do you call that operation? Concatenating a variable using {} in to a string.
Thanks!
This is not concatenation ; this is variable interpolation ; see the Variable parsing section, in the PHP manual.
Basically, you can use any of the two following syntaxes :
echo "This is $variable";
Or :
echo "This is {$variable}";
And you'll get the same result in both cases -- except the second one allows for more complex expressions.
Concatenation would be something like this :
echo "This is my : " . $value;
Where the content of the variable $value is concatenated to the string, using the concatenation operator ..
It's often called string or variable interpolation.
How does {} affect a MySQL Query in PHP?
Don't let the question itself throw you - this answer gives you exactly what you are looking for.
And it's not concatenating; this is concatenating:
$myvar = "This is a string ".$name; // <<< Notice I'm concatenating the variable
// using the . operator
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How are echo and print different in PHP?
Is there any difference between ‘print’ and ‘echo’ in PHP?
What’s the difference of echo,print,print_r in PHP?
There are several ways to print output in PHP; including but not limited to:
echo 'Hello';
echo ('Hello');
print 'Hello';
print ('Hello');
Are there any differences between these four? Also, do the parentheses make any difference at all?
Two differences:
print has a return value (always 1), echo doesn't. Therefore print can be used as an expression.
echo accepts multiple arguments. So you may write echo $a, $b instead of echo $a . $b.
Concerning the parentheses: They are simply wrong in my eyes. They have no function at all. You could as well write echo (((((((((($a)))))))))); people usually include parentheses from ignorance, thinking that print is a function. Furthermore it increases the chance of misinterpretation. For example print("foo") && print("bar") does not print foobar, because PHP interprets this as print(("foo") && print("bar")). So bar1 would be printed, even though it looks different.
This question already has answers here:
What does $$ (dollar dollar or double dollar) mean in PHP?
(7 answers)
Closed 8 years ago.
what does two back to back $ behind a variable means. Like this
$$id
where can I find more information on that
Thanks
In PHP, $$ means you are about to inflict years of pain and suffering on at least one maintenance programmer. Note that you might wind up being that maintenance programmer.
It is a variable variable. Imagine this:
$quux = 'bar';
$foo[$quux] = "baz";
echo $foo['bar']; //prints baz
if there was no such thing as arrays, you might try something like this:
$quux = 'bar';
$$quux = "baz";
echo $bar; //prints baz
luckily we do have arrays so please don't use variable variables unless you are doing something convoluted and magical* and have no other choice.
*: Please don't do convoluted magical things, either.
These are called variable variables.
$foo = 'bar';
$id = 'foo';
echo $id; // prints foo
echo $$id; // prints bar
in the PHP manual of course
http://www.php.net/manual/en/language.variables.variable.php
note that it's obsolete and senseless syntax and you should always use arrays instead.