Recent Comments

PHP

PHP is a language that is used on web servers to merge web page output with optional content or results from a database or user input. Web pages by themselves are static, but intelligence placed on the web server through programs (like PHP) allow a web page to change based on data it gets from the user or from other sources.

PHP is not a stand-alone language, it requires a web server like Apache or IIS to be running to send it the user input. However, it is free to download, and a simple installation includes the Apache web server (also free). When you install this, it runs a small web server on your computer that you can connect to with a local browser, or with other computers on your home or local network.

Download software:

Windows: http://www.php.net/downloads.php

Mac: http://www.ampps.com/downloads

Cost: Free

Instructions to setup software: After you download the file for your computer, run it. This will install an Apache web server on your computer along with the PHP engine. Other web-server scripting languages (Python and Perl) are also installed, along with a database.

Hardware requirements: Any PC or laptop computer

Operating systems supported: Win, Mac OS X, Linux

Difficulty level: 4 out of 5


TRY IT!

1. Install the PHP engine on your computer. It will need a web server processor, so it the installation usually includes a version of Apache. After downloading the file, run it and follow the directions. It is OK to use the default location for files. Finally, follow instructions to start the server, since it needs to be running to listen to page requests from a browser.

2. Test the installation by opening a browser and pointing it to the local web server. Enter the URL: http://127.0.0.1 into the browser address. This is known as the “loopback” address and will send it to your local computer. If Apache was installed to run on a non-standard port (such as 8080), you might need to add the port number to the URL: http://127.0.0:8080. If you get stuck on this step, jump to step 4 and look at the Apache configuration file.

3. Keep working through step 2 until you see a default or sample page from the installed Apache server.

4. Locate the home directory on your computer where the Apache server is reading files. Wherever Apache was installed, it placed a configuration file called conf\httpd.conf. View this file with notepad and look for a line that starts with DocumentRoot. This will identify the home directory for the server. If you were stuck on step 2, this file also sets the port number with the line called Listen. The default is port 80, but it can be changed to a different number.

5. Create a file in the DocumentRoot directory called mytest.php and open it with a text editor (notepad, textedit, or your favorite). Copy/paste the following program into it:

<html>
 <head>
  <title>My Test</title>
 </head>
 <body>

 <?php 
     $now = localtime(time(), true);
     if ($now[tm_hour] < 12)
     {
        echo '<p>Good Morning!</p>';
     }
     else
     {
        echo '<p>Good Afternoon!</p>';
     }
?> 

 </body>
</html>

6. Save the file.

7. Open a browser window and point the URL to your new PHP program file. Enter the URL as: http://127.0.0.1/mytest.php


TRY THIS

1. Writing a PHP program is like merging two different languages in the same file: HTML and PHP. Try adding just some HTML changes to the file, such as a few words before or after the PHP block to see what happens.

2. Change the time of day test to a different time, such as 18 (which is 6 pm), and change the message to say Good Evening.

3. Add another echo command that runs regardless of the time that shows the current time of day.

4. Save the time at the beginning of the code block and at the end of the code block using the time function gettimeofday(TRUE) which returns a floating point number. Subtract the time at the end of the code block from the time at front code block and echo the output. This will show how long it took the code to run.

5. (Advanced) Add the following loop to the program to see how it affects the time. Change the looping count to see how it affects the run time.

for ($i=0; $i<5000; $i++) 
{
     echo "counting: ".$i;
}

LEARN MORE

http://www.php.net/manual/en/ — The official documentation site for PHP.

http://www.w3schools.com/php/ — A step-by-step tutorial site that contains lots of useful sample code.

Google.com –- Search for other code and samples.


RESOURCES, TIPS, TRICKS AND HINTS

Scripting languages do not need a compiler, so you can quickly write and test programs. For writing and testing web page programs, it is handy to keep the file open in an editor like Notepad and make small changes to it. Then keep a browser window open and loaded with the same page through the localhost (127.0.0.1) address URL. You can save the file in the Notepad window after each update and click “refresh” on the browser window. This allows you to instantly see the effect of small additions.

Leave a Comment

Please don't use your real name.