Why concatenate? [duplicate] - php

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.

Related

PHP: Is there a difference between {$foo} and ${foo} [duplicate]

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

PHP variable interpolation vs concatenation [duplicate]

This question already has answers here:
Should I use curly brackets or concatenate variables within strings?
(3 answers)
Closed 6 years ago.
What is the difference between the two following methods (performance, readability, etc.) and what do you prefer?
echo "Welcome {$name}s!"
vs.
echo "Welcome " . $name . "!";
Whatever works best for you works...
But if you want to go for speed use this:
echo 'Welcome ', $name, '!';
The single quotes tell PHP that no interpretation is needed, and the comma tells PHP to just echo the string, no concatenation needed.

Insert a function result in the middle of PHP heredoc [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling PHP functions within HEREDOC strings
I need to insert the results of functions into the middle of heredoc..
$r = func1();
$s = func2();
echo <<<EOT
line1
$r
$s
line3
EOT;
I am not sure if any better way to write it as I need to evaluate all the functions beforehand which make the code stupid.
Better if have something like..
echo <<<EOT
line1
{func1()}
{func2()}
line3
EOT;
Of course the above code does not work, but just want to show my idea...
No, there is no better way to do it.
Heredoc string syntax behaves much like double quote string syntax. You can't put a function name as an expression inside of the double quoted string any more than you can a heredoc. So what you're doing is fine and that's how you should be doing it. The only time you can get interpolation inside of string syntax in PHP is if the value is a variable.
For example...
$foo = function() { return 'value'; };
echo <<<SOMEHEREDOC
{$foo()}
SOMEHEREDOC;
// Is the same as
echo "{$foo()}";
// Is the ssame as
$foo = 'strtoupper';
echo "{$foo('hello world')}";
The above code would output
value
value
HELLO WORLD

PHP Concatenation using {} [duplicate]

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

Differences between echo, echo(), print and print() in PHP [duplicate]

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.

Categories