Changes

Jump to: navigation, search

Creating a Simple PHP Script

1,777 bytes added, 14:18, 24 May 2007
Emdedding PHP into an HTML File
== Emdedding PHP into an HTML File ==
 
As you may have realized, by testing the PHP module on your web server you have already crated your first PHP script. We will no go on to create another script that is embedded into an HTML page. Open your editor and create the following HTML file:
 
<pre>
 
<html>
<head>
<?php
echo '<title>My First PHP Script</title>';
?>
</head>
<body>
<?php
echo '<p>This content was generated by PHP</p>';
?>
<p>And this content is static HTML</p>
</body>
</html>
 
</pre>
 
Save this file as example.php and upload it to your web server. When you load this page into your browser you should see something like the following:
 
[[Image:]]
 
Of you see something like the above in your browser then you have succesfully created and executed your first embedded HTML script.
 
== Embedding HTML into a PHP Script ==
 
In the previous example we embedded some PHP scripts into an HTML web page. We can reverse this by putting the HTML tags into the PHP commands. The following example contains a PHP script which is designed to output the HTML necessary to display a simple page. As with the previous examples, create this file, uplaod it to your web server and load it into a web browser:
 
<?php
echo "<html>\n";
echo "<head>\n";
echo "<title>My Second PHP Example</title>\n";
echo "</head>\n";
echo "<body>\n";
echo "<p>Some Content</p>\n";
echo "</body>\n";
echo "</html>\n";
?>
 
 
When you laod this into your browser it will be as if all that was in the file was HTML, and if you use the ''view source'' feature of your browser the HTML is, infact, all you will see because the PHP pre-processor simply created the HTML it was tolf to create when it executed the script:
 
<pre>
<html>
<head>
<title>My Second PHP Example</title>
</head>
<body>
<p>Some Content</p>
</body>
</html>
</pre>

Navigation menu