0

I declared a variable like this:

$variable = 'Hong Kong,Indonesia,India';

I want to add single quotes for each country. I want result like this:

$variable = "'Hong Kong','Indonesia','India'";

How can achieve this?

Al.G.
  • 3,929
  • 6
  • 32
  • 52
Haj Mohamed A
  • 83
  • 1
  • 8
  • 2
    Escape quotes with slash. Example: `$variable = 'Hong \'Kong\',\'Indonesia\',\'India\'';` – rokas Jan 27 '17 at 12:08
  • 2
    Possible duplicate of [Escape single quotes in string containing single and double quotes](http://stackoverflow.com/questions/38646653/escape-single-quotes-in-string-containing-single-and-double-quotes) – Faegy Jan 27 '17 at 12:39

3 Answers3

1

You can write this way -

$variable = "'Hong Kong','Indonesia','India'";
echo $variable;

Output - 'Hong Kong', 'Indoneisa','India'

Or you can use array rather than a single variable.

$country = array('Hong Kong','Indonesia','India');
echo $country[0];

Output - Hong Kong

CodeGuy
  • 753
  • 1
  • 8
  • 40
0

to add a single quote you have to write it like this

$variable = '\'Hong Kong\'';
Pravin Durugkar
  • 347
  • 2
  • 19
  • Wrong example. The output will be: `\'Hong Kong,Indonesia,India\'` – rokas Jan 27 '17 at 12:11
  • No - when using double quotes you don't have to escape single quotes...so either remove the slashes or surround the string in single quotes. [Read more here](http://php.net/manual/en/language.types.string.php) – Aaron W. Jan 27 '17 at 12:11
  • Still no - if you plan to use double quotes `$variable = "'Hong Kong'";`, if you need to use single quotes `$variable = '\'Hong Kong\'';` – Aaron W. Jan 27 '17 at 12:14
0

There are three ways (amongst many others) you could do this:

$variable1 = "'Hong Kong','Indonesia','India'";

$variable2 = '\'Hong Kong\',\'Indonesia\',\'India\'';

$variable3 = <<<HEREDOC
'Hong Kong','Indonesia','India'
HEREDOC;

I would say this first way is preferable, as this is the clearest. More information on these are found here: PHP Strings

Guillermo Phillips
  • 1,976
  • 1
  • 20
  • 35