Haj Mohamed  A
Haj Mohamed A

Reputation: 83

PHP variable add single quotes

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?

Upvotes: 0

Views: 3616

Answers (3)

Guillermo Phillips
Guillermo Phillips

Reputation: 2216

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

Upvotes: 0

Rahul
Rahul

Reputation: 726

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

Upvotes: 1

Pravin Durugkar
Pravin Durugkar

Reputation: 355

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

$variable = '\'Hong Kong\'';

Upvotes: 0

Related Questions