Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts

    when to use echo? (proper use of echo)?

    when to use echo?

    --------------------------------------------------------------------------------

    it is said that its a wrong practice to use echo to output static html. So when to use echo? because I am used in using echo like this code below.

    echo '<form name="msgform" method="post" action="'.$PHP_SELF.'">';


    echo '<tr>
    <td class="table-list-entry1">'.$row[firstname].' '.$row[lastname].'</td>
    <td class="table-list-entry1">'.$row[position].'</td>
    <td class="table-list-entry1">'.$row[work_tel].'</td>
    <td class="table-list-entry1">'.$row[work_email].'</td>
    </tr>';

    Can u correct it?

    tnx...

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by che_anj View Post
    when to use echo?

    --------------------------------------------------------------------------------

    it is said that its a wrong practice to use echo to output static html. So when to use echo? because I am used in using echo like this code below.

    echo '<form name="msgform" method="post" action="'.$PHP_SELF.'">';


    echo '<tr>
    <td class="table-list-entry1">'.$row[firstname].' '.$row[lastname].'</td>
    <td class="table-list-entry1">'.$row[position].'</td>
    <td class="table-list-entry1">'.$row[work_tel].'</td>
    <td class="table-list-entry1">'.$row[work_email].'</td>
    </tr>';

    Can u correct it?

    tnx...
    It's always a good practice to seperate html and php.
    In your case you can adopt something like
    Code:
    <form name="msgform" method="post" action="<? echo $PHP_SELF; ?>">
    <table>
    <tr>
    <td class="table-list-entry1"><? echo $row[firstname].' '.$row[lastname]; ?></td>
    .............
    ...........
    </tr>
    </table>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    tnx...

  • #4
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    how about this one ?

    echo '<a href="'; echo $_SERVER["PHP_SELF"]; echo '?click=client_list.php&menu=1&empid='.$row['employer_id'].'">'.$row[employer_name].'</a>';

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by che_anj View Post
    how about this one ?

    echo '<a href="'; echo $_SERVER["PHP_SELF"]; echo '?click=client_list.php&menu=1&empid='.$row['employer_id'].'">'.$row[employer_name].'</a>';
    If I were you, I prefer
    Code:
    <a href="<? echo $_SERVER["PHP_SELF"];?>?click=client_list.php&menu=1&empid=<? echo $row['employer_id'];?>"><? echo $row[employer_name];?> </a>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    tnx abduraooft

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    http://www.techmount.com/index.php/2...echo-vs-print/

    The most important difference, in my mind, is that print behaves like a function and can be used as one. When getting to speed, print returns a boolean value for each interaction to specify if it was successful or not while echo does not, which would probably make echo work faster.
    Can't find the URL now, but there was a site that confirmed echo was faster, but it was only after 1000+ echo's, and the difference was microseconds.

    Don't forget you can also do this;

    Code:
    <?php
    
     # php code
    
    if ($age >= 18) { ?>
    Welcome!
    <?php } else { ?>
    You are underage!
    <?php } 
    
     # other php code
    
    ?>

  • #8
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts
    tnx guyss

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,107
    Thanks
    2
    Thanked 326 Times in 318 Posts
    echo to output static html.
    Back to your original question. Static HTML refers to HTML that does not contain any PHP variables or function calls. Your first example does contain PHP variables, so it is actually dynamically generated HTML.

    Some general rules I use -

    Overall, try to keep the number of times you leave and enter php mode down to a minimum, just to keep the code cleaner.

    If you only have a few lines of output, even if it is 100% static HTML, staying in php mode and echoing it makes for clearer overall coding.

    When you have more than a few (3+) lines of output, use a "majority" rule to determine if you should echo a string containing a mix of HTML/PHP output or if you should leave php mode and put inline HTML (that may or may not contain a small amount of php output.) If a majority of the code is static HTML, with only a few overall pieces of php output and with a maximum of one piece of php output per line, leaving php mode and putting the php output in using <?php echo ... ?>, results in easier to read code. If a majority of the lines contain php output or several of the lines contain more than one piece of php output, staying in php mode and echoing a string containing the mix of HTML/PHP results in clearer code.

    Any speed penalty of switching into or out of php mode or of embedding php variables in a double-quoted string vs concatenating them with pieces of strings is a minor consideration on any page (will add at most a few 10's of milliseconds per page.) Go with what results in the cleanest, clearer code.

    Now on to the subject of easy to write and read code and putting php variables in double-quoted strings. You won't believe how many syntax errors have been posted in forums by people that write strings like the following and then cannot figure out what is causing the error -
    PHP Code:
    echo '<a href="' $_SERVER['PHP_SELF'] . '?click=client_list.php&menu=1&empid=' $row['employer_id'] . '">' $row['employer_name'] . '</a>'
    Would you not rather type and read -
    PHP Code:
    echo "<a href='{$_SERVER['PHP_SELF']}?click=client_list.php&menu=1&empid={$row['employer_id']}'>{$row['employer_name']}</a>"
    Just my personal preference.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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