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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clean ways to print (echo)

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I would like to know what are the different ways to print (echo) text & variables.

If using echo "", it's a pain to cancel all the double quotes; see examples. Using the single quotes can also get into conflict with text.


Code:
echo "<a href=\"[URL unfurl="true"]www.link.com\"[/URL] class=\"css_class\">Richy's site</a> \n";


If I need to print a lot of text and variables what are the options aside from using echo <<<EOF etc...

In perl the following is clean and can output multiple lines of text and variables.

Code:
print qq(
	Some text here + $variable
	Another line.
	);

This way it doesn't get in conflict with single and double quotes in the text to print.

I'm new to php, so any tips and tricks would be appreciated.
 
If you need no print very large quantities of text, then closing the PHP tags is the better way. as you don't have to deal, with double quotes or escape characters or anything.

It is not the cleanest of ways, but it is very usefull when you have a lot of text to print out.

For example say you wanted to print a very large chunk of html in the event a condition is met:

Code:
<?PHP
if($somevar=="somevalue'){
$someothervar="someothervalue";
[red]?>[/red] [green]//You close the PHP delimitewr and and just write out everything you need.//[/green]
[blue]A lot of HTMl code here, with "double " quotes interspersed, maybe even some CSS with single 'style... ' quotes then some more text etc.... more quotes " for something inside." .....
[/blue]
[red]<?PHP[/red][green]//Reopen the PHP delimiter here//[/green]
more code here.... 
[green]//final closing php delimiter [/green]

Many people will tell you it is not convenient to exit the PHP delimiters very frequently, but I have yet to find significant delay in the parsing with this practice.

Other methods of outputting large quantities of text, is to build it in sections and then output it.

For example:
Code:
$output="";
$output.="Some text";
$output.="<some html tag></endtag>";
...
[green]//Keep concantenating text to the variable until you are ready to print it out, and then just issue an echo command on a single variable//[/green]
[blue]echo $output;[/blue]

I find that if you stick with double quotes for the outer delimiting, and single quotes inside everything works well.

In case you need to acutallky output the character " then you can use the [blue]htmlentities()[/blue] to convert the characters into there entitiy equivalents:
[red] &quot; [/red].

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I am a strong proponent of staying inside PHP tags. Too many scripts posted here switch in and out of <?php...?> tags so much, it becomes very difficult to follow their code. Also, my own testing has shown that multiple switches in and out of <?php...?> tags does degrade performance, but not by a lot each switch.

If you need to output many lines of code, PHP supports the same heredoc syntax perl does:

Code:
print <<<EOD
This is a large block of text to print.
This is a large block of text to print.
This is a large block of text to print.
This is a large block of text to print.
This is a large block of text to print.
EOD;


In your specific first example, you can print using single-quotes as delimiters:

Code:
echo '<a href="[URL unfurl="true"]www.link.com"[/URL] class="css_class">Richy's site</a>' . "\n";

Strings inside singlequotes are not interpolated. Neither are escape strings such as \n, \r, \t. So to reference variables, I use concatenation:

Code:
$a = "Richey's Site";
echo '<a href="[URL unfurl="true"]www.link.com"[/URL] class="css_class">' . $a . '</a>' . "\n";



Want the best answers? Ask the best questions! TANSTAAFL!
 
Also worth noting for the person who said he always uses double quotes to enclose his strings, is that performance is always worse with double quotes because the engine has to parse the string for variables. I always use single quotes to surround any string unless I am using a variable in the string.

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
I'm always very busy escaping quotes, so i prefer printf or sprintf:
Code:
printf ("<a href='%s' class='css_class'>%s</a>\n", '[URL unfurl="true"]www.link.com',[/URL] 'Richy\'s site');

Expecially when you are populating tables in html from db results, this makes your code very readable.

mcvdmvs
&quot;It never hurts to help&quot; -- Eek the Cat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top