2

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
CoPoPHP
  • 75
  • 9
  • You have to escape the single quotes: `echo 'She said, "Hello, your IP address is \'' . $_SERVER['SERVER_NAME'] . '\'"';` – insertusernamehere Sep 05 '13 at 17:17
  • You can use `echo 'She said, "Hello, your IP address is ' . "'" . $_SERVER['SERVER_NAME'] . "'\"";` – Funk Forty Niner Sep 05 '13 at 17:21
  • possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – insertusernamehere Sep 05 '13 at 17:25

6 Answers6

2

If you are new to PHP, I recommend you take a look at the documentation for Strings to avoid potential pitfalls in your future projects.

Justin Pearce
  • 4,994
  • 2
  • 24
  • 37
1

Quotes must be paired, so you need to keep track of where one type of quotes ENDS and another BEGINS.

Example:

 John wrote: "She said 'Hello' to me"

Similarly, when you are inserting data within a quoted string:

 John wrote: "She said '" .varname. "Hello' to me"

or, in your initial example:

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

Do you see how we first closed the outside quote, added the external var, then re-opened the outside quote in order to continue the quoted text? In your example, all that was left to add was a 'string' containing the closing quotation mark (also a character): ' " '

A good idea is to use an editor that helps with that. I recommend Notepad++. Not only does it have the syntax highlighting to help you keep track of paired questionmarks, paretheses, brackets, etc, but it also has an incredible FTP tool that immediately FTPs changed files up to your server when you save them.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Good answer. However I'm not sure about that FTP tool - developers should be encouraged to develop on a local machine, and deploy in chunks when they are ready to test. Automatic FTPing isn't a deployment strategy, it's just encouraging remote development, which (imo) is not best practice. However, happy to hear a defence of it! – halfer Sep 05 '13 at 21:01
  • Thanks for the poscom. Perhaps I am fortunate. My employer (for another $70/year) purchased a cheap domain + hosting account and I develop live on that. Eliminates dealing with the band-aid workarounds of XAMPP/WAMP with PHP/AJAX/etc. When changes fully tested, I do a full backup and scheduled roll-out of the new code. – cssyphus Sep 05 '13 at 21:07
  • "poscom" - not familiar with that word. However, I'd be inclined to recommend that developing on your machine is not a "band-aid workaround" - it's the primary, recommended way to developing web software. To be honest, there are times where I develop remotely on a branch server (where setting up a local environment for a particular thing is awkward), but I don't recommend it. If you are deploying via FTP however, I suggest you try deploying from your version control system onto a virtual server you have full control over - it's much quicker, and rollbacks are a great deal easier. – halfer Sep 05 '13 at 21:30
1

This way will work:

<?php
echo 'She said, "Hello, your IP address is '  . $_SERVER['SERVER_NAME'] . '"';
?>
Wagner
  • 188
  • 1
  • 11
0

You can also do it this way:

echo "She said, \"Hello, your IP address is {$_SERVER[SERVER_NAME]}\"";

I've escaped the " marks where they are meant literally, and wrapped the array reference in braces. Note that inside a string, associative array lookups don't need quoting, but outside of a string, they do.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

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'] . "'\"";
?>
Jerbot
  • 1,168
  • 7
  • 18
  • Thank you so much! This is very helpful. I'm going to further study how to escape the quotes. – CoPoPHP Sep 05 '13 at 17:23
  • You're the only one that got it right, complete with OP's single quotes around server name. Alternatively, I was going to put in as per my original comment `echo 'She said, "Hello, your IP address is ' . "'" . $_SERVER['SERVER_NAME'] . "'\"";` which would have produced the same thing. – Funk Forty Niner Sep 05 '13 at 17:28
0

In PHP variable can't run in single quotes, it will be treated as a string.

For e.g:

$a = "Hello World";

echo "$a";

Output:

Hello world   

echo '$a'; 

Output:

$a
halfer
  • 19,824
  • 17
  • 99
  • 186
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23