简体   繁体   中英

How do you show double quotes in single quotes PHP

I have a PHP echo statement:

echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address']. ",". $row['City']. "," . $row['State']. " 0". $row['ZipCode']. "," . $row['PhoneNumber']. ",". $row['Lattitude']. ",".$row['Longitude']. "]". ";<br>";  

which outputs:

stores[0] = [The Ale 'N 'Wich Pub , 246 Hamilton St ,New Brunswick,NJ 08901,732-745-9496 ,40.4964198,-74.4561079];

BUT I WOULD LIKE THE OUTPUT IN DOUBLE QUOTES SUCH AS:

stores[0]=["The Ale 'N 'Wich Pub", "246 Hamilton St, New Brunswick, NJ 08901", "732-745-9496 Specialty: Sport", "40.4964198", "-74.4561079"];

I Have looked at the PHP String Functions Manual on PHP site but still don't understand how i can implement it. Your help is appreciated.

The keyword you miss is "escaping" (see Wiki ). Simplest example:

echo "\"";

would output:

"

EDIT

Basic explanation is - if you want to put double quote in double quote terminated string you MUST escape it, otherwise you got the syntax error.

Example:

echo "foo"bar";
         ^
         +- this terminates your string at that position so remaining bar"
            causes syntax error. 

To avoid, you need to escape your double quote:

echo "foo\"bar";
         ^
         +- this means the NEXT character should be processed AS IS, w/o applying
            any special meaning to it, even if it normally has such. But now, it is
            stripped out of its power and it is just bare double quote.

So your (it's part of the string, but you should get the point and do the rest yourself):

 echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address'] .

should be:

 echo "stores[".$row['BarID']."] = [\"". $row['BarName'] . "\", \"" . $row['Address']. "\"

and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM