PHP echo not producing correct result, turning string lowercase

I'm trying to echo a dynamic a tag which calls a javascript function, but the parameters are not being echoed correctly. They should retain their capitalization and not add spacing. Why is it doing this?

I've tried removing variables and just echoing a straight string with what I want, but it still displays incorrectly.

What I need:

echo '<a href="'.$info[0].'" onClick="redirTrackCalcBtn("'.$bname.'", "'.$info[0].'")"><img src="'.$info[1].'"/></a>'

Pure String Version:

echo '<a href="/calc" onclick="redirTrackCalcBtn("Test_Button_1", "/calc")"><img src="/images/calc-eng-desktop.png"></a>'


<a href="/calc" onclick="redirTrackCalcBtn(" test_button_1",="" "="" calc")"="">
    <img src="/images/calc-eng-desktop.png">

Should Output:

<a href="/calc" onclick="redirTrackCalcBtn("Test_Button_1", "/calc")">
    <img src="/images/calc-eng-desktop.png">

I also tried:

echo "<a href=\"".$info[0]."\" onClick=\"redirTrackCalcBtn(\"".$bname."\", \"".$info[0]."\")\"><img src=\"".$info[1]."\"/></a>";

But that still outputs:

<a href="/calc" onclick="redirTrackCalcBtn(" test_banner_1",="" "="" calc")"=""><img src="/images/calc.png"></a>

as per Dharman's response I also Tried:

echo '<a href="'.$info[0].'" 
    onClick=\"redirTrackCalcBtn("'.$bname.'", "'.$info[0].'")\"
    ><img src="'.$info[1].'"/></a>'

This outputs:

<a href="/calc" onclick="\&quot;redirTrackCalcBtn(&quot;Test_Banner_1&quot;," "="" calc")\"="">
<img src="/images/preguntanos-h-es.png">

Edit for context: It's for a dynamic banner within the content of a blog powered by WordPress.

3 answers

  • answered 2019-06-11 22:49 Dharman

    You need to escape one set of the double-quotes, otherwise they are mixed together. Since you went for single-quotes in PHP, you need to use double in HTML/JavaScript and then use single-quotes again, but this time escaped from PHP.

    echo '<a href="'.$info[0].'" onClick="redirTrackCalcBtn(\''.$bname.'\', \''.$info[0].'\')" ><img src="'.$info[1].'"/></a>';

    The JavaScript variables are enclosed within \'


    echo '<a href="'.$info[0].'" onClick=\'redirTrackCalcBtn("'.$bname.'", "'.$info[0].'")\' ><img src="'.$info[1].'"/></a>';

    The onlick part is now enclosed with escaped quotes, everything else stayed the same.

    You have 3 languages mixed together, 3 layers:

    PHP will use '
    -->HTML will use "
    ---->JavaScript will use \'

    Each one uses double or single quotes and you only have two to choose from. Therefore you need to escape one of them.

    A simpler example:

    echo '<a onclick="alert(\'hi\')">Hello</a>';

  • answered 2019-06-12 00:54 Tim Morton

    Perhaps a simpler way to overcome quote escaping confusion is to assign the string in a different way. You can remove one layer of quotation by using heredoc notation.

    as an aside, your "correct" output is not correct:
    onclick="redirTrackCalBtn("Test_Button_1, "/calc")">

    <a href="/calc" onclick="redirTrackCalcBtn("Test_Button_1", "/calc")">
      <img src="/images/calc-eng-desktop.png">

    Your HTML should look like this:

    <a href="/calc" onclick="redirTrackCalcBtn('Test_Button_1', '/calc')">
      <img src="/images/calc-eng-desktop.png">

    Using Heredoc notation, you don't have to concatenate and escape, just write it out the way the HTML should be:

    <a href="{$info[0]}" onclick="redirTrackCalcBtn('{$bname}', '{$info[0]}')">
      <img src="/images/calc-eng-desktop.png">
    echo $link;

  • answered 2019-06-12 01:07 asiby

    You can simplify your expressions using the following technic...

    1. HTML accepts single quote or double quotes for attributes.
    2. PHP can evaluate variables inside of double quote delimited strings. This can make your expressions much more easier to understand.

    So based on this, the answer would be:

    echo "<a href='{$info[0]}' onClick='redirTrackCalcBtn(\"{$bname}\", \"{$info[0]}\")'><img src='{$info[1]}'/></a>";

    This will give the following result ...

    <a href='/calc' onClick='redirTrackCalcBtn("test_button_1", "/calc")'><img src='/images/calc-eng-desktop.png'/></a>

    In your question, you have shown an Pure String Version and what you thought was a normal output. Both of those outputs are wrong. You cannot use something like onclick="redirTrackCalcBtn("Test_Button_1", "/calc")" because the double quote right after the opening parenthesis finishes the onclick attribute which become onclick="redirTrackCalcBtn(". After that, the browser will try its best to find the following attributes and their values. So the spaces that you are seeing are just the natural space between attributes.

    In conclusion, there is nothing wrong with echo.