...

View Full Version : Replacing "print"



Daniellez
09-13-2009, 11:20 AM
I have a few php docs that are loaded with print commands. Is there a way to streamline this code to alleviate all the print commands (or consolidate them in some fashion?)


print "<table align=\"center\" class=\"content\">\n";
print "<tr>\n";
print "<td valign=\"top\">\n\n";

print "<div align='center'><br />";

if (isset($_GET['directory_name']))
{

print "$StrDeleteFolderQuestion<br />";
print "<strong>";
print "/".htmlentities($path.$directory_name);
print "</strong><br />";

}
else if (isset($_GET['filename']))
{

print "$StrDeleteFileQuestion<br />";
print "<strong>";
print "".htmlentities($path.$filename);
print "</strong><br />";

}

print "<a href='$base_url."'><img src='gfx/button_cancel.png' alt='$StrCancel' width='122' height='27' border='0'></a>";
print "&nbsp;&nbsp;&nbsp;&nbsp;";
if (isset($_GET['directory_name'])) print "<img src='gfx/delete_folder.png' alt='$StrYes' width='145' height='27' border='0'>";
else if (isset($_GET['filename'])) print "<img src='gfx/delete_file.png' alt='$StrYes' width='145' height='27' border='0'>";

print "</div>";

print "</td>";
print "</tr>";
print "</table>";

crivion
09-13-2009, 11:33 AM
yes you can do a simple one like


print "<table align=\"center\" class=\"content\">\n";
print "<tr>\n";
print "<td valign=\"top\">\n\n";

print "<div align='center'><br />";

to

print "<table align=\"center\" class=\"content\">\n
<tr>\n
<td valign=\"top\">\n\n

<div align='center'><br />";

and so on

Fou-Lu
09-13-2009, 12:01 PM
Use the heredoc (http://php.ca/manual/en/language.types.string.php#language.types.string.syntax.heredoc) syntax to make it look cleaner.

abduraooft
09-13-2009, 01:04 PM
Since it's not possible(AFAIK) to have loops/conditions inside heredoc, it'd be better to clean the code by multiple php blocks and clever use of single/double quotes. For instance, I'd alter your code like
<?php
echo "<table align=\"center\" class=\"content\">\n
<tr>\n
<td valign=\"top\">\n\n

<div align='center'><br />";

if (isset($_GET['directory_name']))
{
echo $StrDeleteFolderQuestion."<br /><strong>/".htmlentities($path.$directory_name)."</strong><br />";
}
else if (isset($_GET['filename']))
{
echo $StrDeleteFileQuestion."<br /><strong>".htmlentities($path.$filename)."</strong><br />";
}

.........
echo "</div>
</td>
</tr>
</table>";
?>

If there is a large block of html, without any PHP in it, you may close and re-open the php tags like

<?php
if(...){
?>
html here
<?php
}
?>

<?php
if(...){
?>
large html here and <?php echo "a bit of php";?> and some other pure text
<?php
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum