/    Sign up×
Community /Pin to ProfileBookmark

Is the semicolon in the correct place?

Hello. I’m still new to some basic formatting concepts. Something confused me in the book I was reading.

[B]Q:[/B][ATTACH]15531[/ATTACH]
when you have CSS style rules embedded in an PHP string, is the [COLOR=”#FF0000″]semicolon[/COLOR] (that ends the statement after the font formatting) always placed before the double quote? Example in the second line of code:

[CODE]$name = $_GET[‘name’];

print “<p>Hello, <span style=”font-weight:bold[B][COLOR=”#FF0000″];[/COLOR][/B]”>$name</span>!</p>”;
[/CODE]

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[/I] in the sting, the author of the book is placing the semicolon before the double quotes. Is this correct or a typo? The last semicolon surrounding the entire string “is” actually placed after the double quotes which I know to be correct, but this is not the case for the semicolon towards the center of the string.

[canned-message]attachments-removed-during-migration[/canned-message]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 11.2013 — 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>"&lt;p&gt;Hello, &lt;span style="font-weight:bold;"&gt;$name&lt;/span&gt;!&lt;/p&gt;"

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]
print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>";
[/code]
Copy linkTweet thisAlerts:
@waveformauthorMay 12.2013 — 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>"&lt;p&gt;Hello, &lt;span style="font-weight:bold[COLOR="#FF0000"][B];[/B][/COLOR]"&gt;$name&lt;/span&gt;!&lt;/p&gt;"

[/QUOTE]

I'm not sure what you mean as far as [I]having meaning in the context[/I]? The semicolon I highlighted in red is not displayed when the form is submitted. The output shows as this:
[CODE]Hello, [B]Name![/B][/CODE]
The semicolon I highlighted in red as you can see is not being escaped, only the double quotes are which are to be taken literal. I guess what I'm asking is; what is the point of having the semicolon in the HTML part if it doesn't show up in the output. I'm really asking, what it's function is for being there?

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]
print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>";
[/code]
[/QUOTE]


I understand how the escaping of characters work just to be clear on that.

With regard to what you said about your preference on using single quotes. From what I understand.. you can use either for simple variables. However, when using Arrays which use single quotes around the Index/key when you're using a variable(s) in a string, you have to use double quotes because the index/key in the array is [I]already[/I] sounded by single quotes. The book I have then talks about an issue with a phrase error this will present, but gives a solution to the issue by assigning a $_POST element to a variable to simplify it and avoid the phrase error. [I]See the right column in attachment 2[/I]

[canned-message]attachments-removed-during-migration[/canned-message]
Copy linkTweet thisAlerts:
@NogDogMay 12.2013 — Even if it does not all sink in immediately, it's a good idea to read through the PHP manual section on the String type: http://www.php.net/manual/en/language.types.string.php (and read it again several times as weeks and months go by to reinforce it and to pick up bits you missed the first time ? ).

The good news is that PHP provides multiple ways to define string literals, giving you quite a bit of flexibility.

The bad news is that PHP provides multiple ways to define string literals, giving you quite a few reasons to get confused or make mistakes.

Let's take a look at some of the many options to output a line of HTML code that includes a couple variable values in it, one being a simple scalar variable and one an array member. The main thing to remember (which I think you already know) is that variables are not interpolated within single-quoted strings, so you would have to use concatenation in that case. You can also use concatenation within double quotes if you like, but it's not required. However, array elements get a picky about how you use them if you want to use interpolation within a double quoted string, so I find it's best to either use concatenation or "complex" (curly brace) notation with them. And another option is to write your output while [i]not[/i] in PHP mode, only entering that mode when needed.

[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]

The ugliest part (to me) is the handling of array elements.
[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]

:eek:

Therefore, I prefer to always quote my array indexes and then make use of either complex notation or concatenation if using them within a double-quote string, so that my array key quoting is consistent throughout:
[code=php]
echo "<p>{$text['foo']}</p>";
// or
echo "<p>".$text['foo']."</p>";
[/code]

Did any of that help? :rolleyes:

For extra credit, read up on the HEREDOC and NOWDOC quoting techniques. ?
Copy linkTweet thisAlerts:
@waveformauthorMay 12.2013 — Thanks NogDog for the links and the detailed examples.

You're right, the versatility really does make it complicated for students. Us new guys are looking for some kind of structure to grab onto. I guess you just have to take it all in little by little, and keep reviewing.

I'll have to read through what you posted in depth, so I won't respond to it at the moment. Thank you again!

I did want to mention something with regard to what I was asking last night about why there was a [COLOR="#FF0000"]semi colon[/COLOR] placed "before" the [COLOR="#0000FF"]double quotes[/COLOR] for the styling part of the string ...while the end part of the main string the "[I][COLOR="#008000"]main outer quotes[/COLOR][/I]" were placed [I]before[/I] the last semicolon.

[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. Anyway, I noticed that their example also puts the semicolon before the double quotes for local styles, so I guess this carry's over to PHP as well. [I]See the [B]order[/B] in the attached image[/I].

I just needed something solid as to why the semi color was before the double quotes. And apparently the semicolon is the terminator for the style put of the string, while the outer one terminates the main string.

By the way, thanks for the response and help NogDog.

The rules can really get tangled in my head sometimes.

[canned-message]attachments-removed-during-migration[/canned-message]
Copy linkTweet thisAlerts:
@tracknutMay 12.2013 — 
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]


FWIW, that particular semi-colon at the end of the local style, or even at the end of a style in an external stylesheet, is optional and extraneous. It seems to be a rather common appendage added by many coders, so apparently it's commonly taught that way. But there are those of us (possibly a small group of underground rebels) that see it as a useless waste of bandwidth and certainly "ugly" when it appears in the context you're using it, if that counts for anything.

Dave
Copy linkTweet thisAlerts:
@NogDogMay 12.2013 — Ah, okay, that semi-colon is part of the style attribute in that HTML tag. It is the separator between CSS element/value descriptors, essentially the same as the semi-colon's use in PHP. It is not required after the last element:value pair, so could be omitted in this case; but many coders prefer to leave it in both for consistency and to avoid simple errors if they decide to append another element. Either of these is valid in HTML:
[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]

But as the semi-colons here are part of the HTML output, they are meaningless to the PHP parser/compiler:
[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]
Copy linkTweet thisAlerts:
@ebarMay 13.2013 — they do for all of us.... practice makes perfect ?
Copy linkTweet thisAlerts:
@waveformauthorMay 13.2013 — 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.

Sometimes I get mixed up with all the different ways of doing things, but I'm starting to separate the methods in my head. Like in my first example in the original post the author uses a [COLOR="#0000FF"]<[FONT=Arial]span style=[/FONT][/COLOR] tag to format text. When I believe you can also assign an [I]ID[/I], or [I]Class[/I] to the span tag to accomplish the same thing. The later would be more typing I guess.

But then there are also styles for HTML such as [COLOR="#0000FF"]<[FONT=Arial]table style=&#8221; &#8221;[/FONT][/COLOR] .. which I think would be considered HTML styling (if I'm getting the terminology correct)?

I think I'm getting it all, it just comes down to differentiating the different ways of doing the same thing and being aware of the rules for each method. I'm also finding that I often have to go back and look things up as I sometimes forget how to format something and sometimes wonder if my memory is going..lol But I suppose with enough practice I'll remember spellings, spacing, ect.

Like this page encoding for example:
[CODE]<[FONT=Arial]meta http-equiv=&#8221;content-type&#8221; content=&#8221;text/html;charset=&#8221;utf-8&#8221;[/FONT] />[/CODE]

Yesterday I had that element memorized. But today I had to refer back to my notes to remember there was a semicolon between html and charset. It's little things like that I'm hoping will eventually stick in my memory with practice so I don't have to use the notes or book as much.
Copy linkTweet thisAlerts:
@NogDogMay 13.2013 — 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]


Just to make sure we're clear here, let's take a really simple example:
[code=php]
<?php
echo ";;;;;;;;;;;;;;;;;;";
[/code]

The only semi-colon in that line that has any special meaning to PHP is the very last one, [i]after[/i] the closing quote. All of the semi-colons between the opening and closing quotes are just arbitrary characters to be output by the echo command. In other words, they have no more impact on PHP than the plus signs or eights in either of these:
[code=php]
<?php
echo "++++++++++++++";
echo "8888888888888888";
[/code]

In a more complex example:
[code=php]
<?php
echo "<script type='text/javascript'>alert('Hello, World!');</script>";
[/code]

Once again, 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:
[code=html]
<script type='text/javascript'>alert('Hello, World!');</script>
[/code]
Copy linkTweet thisAlerts:
@waveformauthorMay 14.2013 — Firstly, thanks NogDog for the detailed examples.

I don't think I explained the question well. You're giving me examples of how quotes isolate PHP from other types of code.

I'll try to clarify this question at the bottom of this post. By the way, thank you again, I appreciate you taking the time to respond and in with the level of detail you have!

To comment about - when you said:
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]


I do understand that anything between double quotes is extrapolated, such as variables or scripting. And anything between single quotes is taken literately. That's unrelated I know, but just wanted to mention I understand this rule.

But with regard to the semicolon, what you're saying is: things like Javascript and variable's are executed within the double quotes, But any PHP outside the last semicolon, the [I]delimiter[/I] has nothing to do with what happening between these points This --> " and "; <--This.

What I'm saying is, understand that any semicolon between the double quotes has no meaning to php itself.

I get that.

Here is my question:

In your first three examples, notice how the semicolon ended all the strings. You placed it as the last item in the string. However, The last-more complex example you demonstrated ...the semicolon is placed [I]before[/I] the last closing tag [COLOR="#FF0000"]</script>[/COLOR].

I have to apologize

I think where I might have gone wrong with this question was: I'm might be asking about something more related to the rules of CSS and not PHP. Sorry. I just didn't acknowledge it before I made the post.

Like I said, after reading the chapter on specifying local styles, I found that they put the semicolon before the the double quotes.

I was not asking so much about how strings were enclosed by double quotes, but why the semi colon with different types of code gets placed in front, or sometimes behind certain tags depending on the type of coding being used within the string.

I can see where my question didn't make sense because I'm asking a CSS question in the PHP section.
Copy linkTweet thisAlerts:
@tracknutMay 14.2013 — Keep in mind that PHP (I'm coming to the conclusion that PHP is not a good first language to learn) is simply outputting text to a browser, with the echo commands. Your browser speaks HTML, it speaks Javascript, it speaks CSS, and a few more goodies not worth discussing. Those different languages have completely different syntaxes, including if and where they use semicolons, etc. So if you're looking at this from a PHP viewpoint, you need to ask firstly what language you're trying to output with echo, and then you output a string of characters that are syntactically correct within that language. You can see that to do this right, you need to be very familiar with PHP, HTML, CSS and Javascript, just to follow the discussion in this thread!

Dave
Copy linkTweet thisAlerts:
@waveformauthorMay 14.2013 — Good point tracknut.

For this tread, my focus is on PHP and XHTML. I can see where different perspectives can complicate the discussion with respect to how different syntactical rules can interact with other types of code.

I think what I got out of this thread is that I can safely conclude that CSS and PHP syntax is totally independent of each other within their own space.

My original question, the very first post, (I was not aware at the time) that my question seemed to be more related to the rules of CSS, and not PHP. I was asking about the semicolon inside the [COLOR="#FF8C00"]CSS section[/COLOR] of the string (why [COLOR="#FF0000"]it[/COLOR] was placed where it was). And from what I've read, this is just how local styles are formatted. That was my dilemma but it was confirmed in one of the chapters. And It also helped to get feedback from everyone in this post.

[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]
Copy linkTweet thisAlerts:
@NogDogMay 14.2013 — Besides, we like to beat dead horses into the ground (figuratively). ?
×

Success!

Help @waveform spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...