What is the right way to use quotation marks ?
echo 'string' . $variable . 'anotherstring';
or
echo "string" . $variable . "anotherstring";
the problem of course comes to light when we need to print, return, or echo the quotaion mrk itself …
$output = '<div class="' . $class . ' " style =" ' . $style . ' ">' ;
or
$output = "<div class=' " . $class . " ' style =' " . $style . " '>" ;
??
And what about when some jQuery or Javascript enters the game ??
function o99_print_front_script() {
return '
<script type="text/javascript">
var pluginDir = " '. PHP_CONSTANT_X .' " ;
var cluster_styles = [{
url:pluginDir +"/images/marker-o.png",
background: "#c6c6c6"
textColor: "#0f0"
}];
jQuery(document).ready(function() {
jQuery("#example").example_func({
elements:".div_1 .class_2",
infobox: true,
map_opt: {zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP},
infobox_s:s_infobox_k,
marker_icon:pluginDir +"/images/image_1.png"
});
});
</script>
' ;}
OR
function o99_print_front_script() {
return "
<script type='text/javascript'>
var pluginDir = ' ". PHP_CONSTANT_X ." ' ;
var cluster_styles = [{
url:pluginDir +'/images/marker-o.png',
background: '#c6c6c6'
textColor: '#0f0'
}];
jQuery(document).ready(function() {
jQuery('#example').example_func({
elements:'.div_1 .class_2',
infobox: true,
map_opt: {zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP},
infobox_s:s_infobox_k,
marker_icon:pluginDir +'/images/image_1.png'
});
});
</script>
" ;}
Is there some specification for this ? Some standard ? some “best practice” ?
Browsing through code you can see all kind of examples .
which one should we use for “first level” and which for “second” ?
And what if I have to “nest” 3 levels of those ??
this problem highlights itself when one uses some CMS , or system with “plugins” – and then wants to extend it with own code .the resulted HTML code in the DOM gets all confused, and you can not understand which quotes are which especially when JS is involved).
I am aware that all of them will work, but I want to know which one is right and which is wrong (if such terms exists in regarding this).
EDIT I
After reading comments / answers – I have realised that maybe I wrote the question in an unclear fashion. my question is regarding the Outputted xHTML /JS .
Of course that it has direct consequences on the PHP part , but mainly I was wondering what is the best practice to maintain a consistent and uniformal code throught the finished document while maintaining an easy syntax on the back-end..
Even looking at the source of this very page here (this stackexchange site) one can see some inconsistent behavior (at least to me it looks like it )
About markup nesting I would strongly suggest to adopt the
MVC patternso you will obtain a clear separation of the view (your html template) from the controller part (what you have to put in the template). In this way you can avoid to make a “soup” of unmantainable markup and php codeAnyway for the markup part I prefer writing like so
since PHP variables are parsed when included in a string with double-quote delimiters. For long code blocks like the javascript part another possibility is to use
HEREDOC syntaxso you can write php variables like thisSee also http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
hope this helps