so I have this PHP function which returns html/js. But I find the method I am using is wrong and not efficient. Is there a better way?
Here is just a simplified version of the code.
function doSomething() {
$speed = 1000;
$duration = 500;
$start = false; // this is a boolean and doesn't work below (not sure why)
$output = '<div class="container">something</div>' . "\r\n";
$output .= '<script type="text/javascript">' . "\r\n";
$output .= 'jQuery(document).ready(function() {' . "\r\n";
$output .= 'jQuery(".container.").cycle({' . "\r\n";
$output .= 'speed : ' . $speed . ',' . "\r\n";
$output .= 'duration : ' . $duration . ',' . "\r\n";
$output .= 'start : ' . $start . "\r\n"; // this doesn't work I think is because of it becoming a string instead of a boolean here.
$output .= '})' . "\r\n";
$output .= '})' . "\r\n";
$output .= '</script> . "\r\n";
return $output;
}
So as you can see above a bunch of output and bunch of linebreaks and basically very hard to maintain and debug. In addition, the START variable isn’t working per the comment.
There has to be a better way. I thought about heredocs? But not sure…
Thanks for looking.
You could do something like this:
And with a combination of
json_encode()and heredoc syntax:Another option, still, could be:
etc…