Hello. I’m still new to some basic formatting concepts. Something confused me in the book I was reading.
[B]Q:
when you have CSS style rules embedded in an PHP string, is the [COLOR=”#FF0000″]semicolon
[CODE]$name = $_GET[‘name’];
print “<p>Hello, <span style=”font-weight:bold[B][COLOR=”#FF0000″];[/COLOR][/B]”>$name</span>!</p>”;
I got confused because I know usually the semicolon surrounds the entire string, including any variable, and other tags that may include things such as the paragraph ..ect. The semicolon is always placed after the double quotes. However, in this case it seems that because there are [I]style rules
[canned-message]attachments-removed-during-migration
<i>
</i>"<p>Hello, <span style="font-weight:bold;">$name</span>!</p>"
[code=php]
print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>";
[/code]
The semi-colon that is part of the CSS statement (and that you highlighted in red) is, as far as PHP is concerned, just another character within the quoted string literal, and when output to the browser will be parsed by the HTML/CSS interpreter on the browser as having meaning in that context -- but PHP doesn't care what character it is:
<i>
</i>"<p>Hello, <span style="font-weight:bold[COLOR="#FF0000"][B];[/B][/COLOR]">$name</span>!</p>"
[/QUOTE]
[CODE]Hello, [B]Name![/B][/CODE]
The only characters in there that have any special meaning to PHP are the back-slashes used to escape the literal double-quotes, telling PHP that they are [i]not[/i] PHP string delimiters, just literal quote characters within that entire quoted string. The semi-colon that follows all of that (the very last character on the line) does matter to PHP, as in that case (not within a string literal), it is the PHP command separator.
Personally, I generally prefer to use single quotes withing the HTML part, so that I don't have to escape them (and the HTML parser is just as happy with them as the double quotes):
[code=php]
[/QUOTE]
print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>";
[/code]
[code=php]
<?php
$page = 'page.php';
$text = array('foo' => 'click me');
// only enter PHP mode as needed:
?>
<p><a href='/dir/<?php echo $page; ?>'><?php echo $text['foo'];?></a></p>
<?php
// single-quotes with concatenation:
echo '<p><a href="/foo/'.$page.'">'.$text['foo'].'</a></p>';
// double-quotes with concatenation:
// (note, doesn't matter which quote type you use for array keys)
echo "<p><a href='/foo/".$page."'>".$text["foo"]."</a></p>";
// double-quotes with interpolation and complex notation:
echo "<p><a href='/foo/$page'>{$text['foo']}</a></p>";
// and then there's printf(), where you use place-holders that are replaced by the args that follow:
printf(
"<p><a href='/foo/%s'>%s</a></p>",
$page,
$text['foo']
);
[/code]
[code=php]
$text = array('foo' => 'bar');
// this will generate a notice-level error and possible bug:
echo $text[foo];
// this will not be an error:
echo $text['foo']; /* or */ echo $text["foo"];
// however, this will be a parse error:
echo "<p>$text['foo']</p>";
// while PHP considers this to be just fine:
echo "<p>$text[foo]</p>";
[/code]
[code=php]
echo "<p>{$text['foo']}</p>";
// or
echo "<p>".$text['foo']."</p>";
[/code]
[CODE]print [COLOR="#008000"]"[/COLOR]<p>Hello, <span style="font-weight:bold[B][COLOR="#FF0000"];[/COLOR][/B][COLOR="#0000FF"]"[/COLOR]>$name</span>!</p>[COLOR="#008000"]"[/COLOR];[/CODE]
As noticed, this was how it was explained in my [U]PHP book[/U], However after looking in my XHTML book, I noticed a chapter talking about setting [I]local[/I] styles inside the document as a posed to linking to an external CSS.[/QUOTE]
[code=html]
<p style='background-color:#fff;border:solid 1px #333;color:#000;'>some text</p>
<p style='background-color:#fff;border:solid 1px #333;color:#000'>some text</p>
[/code]
[code=php]
<?php
echo "
<p style='background-color:#fff;border:solid 1px #333;color:#000;'>some text</p>
<p style='background-color:#fff;border:solid 1px #333;color:#000'>some text</p>
"; // <-- this is the only semi-colon in this code that PHP is "interested" in,
// since it is not part of the quoted string being echoed.
[/code]
[CODE]<[FONT=Arial]meta http-equiv=”content-type” content=”text/html;charset=”utf-8”[/FONT] />[/CODE]
I see, Makes sense tracknut, and NogDog. Thank you!
So the last semicolon really tells php to keep the HTML styles separate from the PHP. Makes sense.
...[/QUOTE]
[code=php]
<?php
echo ";;;;;;;;;;;;;;;;;;";
[/code]
[code=php]
<?php
echo "++++++++++++++";
echo "8888888888888888";
[/code]
[code=php]
<?php
echo "<script type='text/javascript'>alert('Hello, World!');</script>";
[/code]
[code=html]
<script type='text/javascript'>alert('Hello, World!');</script>
[/code]
everything between the double-quote characters in this example is of no particular meaning to the PHP parser/compiler running on the web server: it only gains any sort of meaning when a web browser that supports JavaScript receives that text and then parses and executes it within the browser, with the browser being totally oblivious to the fact that PHP was ever involved with it at all. All the browser would ever "see" from that PHP file's output is:
[COLOR="#0000CD"] <?php[/COLOR]
[COLOR="#008000"]echo[/COLOR] [COLOR="#B22222"]"<script type='text/javascript'>[/COLOR] alert('Hello, World!');[COLOR="#B22222"]</script>";[/COLOR] [/QUOTE]
[CODE][COLOR="#0000FF"]print "<p>Hello,[/COLOR] [COLOR="#FF8C00"]<span style="font-weight:bold[COLOR="#FF0000"][B];[/B][/COLOR]">$name</span>[/COLOR][COLOR="#0000FF"]!</p>";[/COLOR][/CODE]
0.1.9 — BETA 5.16