SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Suggestion using Echo

    I have this on my site. I am using the echo statement with <a href> tags.

    Do anyone see any problems coding this way?

    PHP Code:
            echo ('Hello ' .$user->firstName.' '.$user->lastName);
            echo (
    '<br />');        
            echo (
    '<a href="../index.php">Home</a>');            
            echo (
    '&nbsp;|&nbsp;');                
            echo (
    '<a href="'.$_SERVER['PHP_SELF'].'?action=logout">Logout</a>');            
            echo (
    '<hr />');
            if ( 
    $user->userType == '1' ) {
            echo (
    '<b>Admin Options:</b><br />' );
            echo (
    '<a href="lnpprofileall.php">LNP Profile</a>&nbsp;=>&nbsp;');
            echo (
    '&nbsp;|&nbsp;');
            echo (
    '<a href="lnpprofilenew.php">New LNP Profile</a>');            
            echo (
    '&nbsp;|&nbsp;');    
            echo (
    '<br />');        
            echo (
    '<a href="../profile/profileall.php">Users</a>');        
            echo (
    '<hr />');                                                                        
            } 
    Appreciate any suggestions/comments.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's not pretty, but it'll work. If the HTML blocks are very long, try heredoc syntax. You can omit the parentheses on all the echo statements if you'd like; it looks cleaner.

  3. #3
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just remove all the bracketsin your echo statements and your pretty much there

    PHP Code:
            echo 'Hello ' .$user->firstName.' '.$user->lastName
            echo 
    '<br />';         
            echo 
    '<a href="../index.php">Home</a>';             
            echo 
    '&nbsp;|&nbsp;';                 
            echo 
    '<a href="'.$_SERVER['PHP_SELF'].'?action=logout">Logout</a>';             
            echo 
    '<hr />'
            if ( 
    $user->userType == '1' ) { 
              echo 
    '<b>Admin Options:</b><br />' 
              echo 
    '<a href="lnpprofileall.php">LNP Profile</a>&nbsp;=>&nbsp;'
              echo 
    '&nbsp;|&nbsp;'
              echo 
    '<a href="lnpprofilenew.php">New LNP Profile</a>';             
              echo 
    '&nbsp;|&nbsp;';     
              echo 
    '<br />';         
              echo 
    '<a href="../profile/profileall.php">Users</a>';         
       echo 
    '<hr />';                                                                       
            } 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan and Mandes,
    Thanks for your quick response. I thought about using the heredoc, but I thought it wouldnt be appropriate to read as this piece is setup as a menu navigational listing...or would it be?

    As for removing brackets/parenthesis, is there a good reason to do so? I always coded this way, but then again, maybe it was a bad example that I picked up.

    Any benefits using print instead of echo too?

    Also, with my <a href> tags in there, I assume it was done correctly. I have seen different variations

    i.e. putting a backslash in those tags.
    PHP Code:
    echo ( "<a href=\"".$_SERVER['PHP_SELF']."?action=logout\">"."Logout</a>"); 
    This actually came from a book from an author of one of the SITEPOINT books i.e. Harry Fuecks

    I never understood of using a backslash in that tag? I simple coded as what I have done in my example.

    Comments?

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You dont need to have the brackets as echo isnt a function its a construct.

    Differences between echo and print lok here for a better explaination

    http://www.faqts.com/knowledge_base/...l/aid/1/fid/40
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry, just realised that I missed a question.

    PHP Code:
    echo ( "<a href=\"".$_SERVER['PHP_SELF']."?action=logout\">"."Logout</a>"); 
    You have to escape some of the speech marks in this example so that the PHP interpreter knows whether the speech mark is the end of the statement or to be included in the string.

    The other way to do this without escaping is to use single speech mark to define the start and end of the statement.

    PHP Code:
    echo ( '<a href="'.$_SERVER['PHP_SELF'].'?action=logout">'.'Logout</a>'); 
    Which can be written more simply as

    PHP Code:
    echo '<a href="'.$_SERVER['PHP_SELF'].'?action=logout">Logout</a>'); 
    However using single speech marks doesnt allow you to put variables inside the statement.

    PHP Code:
    echo 'You had $number attempts to log in'
    would not work, BUT
    PHP Code:
    echo "You had $number attempts to log in"
    would

    Hope that helps
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes,

    That was the explanation I needed regarding the <a href> usage with speech marks and the parenthesis around echo statements. Now I understand it clearly.

    As for the link you provided for differences between echo and print...

    http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

    I am coming up with a blank page. But I will check back later to see if its working.

    Cheers my friend.

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imchaz View Post
    As for the link you provided for differences between echo and print...

    http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

    I am coming up with a blank page.
    works here
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird...
    This is what I am getting.

    See the attached file.
    Attached Images Attached Images

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Still working here for me.

    Never mind, here is the text from the page, all credits to the original authors.

    There is a difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World";And $ret will be 1That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precendence list though. Only "," AND, OR and XOR are lower.echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.If the grammar is: echo expression [, expression[, expression] ... ]Then echo ( expression, expression ) is not valid. ( expression ) reduces to just an expression so this would be valid: echo ("howdy"),("partner");but you would simply write this as: echo "howdy","partner"; if you wanted to use two expression. Putting the brackets in there serves no purpose since there is no operator precendence issue with a single expression like that.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There is another subtle but important difference in how you use quotes.

    echo / print below is swoppable.

    With double quotes, PHP parses text inside double quotes ...

    so:
    PHP Code:
    $var "salmon";

    print(
    "I wouldn't like a $var to swim up my rectum.")

    // and its equivalent:

    echo 'I wouldn\'t like a ' $var ' to swim up my rectum' ;

    // both will give the required result - a stern warning.

    //whereas:

    echo 'I wouldn\'t like a $var to swim up my rectum' ;

    outputsI wouldn't like a $var to swim up my rectum 
    With single quotes you have to "drop in and out" of PHP and concatonate the pieces of the string.

    I hope that helps you, and watch out for them pesky salmon.

  12. #12
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    With single quotes you have to "drop in and out" of PHP and concatonate the pieces of the string.
    Hi Mate

    I'll just pick you up on your above comment incase it causes confusion for the OP, you dont actually 'Drop' out of PHP, this would imply using tags <?php and ?>, but rather interrupt the text to insert the variable

    PHP Code:
    echo 'There are no' $var 'on my person'
    notice that the 3 parts of the statement are joined together using the . operator

    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    Yeah, poor choice of words then - its just how I describe it to myself. I wonder what is the correct term then when you concat literal text and vars?

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Off Topic:

    Yeah, poor choice of words then - its just how I describe it to myself. I wonder what is the correct term then when you concat literal text and vars?
    String concatenation. There's no need for a separate word when the concatenation involves literal strings and variable strings. If that wasn't what we were using it for, why would concatenation exist?

  15. #15
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One vote for dropping in and out of php.
    And creating helper methods to get the ugly logic out the templates.

    PHP Code:
    <?php echo user_greeting(); ?>
    <br />
    <a href="../index.php">Home</a>
    &nbsp;|&nbsp;
    <?php echo logout_link(); ?>
    <hr />
    <?php if $user->is_admin(); { ?>
      <b>Admin Options:</b><br />
      <a href="lnpprofileall.php">LNP Profile</a>&nbsp;=>&nbsp;
      &nbsp;|&nbsp;
      <a href="lnpprofilenew.php">New LNP Profile</a>
      &nbsp;|&nbsp;
      <br />
      <a href="../profile/profileall.php">Users</a>
      <hr />
    <?php ?>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest interspersing PHP with raw HTML too, it saves a little on script execution time and makes code cleaner (plus many editors will syntax highlight your HTML if you drop in and out of PHP). Try something similar to samsm's example.

  17. #17
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes, Cups, DG, SamSM, & Akash,

    Thanks for all the suggestions. All this info will definitely help me write cleaner and efficient code on my next pages. I will go back to what I have written and make the appropriate changes to them.

    Samsm, thanks for pointing me to combine the two.

    I love posting on this forum. Responses are so much quicker.

    Cheers all!


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
  •