Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Listing Inventory Items Understanding Whitespace

There is a line of code I do not quite understand, please help!

echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';  
  1. Why single quotation inside double quotation?
  2. Why concatenation?

Thank you!

2 Answers

Saad Lulu
Saad Lulu
6,821 Points

We usually use single quotes for string that will not print out a value of a variable, and double quotes will render the value of a variable.

<?php
$string = 'hello world';
echo 'I said $string'; // I said $string
echo "I said $string" // I said hello world
?>

if you want to use single quotes but also get a value of a variable you can use concatination like so,

<?php
  $string = 'Hello world'
  echo 'I said' . $string; // I said Hello world
?>

sry, just found that I did not type the code properly, and it wasn't displayed.

vickiecomrie
vickiecomrie
3,248 Points

You can also use '' and "" interchangeably-- for example

<?php $string="vick"; echo 'I said $string'; echo "I said $string"; ?>

I said $stringI said vick