String creation problems!


Sorry for the poor subject title! I have know idea what to class this as.

I am trying to create a link in php and I am really struggling.

This is what I should end up with

<a onclick="return bookWebinar(385875262,"") href="#"><img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" /></a>

However when looking at the source code of the generated html this is what I am ending up with

<a href="#"")"="" onclick="return bookWebinar(385875262,">
<img border="0" width="131" height="45" alt="Book Now" src="my_content/images/book.jpg">

This is the php I am using to generate this.

$urltodirect = urlencode("");
$prams = $webinarKey .",".'"'.$urltodirect.'"';
$button = '<a onclick="return bookWebinar('.$prams.')" href="#"><img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" /></a>';

If I drop the $urltodirect from the php then it works just fine and creates the button/link and it works.

What am I missing please!


You are trying to output html and js using PHP.

To avoid the nightmare of which quote goes where, try using PHPs heredoc syntax to interpolate PHP variables into your string:

Start off with:

$link = <<<EOL



echo $link;

To make sure you understand that that last heredoc delimeting line EOL; HAS to be the first thing on that line …

Then go to putting your target string in heredoc, make sure that works… (added some hard returns for ease of reading)

$link = <<<EOL

<a onclick="return bookWebinar(385875262,'http//')" href="#">
<img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" />


echo $link;

And then finally start adding your REAL variables back in to the string, do it one at a time and test on each iteration:

$urltoredirect = "";
$link = <<<EOL

<a onclick="return bookWebinar(385875262,'{$urltoredirect}')" href="#">
<img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" />


echo $link;

You can optionally use the {} parenthesis to delineate the variables if you wish, as I have done.

That way you can mix single and double quotes so that your output is valid html/js without worrying about PHP string concatenation quote requirements mucking the water up.

HTH restore some sanity!

Thanks very much Cups.

I think the forum added some white space to the end of the examples as that threw me for a little while!

Never mind sanity now restored and I can get this 5 minute job finished that I started 3 hours ago!!!


We’ve all been there …

Eventually the “am I going mad?” feeling becomes more familiar, and you even get used to it for a while!

Then after many code cycles of paring code back and back again to find really silly errors that you should eventually have a light bulb moment and realise you should start at the bottom and get into the habit of writing a bit, testing it, forcing PHP to prove what you think SHOULD happen (or should NOT happen in many cases) then carrying on.

Extreme cases cause you to arrive at the door of Test Driven Development. :wink:

Good tricks to learn :

use var_dump() on all your variables, especially POST/GET and use echo to in order to inspect what is going on.
turn on error reporting on you development server

When working with JS and PHP :

inspect the html source code of your page
use heredoc syntax in PHP to assemble the code
get the JS working FIRST with hard-coded values, then replace the values with variables
check out the IDEs which support JS validation/markup

Mixing PHP/JS can be a real nightmare though - it compounds the possibilities for mistakes.