I know there is a way to use html and css tags inside php code.
I just dont know the right way of doing it. I remember it goes something like using / before or after the "
<?php
echo "<table>";
echo "<tr>";
echo "<td style=/"width: 50px"/>some text</td>";
echo "</tr>";
echo "</table>";
?>
Can someone tell me the right way please?
php and html, css tags
Started by
HEki_
, Jan 26 2012 02:50 PM
5 replies to this topic
#1
Posted 26 January 2012 - 02:50 PM
#2
Posted 26 January 2012 - 08:07 PM
could try:
<?php echo <<<EOF <html> <!-- more html --> </html> EOF; ?>
- 1P0 likes this
#3
Posted 26 January 2012 - 11:34 PM
There's several ways to handle it, depending on what the rest of the page looks like.
You can use the heredoc like what Derek posted.
You can flip it around and do an HTML page with embedded PHP:
You can do what you did but use single quotes instead of double, so you do not have to escape your double quotes in the HTML:
If you wondering how to do it exactly like you have, you escape the double quotes with a backslash, not a forward slash.
You can use the heredoc like what Derek posted.
You can flip it around and do an HTML page with embedded PHP:
<table> <tr> <td <?php echo 'style="width:50px" ?>>some text</td> </tr> </table>
You can do what you did but use single quotes instead of double, so you do not have to escape your double quotes in the HTML:
<?php echo '<table>'; echo '<tr>'; echo '<td style="width: 50px">some text</td>'; echo '</tr>'; echo '</table>'; ?>
If you wondering how to do it exactly like you have, you escape the double quotes with a backslash, not a forward slash.
<?php echo "<table>"; echo "<tr>"; echo "<td style=\"width: 50px\">some text</td>"; echo "</tr>"; echo "</table>"; ?>
Edited by Hayden, 26 January 2012 - 11:39 PM.
- 1P0 likes this
#4
Posted 27 January 2012 - 01:09 PM
PHP is a templating technology initially designed to add programmatic code inside html.
So you can just start your file with html or close the php code with a ?> even inside a php function write your html+css and then resume php code with <?php as shown here:
<table>
<tr>
<td style="width: 50px"/><?php echo $myStringVarible; ?></td>
</tr>
</table>
and if you have a pure html file called mypurehtml.php with or without some little php inside you can always do like
function LoadHtml() {
require('mypurehtml.php');
}
and inside that mypurehtml.php file, you will have access to LoadHtml function local variables.
So you can just start your file with html or close the php code with a ?> even inside a php function write your html+css and then resume php code with <?php as shown here:
<table>
<tr>
<td style="width: 50px"/><?php echo $myStringVarible; ?></td>
</tr>
</table>
and if you have a pure html file called mypurehtml.php with or without some little php inside you can always do like
function LoadHtml() {
require('mypurehtml.php');
}
and inside that mypurehtml.php file, you will have access to LoadHtml function local variables.
#5
Posted 28 January 2012 - 12:46 PM
I often open and close my PHP as need in my HTML code, depending on the situation. I sometimes end up with something like:
This will effectively output a table with several rows saying "Hello world".
There is a super negligible performance decrease when opening and closing tags like that.
[/code]
<table> <?php while($c < 5){ ?> <tr><td>Hello world</td></tr> <?php $c++ } ?> </table>
This will effectively output a table with several rows saying "Hello world".
There is a super negligible performance decrease when opening and closing tags like that.
[/code]
#6
Posted 29 January 2012 - 03:55 PM
I often open and close my PHP as need in my HTML code, depending on the situation. I sometimes end up with something like:
<table> <?php while($c < 5){ ?> <tr><td>Hello world</td></tr> <?php $c++ } ?> </table>
This will effectively output a table with several rows saying "Hello world".
There is a super negligible performance decrease when opening and closing tags like that.
You could do something like this as well.
<table> <?php while($c < 5): ?> <tr><td>Hello world</td></tr> <?php $c++; ?> <?php endwhile; ?> </table>
I do not believe there are any performance differences but IMO it's good code formatting.
- 1P0 likes this
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users