Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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.

 

Comments

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 :(

$around='around';
echo "What goes ". $around .",comes ". $around ." <br/>";
echo 'What goes '. $around .',comes '. $around .'<br/>';
$around="around";
echo "What goes ". $around .",comes ". $around ." <br/>";
echo 'What goes '. $around .',comes '. $around .'<br/>';

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";)

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:

$num = 1;
$around="around";
echo "what it looks like when variables are variables using the dot. ";
echo $num++ .' What goes ' . $around . ', comes ' . $around . '. ';
echo $num++ .' What goes ' . $around . ', comes ' . $around . '. ';

echo "what it looks like when variables become strings by putting them in full quotes. ";

echo "$num++ What goes $around comes $around ";
echo "$num++ What goes $around comes $around ";

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

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.

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.

In html file it was:

What goes around, comes around.

Then I can create a php file to output above as:


	<?php
	$around = "around";
	echo 'What goes ' . $around . ', ' . 'comes ' . $around . '.';
	?>

But when I see in "view script" button it was like.....

<?php   
 
$around = "around"; 
echo 'What goes ' . $around . ' comes ' . $around . '.';
 
?>

So my question is why there is no "comma(,)" between around and comes?

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.

hi

this has also the same output

$around ="around";
  
  printf("what goes %s comes %s .",$around,$around);

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.