Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

println

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
In the vein of there's no such thing as a stupid question, I want to make sure I just haven't been missing something for the last 18 months.

Is there any built in println type function in PHP? I'm getting sick of appending
Code:
."\n"
to my echo statements if I want my HTML source to be readable.

I'm pretty sure I know the answer, but you know sometimes you just don't see something. And yes I know I can write my own, but I don't want that.

-Rob
 
If there is one, I don't know about it.

I don't explicitly output newline characters. I use multiline print statements.

print '
<html>
<body>
foo!
</body>
</html>';


My coding standard is that every tag that needs one includes it's own newline. (I know that isn't clear. Maybe an example...).

Code:
<?php
$foo = array ('foo', 'bar', 'baz', 'fred');

print '<html>
   <body>
      <table>';

foreach ($foo as $fo)
{
//The <tr> tag includes its own newline
   print '
         <tr>
            <td>
               '. $fo . '
            </td>
         </tr>';
}

//</table> tag includes its own newline
print '
      </table>
   </body>
</html>';
?>


The &quot;gotcha&quot; with doing it this way is that in your code indentation, you have to keep track of two levels of indentation -- one for your code and one for your print literals.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
No doubt, hey that reminds me, I picked up a new syntax the other day which works really well for multiline print statements, haven't had much chance to use it yet though... it's called heridoc (not sure on spelling) and goes like this...

Code:
$something = <<< IDENTIFIER
multiline text here, all variables will be expanded
and you don't need to escape single, double or other quotes
for that matter you don't need to escape anything.  All spacing and formatting will be preserved.
IDENTIFIER;

IDENTIFIER can obviouslly be whatever you want.

-Rob
 
One gotcha on that syntax...

the
IDENTIFIER;

which closes things, must be exactly like that, with no whitespace before it, so if your editor auto-idents that, it will goof everything up.

-Rob
 
Yeah, heredoc (short for &quot;here document&quot;) is documented in the online manual:
I'll use it if I have a lot of HTML to dump out at once. But for shorter stretches, it's easier, in my opinion, to just use multiline quoted strings. Particularly when outputting variables intermixed with literals.

Heredoc syntax in PHP will interpolate variables. But it has the same problem as a regular quoted print statement in not liking associative array references in the string. And it's a lot easier to break out of a quotes string to concatenate a &quot;bare&quot; associative array reference than with a heredoc.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top