PHP Lesson 1
PHP Lesson 1
PHP is mostly known for dynamically generating Web site pages in conjunction with a Web server, such as Apache. However, because Lessons 1, 2 and 3 will concentrate on the three basic programming logical constructs of sequence, selection and repetition, you will, initially be using PHP in a Command Line environment only. This minimises overheads in your learning curve of basic programming skills. Then, later Lessons will all be about using PHP, in conjunction with Apache Web Server to generate dynamic web pages that interact with a Web sites database. PHP on the command line To run PHP programs (or scripts, as they are often called) on the command line, the two files php.exe and php5ts.dll are required. Your teacher will provide these. A PHP program is just a text file created with Notepad or similar text editor. Use php as the extension of any PHP program file name, eg, myprogram.php.
Enter the CLI (Command Line Interface) Now run this script using C:\php\php cli.php You should get an output of "Testing PHP CLI". Congratulations, you've made your first PHP CLI script work!
*** Note: instead of echo you can also use print for outputting from a PHP script ***
FM 26/07/09
<?php echo "Testing PHP CLI. Type in your name"; print "\n"; $input = fgets(STDIN); // Simple intro statement called a PROMPT // This newline is so the cursor is on the next line // Here we are getting input from // the STandarD INput stream, this is where the // user enters input // After getting input, we write it to the // STandarD OUTput stream, so you can see it in //your Command Prompt
Now run this script. The result is (with input in bold); Testing PHP CLI. Type in your name Cheese How are you Cheese ? This brings up a question. Why did the ? go on the next line? This is because when you were typing in your name, you pressed 'Enter' after finishing. This sent a newline character (\n) to your input stream. So when we output your name, we also output the \n, causing anything after it to move to the next line. Here is a simple remedy for that.
<?php echo "Testing PHP CLI. Type in your name"; print "\n"; $input = trim(fgets(STDIN)); // Notice the use of the trim() function to // get rid of the newline character in the input // After getting input, we write it to the //STandarD OUTput stream, so you can see it in //your Command Prompt
Now our output will come out exactly as we want it to. Programming concepts PHP on the command line 2 FM 26/07/09
If your program does not run properly, check the syntax, especially the following; the ';' at the end of each line (but not required for comment lines) matching quotes Great! !...your first successful PHP program. Generally, you would not use as many comments as there are in the above program. Nevertheless, it is good programming practice to comment blocks of code or even individual lines. You can and should use `blank lines' to make your code more `readable'. Blank lines, like the //comment lines are not used by PHP.
Questions to consider Are there any `bugs' in the above program? Bugs can be of two types - syntax bugs or logic bugs. You must be able to fix both types. What bugs were there in the above code when you first tried to run it? Did you get the correct answer? How would you want to modify the program?
FM 26/07/09
Exercise 1.2 A program is required which will calculate the total price of 3 items purchased at the local supermarket. Input to the program is via the keyboard (with suitable prompts) and includes: Price for item 1 Price for item 2 Price for item 3 The output is to be displayed on the monitor with suitable messages that must include: Total Price (the sum of the three prices) The program should be user friendly and include a greeting and farewell message and comments.
Exercise 1.3 A program is required which will calculate the total marks for a student's two assessment tasks And the average mark for the two assessments. Input to the program is via the keyboard (with suitable prompts) and includes: Student Name Mark for assignment 1 Mark for assignment 2 The output is to be displayed on the monitor with suitable messages that must include: Total Mark (the sum of the two assessments) The average mark (average of the two assessments) The program should be user friendly and include a greeting and farewell message and comments.
Exercise 1.4 A program is required which will calculate the total amount owed by a customer at the end of a month. Input to the program is via the keyboard (with suitable prompts) and includes: Customer Name Amount owed at the beginning of the month The total value of purchases during the month The total repayments made during the month
There is also a monthly account keeping fee that is added to the customer's total amount owing. This fee is 10% of all transactions. account keeping fee = 0.1 *(payments + purchases) The program will calculate the amount still owed by the customer at the end of the month by: Subtracting the value of total payments Adding the value of total purchases Adding the account keeping fee
to the amount owed at the beginning of the month. The output is to be displayed on the monitor with suitable messages that must include: The cost of the account keeping fee The total amount still owed at the end of the month
All customer details are to remain on the screen during processing. The layout of details on the screen is to be of commercial nature, suitable for printing as a invoice. The program should be user friendly and include a greeting and farewell message and comments.
FM 26/07/09