How to run a multiline PHP script in HTML page?
I am trying to wrap my head around php and html, and wanted to ask some
help with what I guess are basics.
I am aware that you can run a php script on a single line in a .html page
with tags. The problem comes when I have a longer script on multiple lines
(It generates a table) that I run from localhost:
<?php
$rows = 10; // define number of rows
$cols = 10;// define number of columns
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>Fill</td>";
}
echo "</tr>";
}
echo "</table>";
?>
The above will come out as plain text after first ">" sign, so I guess for
some reason the page reads it as I close the php snippet and executes rest
as plain HTML (I run the file through apache). I am aware that you can
rename the file to .php instead, and get whole thing working, but I want
to understand know how PHP behave on HTML pages.
So I have two questions:
1) Is it possible to make the .html read the above code snippet without
breaking it apart? I've tried htmlspecialchars, didn't help.
2) Is it possible to have the above snippet as a separate .php file, that
I call from .html page like I can call a javascript function? I've tried
which didn't work. I've looked into .htaccess, but it seems that's a
solution for live versions, which didn't work for me.
Thank you!
No comments:
Post a Comment