Programming

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.

Write a comment about “PHP”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.