Log in

View Full Version : add code font size in the php echo



rhodarose
11-11-2010, 06:31 AM
Good day!

I have a pure php code, so my table was in echo and I want to change the font and font-size of text in table header but when I put thid code:


echo "<font size="18" face='Arial'>";
echo "<table>";

and at the lower part i close it
echo "</table>";
echo "</font>";



It only take effect in the text outside the table. and when I try to put it on the <table> It doesn't take effect.

Thank you in advance

jscheuer1
11-11-2010, 10:48 AM
That's not pure PHP code. It's PHP and HTML. The font tag is deprecated in some HTML, discouraged in all. Why use it in code that is output by PHP? Why use echo to output an entire table? Good PHP practice is to drop out of PHP, write the table in ordinary HTML, then resume PHP:


<?php
//some preparatory PHP stuff here
?>
<table>
<tr>
<td></td>
</tr>
</table>
<?php
//continue on with more PHP
?>

That way both single and double quotes (if either are used) will maintain their literal meaning in the HTML code without needing to be escaped. If you need something resolved from PHP, you may put it inline in the table:


<?php
//some preparatory PHP stuff here
?>
<table>
<tr>
<td><?php echo $myvar; ?></td>
</tr>
</table>
<?php
//continue on with more PHP
?>

That's assuming that $myvar was previously defined.

Anyways, the final determination of the font used in the table are its td's, and the font should be set in an external stylesheet, so you could have:


<?php
//some preparatory PHP stuff here
?>
<table class="myclass">
<tr>
<td><?php echo $myvar; ?></td>
</tr>
</table>
<?php
//continue on with more PHP
?>

And for the font you want, this rule should be in the page's external stylesheet:


table.myclass td {
font: normal 48px arial, sans-serif;
}

fileserverdirect
11-12-2010, 06:51 PM
If you still want to use your code, be sure to escape:


echo "<font size=\"18\" face='Arial'>";
echo "<table>";

//and at the lower part i close it

echo "</table>";
echo "</font>";
This would only fix the php error btw.
This wont change the font size, and it is deprecated anyways, use CSS! (see jscheuer's post.)

james438
11-12-2010, 08:44 PM
Sometimes there are some rather dynamic tables, which I use php to generate and display. For example display certain user stats, but only display the table when the user is logged in and if the user is of such a rank then generate a heavily abridged table with different columns. For this reason I sometimes do not see a way to easily escape out of php to dynamically generate the HTML.

in jscheuer's line font: normal 48px arial, sans-serif; the 48px is the size equivalent of font size = "18". sans-serif is a backup font in case the visitor's browser does not recognize arial. It is a good idea and common practice to have a secondary font available.