Japar S Japar S - 7 months ago 15
PHP Question

Semicolon in echo php

i have code like this :

<?php
echo '<a onclick="function(''.$some.'');">';
?>


how can it return html like this :

<a onclick="function('some');">


Big Thanks

Answer

Since you quoted the string with single quotes, you have to escape them inside the string.

Simply add a backslash \ before each single quote inside the string:

<?php
    echo '<a onclick="function(\''.$some.'\');"';
?>

Also you can do that in a few other ways like:

echo "<a onclick=\"function('{$some}');\""; 

Double quotes lets you simply embed a variable instead of concating, but now you have to escape double quotes.

Comments