-2

I don't understand the function of the two backslashes on the following line...

echo "<p style=\"font-family: $font; font-size: {$size}em;\">Hello, world!</p>";

of this code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Saying hello with style</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
    </head>
    <body>
        <h1>Saying hello with style</h1>

<?php

function helloWithStyle( $font, $size ) {
    echo "<p style=\"font-family: $font; font-size: {$size}em;\">Hello, world!</p>";
}

helloWithStyle( "Helvetica", 2 );
helloWithStyle( "Times", 3 );
helloWithStyle( "Courier", 1.5 );

?>

    </body>
</html>

Removing the two backslashes causes a "expecting , or ;" error on the code line pasted above. Why is this?

Thank you

Aaron
  • 1,956
  • 5
  • 34
  • 56
Preston blah
  • 229
  • 1
  • 3
  • 7
  • This isn't too narrow at all. The answer would provide a general concept that can be a mystery... when and what do backlashes do in urls when passing them. – curls Oct 02 '15 at 12:23

4 Answers4

2

A backslash \ is an escape character, allowing for the string to output without errors.

If you start a string with double quotes ", the next time you use double quotes, it will effectively end the string. The same applies for a single quote ' to start a string.

If you start a string with double quotes " you can use single quotes ' within the string without escaping the quotes, and vice-versa.

Using your snippet as an example, these are all equivalent:

echo "<p style=\"font-family: $font; font-size: {$size}em;\">Hello, world!</p>";

echo '<p style="font-family: $font; font-size: {$size}em;">Hello, world!</p>';

echo "<p style='font-family: $font; font-size: {$size}em;'>Hello, world!</p>";

PHP: Strings Documentation

Aaron
  • 1,956
  • 5
  • 34
  • 56
  • Why are the quotes (whether single or double) around the inline style components necessary? – Preston blah Mar 14 '13 at 21:45
  • @Prestonblah Because they are? You need them in HTML, therefore you need them in PHP that outputs HTML. – Niet the Dark Absol Mar 14 '13 at 21:48
  • @Kolink Constants are not looked for in strings. Thus, the code will not be interpreted to contain constants which might otherwise have returned an error. This is one reason why, in addition to the fact of what you said. – Preston blah Mar 14 '13 at 22:41
1

They escape the quotes so they are not counted as actual quotes - otherwise they mark the end of the string and the rest of the HTML is treated like it were PHP code, which obviously doesn't work.

See the documentation

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Because the " mark marks the start and end of a string. If you want to include a quote mark within the string, you must escape it using the backslash character, otherwise it is treated as the end of a string, and the content that follows doesn't make sense in a coding context hence the error.

Chris K
  • 435
  • 2
  • 10
0

It's because you're only using double quotes. \ is an escape character that will allow php to print the " without assuming it's the end of the quote. A better way to write that line might be

  echo '<p style="font-family: $font; font-size: {$size}em;">Hello, world!</p>';
zajd
  • 761
  • 1
  • 5
  • 18