Escape Quotes in a PHP Variable

Hi Experts,

Thanks for reading. I'm looking for some advice. In regards to escaping quotes in a variable, is the following okay to do in place of the second code?

$variable = '<div class="box">User\'s Info</div>';

Open in new window


$variable = "<div class=\"box\">User's Info</div>";

Open in new window


Is the first code the best practice? Do you have a better suggestion? Any advice is appreciated.

Thanks!
LVL 1
SevenAteAnthonyAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
Neither is prefered, they're both ok.  The only exception to that is that you need double quotes if you want to embed variables in the string.  With single quotes, you need to use the '.' dot syntax to include variables.  Alternate methods are available so you can choose the one that works best for you.
0
 
SevenAteAnthonyAuthor Commented:
Thank you for the feedback, DaveBaldwin.
0
 
twohawksCommented:
Don't you need to use the dot either way?

<?php

$myvar1 = "linktosomeImage";

echo $myvar1 . " <br />";

$myvar2 = ' &nbsp;&nbsp;&rarr;&nbsp;<a href="'.$myvar1.'" rel="nofollow" title="a little tip here">Right-Click this Link to Save Image</a>';

echo $myvar2;

?>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Dave BaldwinFixer of ProblemsCommented:
0
 
SevenAteAnthonyAuthor Commented:
Thank you.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
All Courses

From novice to tech pro — start learning today.