Connecting Tech Pros Worldwide Help | Site Map

PHP to echo a line in html source...

  #1  
Old August 23rd, 2005, 09:35 PM
Domestos
Guest
 
Posts: n/a
Here an unusual one...

Say i am writing a few lines of code in php script as so...

<?php
echo '<table>';
echo '<tr>';
echo '<td> blah blah </td>';
echo '</tr>';
echo '</table>';
?>

On my html source the code looks like this...

<table><tr><td>blah blah</td></tr></table>

Is there anyway I can get each html tag on a seperate line in the html
source so it is nice and neat (easy readable)?

Thanks
Domestos



  #2  
Old August 23rd, 2005, 09:45 PM
Andy Hassall
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


On Tue, 23 Aug 2005 20:24:38 GMT, "Domestos" <never.you@mind.net> wrote:
[color=blue]
>Here an unusual one...
>
>Say i am writing a few lines of code in php script as so...
>
><?php
>echo '<table>';
>echo '<tr>';
>echo '<td> blah blah </td>';
>echo '</tr>';
>echo '</table>';
>?>
>
>On my html source the code looks like this...
>
><table><tr><td>blah blah</td></tr></table>
>
>Is there anyway I can get each html tag on a seperate line in the html
>source so it is nice and neat (easy readable)?[/color]

echo "<table>\n";

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
  #3  
Old August 23rd, 2005, 09:45 PM
Ken Robinson
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


Domestos wrote:[color=blue]
> Here an unusual one...
>
> Say i am writing a few lines of code in php script as so...
>
> <?php
> echo '<table>';
> echo '<tr>';
> echo '<td> blah blah </td>';
> echo '</tr>';
> echo '</table>';
> ?>
>
> On my html source the code looks like this...
>
> <table><tr><td>blah blah</td></tr></table>
>
> Is there anyway I can get each html tag on a seperate line in the html
> source so it is nice and neat (easy readable)?[/color]

How about:

<?php
$tmp = array();
$tmp[] = '<table>';
$tmp[] = "\t".'<tr>';
$tmp[] = "\t\t".'<td> blah blah </td>';
$tmp[] = "\t".'<tr>';
$tmp[] = '</table>';
echo implode("\n",$tmp)."\n";
?>

I use similar constructs all the time. It make for nice readable code.

Ken

  #4  
Old August 23rd, 2005, 09:45 PM
JDS
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


On Tue, 23 Aug 2005 21:36:30 +0100, Andy Hassall wrote:
[color=blue]
> echo "<table>\n";[/color]

Let me also point out that Andy used double-quotes. Doing this:

echo '<table>\n';

will not produce the same output.

I recommend that you not echo, line by line, large blocks of HTML like you
have done. Makes it quite hard to read (and debug or alter or update).

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

  #5  
Old August 23rd, 2005, 09:55 PM
Hilarion
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


> [snip][color=blue]
> <?php
> echo '<table>';
> echo '<tr>';
> echo '<td> blah blah </td>';
> echo '</tr>';
> echo '</table>';
> ?>
>
> [snip]
>
> Is there anyway I can get each html tag on a seperate line in the html
> source so it is nice and neat (easy readable)?[/color]

Many ways.
First one (using double quotes and escape sequence \n):

<?php
echo "<table>\n";
echo "<tr>\n";
echo "<td> blah blah </td>\n";
echo "</tr>\n";
echo "</table>\n";
?>

or:

<?php
echo "<table>\n<tr>\n<td> blah blah </td>\n</tr>\n</table>\n";
?>

or:

<?php
echo '<table>'."\n";
echo '<tr>'."\n";
echo '<td> blah blah </td>'."\n";
echo '</tr>'."\n";
echo '</table>'."\n";
?>

or escaping to HTML:

<?php
// some PHP code
?>
<table>
<tr>
<td> blah blah </td>
</tr>
</table>
<?php
// other PHP code
?>

or using multiline strings (and double quotes):

<?php
echo "<table>
<tr>
<td> blah blah </td>
</tr>
</table>
";
?>

or (probably) many more.


Hilarion
  #6  
Old August 23rd, 2005, 10:05 PM
Domestos
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


I suppouse to understand them I need to understand the significance between
doubles quotes and single quotes in php code...

Can any one help? or point me to some text

thanks for your help so far, the hardest part is choosing which one to use
and stick too...

Andy Mak



  #7  
Old August 23rd, 2005, 10:15 PM
Andy Hassall
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


On Tue, 23 Aug 2005 20:59:51 GMT, "Domestos" <never.you@mind.net> wrote:
[color=blue]
>I suppouse to understand them I need to understand the significance between
>doubles quotes and single quotes in php code...[/color]

http://www.php.net/manual/en/language.types.string.php

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
  #8  
Old August 23rd, 2005, 10:15 PM
Domestos
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


[color=blue]
> or using multiline strings (and double quotes):
>
> <?php
> echo "<table>
> <tr>
> <td> blah blah </td>
> </tr>
> </table>
> ";
> ?>[/color]

<snip>

That one thats worked for me is as follows... multiline string, with single
quotes. I needed to embed php variables into the string also... as
follows...
echo '<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="gfx/interface/win_'.$wincol.'_tl.gif" width="10" height="10"
border="0"></td>
<td><img src="gfx/interface/win_'.$wincol.'_t.gif" width="'.$winwid.'"
height="10" border="0"></td>
<td><img src="gfx/interface/win_'.$wincol.'_tr.gif" width="10" height="10"
border="0"></td>
</tr>
<tr>
<td><img src="gfx/interface/win_'.$wincol.'_l.gif" width="10"
height="'.$winhgt.'" border="0"></td>
<td width="'.$winwid.'" height="'.$winhgt.'" bgcolor="#393939">';


  #9  
Old August 24th, 2005, 12:15 AM
Will Woodhull
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


Domestos wrote:
<snip>[color=blue]
> Is there anyway I can get each html tag on a seperate line in the html
> source so it is nice and neat (easy readable)?[/color]


My favorite for large blocks of HTML hasn't been mentioned yet. It is
the "here document" structure for defining a string:

<?php
$something = "blah blah"; # just to show off variable interpolation

echo <<<EOHEREDOC
<table>
<tr>
<td> $something </td>
</tr>
</table>
EOHEREDOC;

?>

Check out 'PHP "Here document" tutorial' with Google. Unfortunately
this form of defining a string is not covered very well in the PHP
manual.

The here document preserves whitespace and newlines, and interpolates
variables in the same way doublequote strings do.

  #10  
Old August 24th, 2005, 04:05 PM
Oliver Grätz
Guest
 
Posts: n/a

re: PHP to echo a line in html source...


Domestos schrieb:[color=blue]
> <table><tr><td>blah blah</td></tr></table>
>
> Is there anyway I can get each html tag on a seperate line in the html
> source so it is nice and neat (easy readable)?[/color]

Well, you CAN try to make it nice by yourself, but there's also the
possibility of letting software do that job. With "tidy" one can
prettyprint HTML code and there are possibilities do automagically use
it when sending HTML to the browser. There are extension to PHP that
allow for this. You should read a bit about using the output buffers or
the tidy extension.

As an appetizer, just have a look at this comment:
http://de.php.net/manual/en/ref.tidy.php#48981

AllOLLi
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with generating html source from the php shror answers 10 September 28th, 2007 12:25 PM
OScommerce PHP help. Steven Kalcevich answers 6 November 22nd, 2005 12:08 AM
PHP Upload ! Storing Image data in MySQL dave answers 3 July 17th, 2005 10:40 AM
help with the ecard php andi.z answers 2 July 17th, 2005 12:01 AM