-5

I have this: $animal=cat; how can I add single quotes to this variable to get an output of, for example, 'cat'?

echo "$animal";

//Output 'cat'
3141
  • 401
  • 1
  • 6
  • 23
user9154057
  • 1
  • 1
  • 1
  • 2
  • 3
    ... `echo "'$animal'";` ? – Federkun Dec 29 '17 at 20:00
  • Variables aren't interpreted in single-quoted strings. Use double quotes, like in the code you've shared. – ceejayoz Dec 29 '17 at 20:02
  • $animal = "'cat'"; will display with siingle quotes. echo $animal; – halojoy Dec 29 '17 at 20:04
  • 1
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – MonkeyZeus Dec 29 '17 at 20:09

3 Answers3

5
echo "'$animal'";

Just write signle quotes before and after variable but remember variable should be written in double quotes

Vinod Kumawat
  • 741
  • 5
  • 8
2

simply like this

echo "'". $animal. "'";
// output 'cat'
samehanwar
  • 3,280
  • 2
  • 23
  • 26
1

You can do

echo "\"$animal\"";

Or you can just use single quotes instead as the following:

echo '"$animal"';

Whichever you prefer.

ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51