-1

I have a little question regarding the use of double quote and single quote.

When I use the code below with double quotes echo "":

$result_entries = mysqli_query($con,"SELECT * FROM bhost_entries where author_u_id='$user_info[u_id]'");
while($entries = $result_entries->fetch_object())
  {
  echo "
  <tr>
   <td>
   {$entries->title}
   </td>
  </tr>
  ";
  }

..output will be

Entry1
Entry2
Entry3
etc..

But when I use the same code with single quote echo '' I'll get:

{$entries->title}
{$entries->title}
{$entries->title}
etc..

as output.

Why do they behave differently?

Appreciate any thoughts.

Lwoj Dode
  • 13
  • 1

2 Answers2

4

This is because it is the main difference between single and double qoutes. Single qoutes does not parse variables while double qoutes parse variables.

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Thank you for the short explanation. So, in other words should I use double quotes or single quotes for my output? Or what do you suggest? – Lwoj Dode Apr 18 '13 at 16:02
  • Read http://php.net/manual/en/language.types.string.php and use whatever you need. There is no right and wrong. – str Apr 18 '13 at 16:03
  • 1
    You must use double qoutes if you want to put your variables in qoutes and it has to be parsed or else you can use single qoutes. – chandresh_cool Apr 18 '13 at 16:07
0

If you want to use single quotes you need to do this:

  echo '
  <tr>
   <td>
   '.$entries->title.'
   </td>
  </tr>
  ';

if you want to use double quotes you can stick with your original syntax! As stated before, double quotes allow php variable to be parsed with correct syntax while single quoted strings do not. Keep this in mind when you may have to escape single or double quotes within a php string.

echo 'Arnold once said: "I\'ll be back"';

from PHP Manual

Dan
  • 3,755
  • 4
  • 27
  • 38