by Kevin Yank of SitePoint.com
Part 3: Getting Started with PHP
Last week, we learned how to use the MySQL database engine to store a list of jokes in a simple database (composed of a single table named Jokes
). To do so, we used the MySQL command line client to enter SQL commands (queries). This week, we'll introduce the PHP server-side scripting language. In addition to the basic features we'll be looking at this week, this language has full support for communicating with MySQL databases.
Presenting PHP
As we've discussed previously, PHP is a server-side scripting language. This concept is not obvious, especially if you're just used to designing pages with HTML and JavaScript. A server-side scripting language is similar to JavaScript in many ways, as they both allow you to embed little programs (scripts) into the HTML of a Web page. In executing, such scripts allow you to control what will actually appear in the browser window in some way more flexible that what is possible using straight HTML.
The key difference between JavaScript and PHP is that, while the Web browser interprets JavaScript once the Web page containing the script has been downloaded, server-side scripting languages like PHP are interpreted by the Web server before the page is even sent to the browser. Once interpreted, the PHP code is replaced in the Web page by the results of the script, so all the browser sees is a standard HTML file. The script is processed entirely by the server. Thus the designation: server-side scripting language.
Let's look back at the today.php
example presented in Part One:
<HTML>
<HEAD>
<TITLE>Today's Date</TITLE>
</HEAD>
<BODY>
<P>Today's Date (according to this Web server) is
<?php
echo( date("l, F dS Y.") );
?>
</BODY>
</HTML>
Most of this is plain HTML. The line between <?php
and ?>
, however, is written in PHP. <?php
means "begin PHP code", and ?>
means "end PHP code". The Web server is asked to interpret everything between these two delimiters and convert it to regular HTML code before sending the Web page to a browser that requests it. The browser is presented with something like this:
<HTML>
<HEAD>
<TITLE>Today's Date</TITLE>
</HEAD>
<BODY>
<P>Today's Date (according to this Web server) is
Wednesday, June 7th 2000.</BODY>
</HTML>
Notice that all signs of the PHP code have disappeared. In their place, the output of the script has appeared and looks just like standard HTML. This example demonstrates several advantages of server-side scripting:
Basic Syntax and Commands |
SitePoint.com is a fast growing Web Developer Community. Kevin Yank is the Editor of the SitePoint TechTimes, a fresh, technically oriented newsletter for the serious Webmaster. |