www.webdeveloper.com
Results 1 to 14 of 14

Thread: Is the semicolon in the correct place?

  1. #1
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30

    Question 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.

    Q:semicolon.jpg
    when you have CSS style rules embedded in an PHP string, is the semicolon (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;\">$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 style rules 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.
    Last edited by waveform; 05-11-2013 at 11:08 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    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:
    Code:
    "<p>Hello, <span style=\"font-weight:bold;\">$name</span>!</p>"
    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 not 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):
    PHP Code:
    print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30
    Quote Originally Posted by NogDog View Post
    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:
    Code:
    "<p>Hello, <span style=\"font-weight:bold;\">$name</span>!</p>"
    I'm not sure what you mean as far as having meaning in the context? The semicolon I highlighted in red is not displayed when the form is submitted. The output shows as this:
    Code:
    Hello, Name!
    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?
    Quote Originally Posted by NogDog View Post
    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 not 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):
    PHP Code:
    print "<p>Hello, <span style='font-weight:bold;'>$name</span>!</p>"
    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 already 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. See the right column in attachment 2
    Attached Images Attached Images
    Last edited by waveform; 05-12-2013 at 12:06 AM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    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 not in PHP mode, only entering that mode when needed.

    PHP Code:
    <?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']
    );
    The ugliest part (to me) is the handling of array elements.
    PHP Code:
    $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>"


    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:
    PHP Code:
    echo "<p>{$text['foo']}</p>";
    // or
    echo "<p>".$text['foo']."</p>"
    Did any of that help?

    For extra credit, read up on the HEREDOC and NOWDOC quoting techniques.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30
    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 semi colon placed "before" the double quotes for the styling part of the string ...while the end part of the main string the "main outer quotes" were placed before the last semicolon.

    Code:
    print "<p>Hello, <span style=\"font-weight:bold;\">$name</span>!</p>";
    As noticed, this was how it was explained in my PHP book, However after looking in my XHTML book, I noticed a chapter talking about setting local 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. See the order in the attached image.
    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.
    Attached Images Attached Images
    Last edited by waveform; 05-12-2013 at 02:15 PM.

  6. #6
    Join Date
    Aug 2006
    Posts
    1,904
    Quote Originally Posted by waveform View Post
    As noticed, this was how it was explained in my PHP book, However after looking in my XHTML book, I noticed a chapter talking about setting local styles inside the document as a posed to linking to an external CSS.
    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

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    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:
    HTML Code:
    <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>
    But as the semi-colons here are part of the HTML output, they are meaningless to the PHP parser/compiler:
    PHP Code:
    <?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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    124
    they do for all of us.... practice makes perfect
    Samantha Morrison
    Knock-Out Web Designs
    http://kowebdesigns.com
    Website Developer/Designer

  9. #9
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30
    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 <span style= tag to format text. When I believe you can also assign an ID, or Class 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 <table style=” ” .. 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:
    <meta http-equiv=”content-type” content=”text/html;charset=”utf-8” />
    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.
    Last edited by waveform; 05-13-2013 at 05:11 PM.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Quote Originally Posted by waveform View Post
    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.
    ...
    Just to make sure we're clear here, let's take a really simple example:
    PHP Code:
    <?php
    echo ";;;;;;;;;;;;;;;;;;";
    The only semi-colon in that line that has any special meaning to PHP is the very last one, after 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:
    PHP Code:
    <?php
    echo "++++++++++++++";
    echo 
    "8888888888888888";
    In a more complex example:
    PHP Code:
    <?php
    echo "<script type='text/javascript'>alert('Hello, World!');</script>";
    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:
    HTML Code:
    <script type='text/javascript'>alert('Hello, World!');</script>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30
    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:
    <?php
    echo "<script type='text/javascript'>alert('Hello, World!');</script>";
    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 delimiter 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 before the last closing tag </script>.

    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.
    Last edited by waveform; 05-13-2013 at 08:28 PM.

  12. #12
    Join Date
    Aug 2006
    Posts
    1,904
    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

  13. #13
    Join Date
    Jun 2007
    Location
    USA
    Posts
    30

    Lightbulb

    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 CSS section of the string (why it 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:
    print "<p>Hello, <span style=\"font-weight:bold;\">$name</span>!</p>";
    Last edited by waveform; 05-13-2013 at 08:52 PM.

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Besides, we like to beat dead horses into the ground (figuratively). :P
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles