Closed Thread Icon

Topic awaiting preservation: Inline Vs Echo (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12239" title="Pages that link to Topic awaiting preservation: Inline Vs Echo (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Inline Vs Echo <span class="small">(Page 1 of 1)</span>\

 
Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 05-29-2002 22:24

Well some of us discussed this a little bit in another thread, but I'm kind of confused now.
Allot of people say that PHP should never break away. It should go from <?php to ?> and never stop. Those people say that if you are going to add html into the file that you need to echo it and add the back slash to the double quotes. Other people say that using inline html is better and much faster. Indeed I have noticed that inline html in php is much faster than echoing the html, but how is on coding? I mean it makes the code look better, but is it the right way to do things? Or is it considered bad coding.

Read this article: Inline not echo

Then read this article: Echo not Inline

Two very diffrent views on Inline vs Echo. Tell me what you think. I respect most of your opinions.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 05-29-2002 23:05

I'm also interested in what the coders here say about this topic.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-29-2002 23:15

funk dat.

I use inline and I use it alot. It's easier and I don't see how it makes anything more difficult to update.

Personally I've actually gotten into the habit of keeping my PHP and HTML seperate. (I do this with mod_perl on my company stuff too). This makes everything much easier to update than this method and according to both articles it looks like it's a better performer too.

Beside's I love having the ability to drop out of php mode mid loop if I need to print a bunch of HTML.

Something like:
<?
$count = 0;
while($count < 10) {
?>
Lot's of html here
<?
$count++;
}
?>

That way I keep from having to worry about all the backslashes, quotes etc.
I think that second article is crap, he has no basis for his "rule" except for his PHP experience on a site which it sounds like someone set up poorly in the first place.

EDIT: (a ha I was correct, his gripe is from trying to edit a site based on PHP-NUKE. If you're gonna use someone else's codeset don't complain about the overhead of modifying it)



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................


[This message has been edited by bitdamaged (edited 05-29-2002).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-30-2002 00:17

Lets see what official PHP manual has to say about this:

--- start copy/paste ---

PHP allows you to use structures like this: Example 5-2. Advanced escaping

<?php
if ($expression) {
&nbsp;&nbsp;&nbsp;&nbsp;?>
&nbsp;&nbsp;&nbsp;&nbsp;<strong>This is true.</strong>
&nbsp;&nbsp;&nbsp;&nbsp;<?php
} else {
&nbsp;&nbsp;&nbsp;&nbsp;?>
&nbsp;&nbsp;&nbsp;&nbsp;<strong>This is false.</strong>
&nbsp;&nbsp;&nbsp;&nbsp;<?php
}
?>

This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print() or somesuch.

--- end copy/paste ---


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-30-2002 00:59

I'm gonna add something here. I think where the author of the second article had issues was with huge masses of code where there are ton's of additional php statements embedded within the HTML page. this can make finding the relevant PHP when you are trying to change something problematic.

But again this is something that is easily avoided with common "good coding" practices.

An example. One thing I used to do (and still do in a way) is put all programatical code in the top of my php docs. and assign all dynamic content to a variable. After this Comes all my HTML which only contains <?= $myvar; ?> statements inside. This allow's me to easily update my PHP and my HTML easily since the PHP and HTML are kept seperate. (I do a similar thing now but I use a lot more classes and actually include the HTML files instead putting them in the page, this allow's me to have lot's of easily reusable code)



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 05-30-2002 10:20

Thanks guys you confirmed what I was thinking. I use Inline HTML myself. I found that putting all my HTML into a file and using PHP Include made for cleaner code. It also made it much easier to update not to mention faster. I also found that I could easily make a header and footer for every single page. You don't know how much time that has saved me. Anyways .. Cheers guys.

May the hardcore coder live on.


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-31-2002 01:53

Okay

I have a question on this subject that's been bothering me a bit. Please forgive me for the long example, but I'd like to know if there's a better way to write this inline as I'm working with a great graphics, HTML guy and I'd like my code to be as easy as possible for him to work with.

A lot of my code involves using loops and conditional statements to decide what and how much HTML to echo to the browser.

code:
<?php
include("$DOCUMENT_ROOT/includes/admin/headers/header_admin.inc.php");
?>
</HEAD>

<BODY bgcolor=#cc6600 background=/images/admin/adminbg.gif>

<table cellpadding=10 cellspacing=0 border=0 width=100% height=100%>
<tr>
<td bgcolor=#ff9900 align=center valign=middle><a href="javascript:window.close()"><img src="/images/admin/logoff.gif" width="101" height="20" border="0"></a>
</td>
<td bgcolor=#000066 align=right height=50><a name=top><img src="/images/admin/pcs.gif" width="132" height="50" border="0"></a></td>
</tr>
<tr>
<td bgcolor=#000099 width=150 valign=top>

<?php

if(!isset($doit))
{
//content area
$display .= "<td bgcolor=ffffff valign=top>\n";
$display .= "<h1>Team Photo Gallery Delete</h1>\n";
$display .= "<form action=\"$PHP_SELF\" method=\"POST\">\n";
//start thumbnail display table
$display .= "<table cellpadding=6 cellspacing=2 border=0>\n<tr>\n";
$display .= "<td align=\"left\"><p><b>Photo</b></p></td>\n<td align=\"left\"><p><b>Select</b></p></td>\n";


//get current directory of this file

$curdir = $file_name;

$parent_dir = "$DOCUMENT_ROOT/gallery/$curdir/"; //starting directory

$photo_array = array();



if($file = @opendir($parent_dir))

{

while(($entry = readdir($file)) !== false)

{

if($entry != '.' && $entry != '..' && !eregi("\.html$",$entry) && !eregi("\.php$",$entry) && !eregi("\.pcgi$",$entry) && !@is_dir($parent_dir.$entry))

{

array_push($photo_array, $entry);

}

}

}



natcasesort($photo_array);

foreach($photo_array as $photo)

{



if($i % $thumbs_per_row != 0)

{
if(is_file($parent_dir."thumbnails/t_".$photo))
{
$path_to_pic = $parent_dir.$photo;

$size = getimagesize($parent_dir."thumbnails/t_".$photo);

$display .= "<td bgcolor=orange><img src=\"/gallery/$curdir/thumbnails/t_$photo\" $size[3] border=2></td>\n";
$display .= "<td bgcolor=orange valign=\"bottom\"><input type=\"checkbox\" name=\"photo[]\" value=\"$path_to_pic\"></td>\n";

$i++;
}

}

else

{
if(is_file($parent_dir."thumbnails/t_".$photo))
{
$path_to_pic = $parent_dir.$photo;

$size = getimagesize($parent_dir."thumbnails/t_".$photo);

$display .= "</tr>\n<tr>\n<td bgcolor=orange><img src=\"/gallery/$curdir/thumbnails/t_$photo\" $size[3] border=2></td>\n";
$display .= "<td bgcolor=orange valign=\"bottom\"><input type=\"checkbox\" name=\"photo[]\" value=\"$path_to_pic\"></td>\n";

$i = $i + 2;
}

}

}



if($i % $thumbs_per_row != 0)

{
$display .= "<input type=\"hidden\" name=\"doit\" value=1>\n</tr>\n";
$display .= "<tr>\n<td bgcolor=orange align=left><input type=\"submit\" name=\"submit\" value=\" Delete Photos \"></td>\n";
$display .= "</tr>\n</table>\n</form>\n</td>\n";

}

else

{
$display .= "<input type=\"hidden\" name=\"doit\" value=1>\n</tr>";
$display .= "<tr>\n<td bgcolor=orange align=left colspan=2><input type=\"submit\" name=\"submit\" value=\" Delete Photos \"></td>\n";
$display .= "</tr>\n</table>\n</form>\n</td>\n";

}
}
elseif(isset($doit))
{
$display = "<td bgcolor=ffffff>\n<table cellpadding=0 cellspacing=0 border=0 width=100% height=100%>\n<tr>\n<td valign=\"top\"><p>";
foreach($HTTP_POST_VARS['photo'] as $a)
{
unlink($a);
$b = preg_replace("/\/$file_name\//", "/$file_name/thumbnails/t_", "$a");
if(is_file($b))
{
unlink($b);
}
preg_match("/\/$file_name\/(.*\..*)$/", $a, $match);
$cur_file = $match[1];
$display .= "$cur_file has been deleted.<br>";
}
$display .= "</td>\n</tr>\n";
$display .= "</table>\n</td>\n";
}

include("$DOCUMENT_ROOT/includes/admin/admin_menu.inc.php");
echo $display;
include("$DOCUMENT_ROOT/includes/admin/footer.inc.php");
?>



Not to mention the fact that I'd love not to have to escape all the double quotes in my HTML that I'm echoing.

Thanks



-Butcher-

Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 06-01-2002 12:49
quote:
A lot of my code involves using loops and conditional statements to decide what and how much HTML to echo to the browser.



Not sure what you are doing there, but why are you echoing the HTML?
Is the HTML something that you can just put in the PHP file? (Inline html)
Or might the HTML be put inside via the INCLUDE or REQUIRE function in PHP?

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 06-04-2002 05:30

Kriek

The script that I posted is displaying different things to the user depending on which load of the page it is. On the first load of the page the code builds a form (which changes each time based on the contents of the directory in question) for the user to make choices from. On the second load of the page the script deletes the files (photos actually) that the user has chosen and displays messages confirming what has been done. Each time the page is displayed it can be different than the last time, which is why I can't just include the html in a file, because it gets built dinamically as the script runs, and then the result gets echoed where I need it inbetween my include files which are the static html parts of the page.

If I haven't answered your question well enough, let me know and I'll give it another shot.

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-05-2002 17:52

Hey butcher. There's no reason you can't use some include files here, as well as a couple of functions to clean everything up.

(Note I think you can use require's within conditionals but they are always going to be parsed so using include's lessen's the over head).

One thing I notice right off the bat is you have no function's here. This is one way you can use the include's anywhere.

something like

function get_middle_html() {
if ($some_var == "something") $retfile = "/return/this/file.inc";
elseif($some_var == "something else") $retfile = "/return/this/file2.inc";
else($some_var == "something else") $retfile = "/return/this/file3.inc";
include($ret_file);
}

<html>
Whatever
<?
get_middle_html();
?>

More staticHTML
</html>

Using more functions would definately allow you to clean some things up.




.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 06-06-2002 04:38

Thanks Bit

I'll cut it up, move what I can into functions and see how it works.



-Butcher-

« BackwardsOnwards »

Show Forum Drop Down Menu