There are a few places where to place your PHP code on your dynamic web page. This all depends on your needs and preference. Of course there is a slight difference to whether you put it on a script tag, before any HTML, within your HTML, or at the bottom of the page. Here I will explain what I have found some of the differences to be.
Before HTML
A few of the reasons you would want to place your code before the HTML are because you want to redirect the user to another page on your website, or getting data out of a database and placing it on your page as content.
<?php $pageTitle = "Cesar's page"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $pageTitle; ?></title> </head> <body> ...
In the script tag
Yes! Just like Javascript you can place your PHP code within a script tag, but also like Javascript gets executed after your page loads. Though I have not seen many people using this method I think it really helps you keep your code clean and organized. It is similar to the .NET code placement.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Web Page</title> <script language="php"> $pageContent = "Cesar's page content."; </script> </head> <body> <?php echo $pageContent; ?> </body> </html>
Within HTML
Mixing HTML and PHP is probably the most common way PHP applications are made. As useful and conventional as this type of coding can be, it can also become confusing. Below is an example of how that looks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Web Page</title> </head> <body> <?php $HeadDiv = "<div><h1>Welcome to my website!</h1></div>"; $BodyDiv = "<div><p>Are you ready to hire me?! Click on my port...</p></div>"; $FooterDiv = "<div>copyright ...</div>"; ?> <?php echo $HeadDiv; echo $BodyDiv; echo $FooterDiv; ?> </body> </html>
After HTML
This might be an unorthodox way of code execution but nonetheless it is a possibility and depending on your application needs, it might be a useful one. Your code will execute after the HTML so anything that would be rendered above your HTML will render below it (afterwards) now. Here is how that looks. Please comment if you have an idea of how it would be useful.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My WebApp</title> </head> <body> <form action="/code/untitled.php" method="post"> <input type="text" name="txtMessage" /> <input type="submit" value="submit" /> </form> </body> </html> <?php echo $_POST['txtMessage'];?>


Wed, Oct 29, 2008
Code, PHP