PHP Beginnings Ex. #6: Concatenation of Strings
For this PHP exercise, write a script using the following variable:
$around="around";
Single quotes and double quotes don't work the same way in PHP. Using single quotes (' ') and the concatenation operator, echo the following to the browser, using the variable you created:
What goes around comes around.
 
 
Here's the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>Concatenation of Strings</title>
</head>
<body>
<?php
$around = "around";
echo 'What goes ' . $around . ' comes ' . $around . '.';
?>
</body>
</html>
The point of this exercise is to use the period (".") concatenation operator. Using double quotes (" "), you could have included the variables in you string, and PHP would print them as intended. If your output doesn't look right, be sure you have included the appropriate spaces inside the quotes.
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
Comments
doubt
Its mentioned Single quotes and double quotes don't work the same way in PHP, what is the difference??
I get the same output with the following, so confused as to how they work differently :(
Answer
I know its a bit late in regards of answering this question, but if anyone else is interested.
Its not that you will get different results by using:
echo "What goes ". $around .",comes ". $around ." ";
vs
echo 'What goes '. $around .',comes '. $around .'';
It is that you will get different results if you wrap $around in double or single quotes
For example, when using single quotes, you cannot use the echo statement to output a variable(echo '$around';). It will simply output $around.
While using double quotes, you can output, via echo, variables(echo "$around";)
an example
And thus variable can still act as a variable when not in quotes and becomes a simple string when in the quotes. Try this version of the exercise as an illustration:
Out put will look like this:
what it looks like when variables are variables using the dot.
1 What goes around, comes around.
2 What goes around, comes around.
what it looks like when variables become strings by putting them in full quotes.
3++ What goes around comes around
3++ What goes around comes around
Called interpolation
Basically a variable inside a double quote is replaced or interpolated with the value of the variable. Single quotes, on the other hand, treat variables as literals and they are not replaced with the values. Otherwise single and double quotes behave the same way for straight strings where variables are not involved.
actually
They do work differently!
when using single quotes(' ') the code looks like this:
$around="around";
echo 'what goes '.$around.', comes '.$around.';
but when you use double quotes (" ") you can do it like this:
$around="around";
echo "what goes $around, comes $around";
theres no need to quote the variable.
Question
In html file it was:
What goes around, comes around.
Then I can create a php file to output above as:
But when I see in "view script" button it was like.....
So my question is why there is no "comma(,)" between around and comes?
Good catch!
You are absolutely right. As it was written, your code better fulfills the instructions. Really, the phrase doesn't need the comma, so we've taken it out.
answer
hi
this has also the same output
One more thing to consider
The double / single quote also effects processing time. You do not notice it when you only have a few lines of text, but when it gets into the 100's it makes a difference.
With double quotes, there is no need for concatenation is not necessary. In that case, the content between the double quotes are searched. Which adds processing time.
With single quotes, only the concatenated areas or searched because it denotes that there is a variable.
I remember that from a video tutorial I watched on thephpbaics dot com.