chan yoonghon chan yoonghon - 2 months ago 8
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

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.

Example/Demo.

Comments