Advanced Search

Results 1 to 5 of 5

Thread: Bypassing the Echo

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Lightbulb Bypassing the Echo

    Both of the following excerpts that I have come across appear to work. But I cannot find in my PHP book any indication that the second excerpt is acceptable. I invite comments on the second excerpt: Is it efficient? Is it preferable? Is there any downside to using it?

    Best,

    A.


    PHP Code:
    <?php
    if ( $here === 'qualifications' ) {
    echo 
    "Our widgets are better than anyone else's. 
    <br />
    <br />
    Call us."
    }
    ?>

    PHP Code:
    <?php
    if ( $here === 'qualifications' ) {
    ?>Our widgets are better than anyone else's. 
    <br />
    <br />
    Call us.
    <?php
    }
    ?>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    You're thinking about it backwards-- that's NOT part of PHP-- specifically because PHP is only within the <?php ?> tags. That's just normal HTML.
    In other words, that's completely acceptable and useful.

    What's very interesting about this is that although that's technically outside of the PHP, it still does actually operate within the "if" structure of the PHP-- only if the "IF" is active, then that HTML is displayed. This of course works with other things like loops, functions, etc., in just the same way.


    So I suppose it is "part of PHP" in some vague sense, but at the same time it's incredibly convenient because it allows you to use "real" HTML without worrying about escaping the quotes or anything-- just type the HTML as you normally would, but you can still use it within an "if" or other PHP structure.


    This is useful for templating and layout in general-- it's best to separate your PHP and HTML as much as possible, and this is one good way to do it. It's much preferable to multi-line strings with lots of complex HTML.

    (Also, just for convenience, it'll work in your syntax-highlightling editor and highlight the HTML rather than leaving it as a mono-color string.)



    As for efficiency, it's more efficient-- you are processing plain HTML (DIRECTLY AS OUTPUT), without ever making it a string to process. Realistically it won't make much of a noticable difference (unless you have a huge site, and still might not be obvious even then), but it won't hurt anything certainly.


    And just for the record, this is important/helpful to know as well:
    PHP Code:
    <?php
    if ( $here === 'qualifications' ) {
    ?>Our widgets are better than anyone else's. 
    <br />
    <?php echo 'Some text'?>
    <?php 
    echo $myvar?>
    <br />
    Call us.
    <?php
    }
    ?>
    As you can see, you can still use PHP inside that, and it will also be within the "if" statement.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    marain (11-25-2012)

  4. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default

    Parenthetical addendum: In reviewing what I posted originally, I note that, in attempting to simplify the problem, I forgot the semicolons. I assume that in the first example, we need a semicolon after call us." In the second example, I assume the semicolon would immediately follow the second <?php.

    A.
    Last edited by marain; 11-25-2012 at 02:27 PM. Reason: To clarify an ambiguity.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    You're right-- there's a missing semi-colon in the first example. That's required.

    But the second example doesn't have any kind of "echo" statement or anything like that. Remember, it's HTML, not PHP. So not only do you not need it-- but it's not allowed. There's nothing to "end" there with a semi-colon. It's not a line of code. It's just HTML, which happens to be within the context of an "if" statement.

    Does that help?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    130
    Thanks
    49
    Thanked 0 Times in 0 Posts

    Default

    Indeed it does. Thanks again.

    A.

Similar Threads

  1. Bypassing Random User Script
    By Xcalibre in forum PHP
    Replies: 1
    Last Post: 03-30-2012, 04:39 PM
  2. echo inside an echo?
    By crobinson42 in forum PHP
    Replies: 5
    Last Post: 05-20-2010, 02:32 AM
  3. Bypassing X Amount of Uses
    By Trinithis in forum The lounge
    Replies: 4
    Last Post: 08-08-2007, 12:15 AM
  4. echo...
    By pavmoxo in forum PHP
    Replies: 11
    Last Post: 04-28-2006, 04:55 PM

Bookmarks

Posting Permissions

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