...

View Full Version : when to use echo? (proper use of echo)?



che_anj
08-23-2007, 11:46 AM
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...

abduraooft
08-23-2007, 01:12 PM
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

<form name="msgform" method="post" action="<? echo $PHP_SELF; ?>">
<table>
<tr>
<td class="table-list-entry1"><? echo $row[firstname].' '.$row[lastname]; ?></td>
.............
...........
</tr>
</table>

che_anj
08-23-2007, 01:43 PM
tnx...

che_anj
08-23-2007, 01:52 PM
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>';

abduraooft
08-23-2007, 01:59 PM
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

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

che_anj
08-23-2007, 02:04 PM
tnx abduraooft

hardnrg
08-23-2007, 02:06 PM
http://www.techmount.com/index.php/20040622/php-dev-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;



<?php

# php code

if ($age >= 18) { ?>
Welcome!
<?php } else { ?>
You are underage!
<?php }

# other php code

?>

che_anj
08-23-2007, 02:28 PM
tnx guyss

CFMaBiSmAd
08-23-2007, 04:36 PM
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 -

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 -

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum