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.
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
my php code doesn't work inside a li tag.
I tried several things but it doesn't work.
echo '<li $page_title=="Cart" ? "class="nav-item active"" : "">';
I don't understand why it doesn't work, can someone help me with this code?
You can't execute php code inside of single quotes, and you can't do ternary operators inside of double quotes or single quotes. Store it in another variable and run it that way.
$class = ($page_title=='Cart') ? "class='nav-item active'" : '';
echo "<li $class>";
https://3v4l.org/uhQAi
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 5 years ago.
I'm having some weird problem escaping " in an echo function.
echo "Site";
Any idea what I'm doing wrong?
You are doing it the javascript way for one. Concatenating in PHP works using . or ,.
Then you are using to many "
Try this line:
echo "Site";
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 8 years ago.
I'm tring to create an array of string but I can't do this if I use a variable.
$dir_photo="./Foto_NEW/";
$photo= array($dir_photo+"DSCN2507.JPG",$dir_photo+"IMG_0054.JPG",$dir_photo+"IMG_0058.JPG");
The result is 0 0 0.
+ is the concatenation operator in Javascript, but in PHP it's the period . So what you need is this:
$photo= array($dir_photo."DSCN2507.JPG",$dir_photo."IMG_0054.JPG",$dir_photo."IMG_0058.JPG");
You need to replace the + with a .
$dir_photo."DSCN2507.JPG"
+ is used in javascript, . is used in php
Try this:
$dir_photo="./Foto_NEW/";
$photo= array($dir_photo."DSCN2507.JPG",$dir_photo."IMG_0054.JPG",$dir_photo."IMG_0058.JPG");
You need to use . instead of + to concatenate strings.
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:
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