chan yoonghon chan yoonghon - 10 months ago 56
PHP Question

PHP include " and ' character into a String

I have a String in php variable. I want to add " and ' character before of the string variable and ' and " character after of my string variable. The following is my code but it fail to display what I wanted like "'String'"

$channel = """.$channel.""";
$channel = "\'".$channel."\'"
echo $channel

Answer Source

You're escaping them incorrectly (in the wrong order).

What you want is this:

$channel = '"\'' . $channel . '\'"';

Notice how we're escaping the ' ? That's because we are using it to supply the string -> '....'. We need to escape it otherwise it'd "end" the string there. Causing syntax errors.