Search code examples
phpquotesquote

PHP beginning programming, use of quotes


I just started studying PHP and I'm having some difficulty comprehending the use of single and double quotes.

The output I'm trying to get is: She said, "Hello, your IP address is 'localhost'"

Here's the code I have so far, with no erros:

<?php
echo 'She said, "Hello, your IP address is '  . $_SERVER['SERVER_NAME'];
?>

When trying to add the single quotes to the SERVER_NAME and the final double quote, I get errors.

Can you provide me with any further information to understand how to use and add single and double quotes for output?


Solution

  • You'll need to change to echoing with double quotes in order to use escaping. Then escape the double quotes you want to print with \"

    <?php
    echo "She said, \"Hello, your IP address is '" . $_SERVER['SERVER_NAME'] . "'\"";
    ?>