PHP Programming Slide # 2

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

The Web Wizard’s Guide to PHP by David A. Lash:

Copyright © 2003 Pearson Education, Inc. Slide 2- 1 The Web Wizard’s Guide to PHP by David A. Lash

Slide 2:

Copyright © 2003 Pearson Education, Inc. Slide 2- 2 CHAPTER 2 Using Variables

Objectives :

Copyright © 2003 Pearson Education, Inc. Objectives To learn how to store and access data in PHP variables To understand how to create and manipulate numeric and string variables To review how to create HTML input forms To learn how to pass data from HTML forms to PHP scripts

Using PHP Variables:

Copyright © 2003 Pearson Education, Inc. Slide 2- 4 Using PHP Variables Variables are used to store and access data in computer memory. A variable name is a label used within a script to refer to the data.

Assigning New Values to Variables:

Copyright © 2003 Pearson Education, Inc. Slide 2- 5 Assigning New Values to Variables You can assign new values to variables: $days = 3; $newdays = 100; $days = $newdays; At the end of these three lines, $days and $newdays both have values of 100.

Selecting Variable Names:

Copyright © 2003 Pearson Education, Inc. Slide 2- 6 Selecting Variable Names You can select just about any set of characters for a variable name in PHP, but they must: Use a dollar sign ($) as the first character Use a letter or an underscore character (_) as the second character. Note: Use short, descriptive variable names, and be consistent (i.e. $userId/$userName or $user_id/$user_name)

Combining Variables and the print Statement:

Copyright © 2003 Pearson Education, Inc. Slide 2- 7 Combining Variables and the print Statement To print out the value of $x: print ("$x"); - or - Print $x; The following code will output “Bryant is 6 years old”. $age=6; print ("Bryant is $age years old.");

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 8 A Full Example ... 1. <html> 2. <head> <title>Variable Example </title> </head> 3. <body> 4. <?php 5. $first_num = 12; 6. $second_num = 356; 7. $temp = $first_num; 8. $first_num = $second_num; 9. $second_num = $temp; 10. print ("first_num= $first_num <br> second_num=$second_num"); 11. ?> </body> </html>

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 9 A Full Example ... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/firstnum.php

Using Arithmetic Operators:

Copyright © 2003 Pearson Education, Inc. Slide 2- 10 Using Arithmetic Operators You can use operators such as a plus sign (+) for addition and a minus sign (–) for subtraction to build mathematical expressions. For example <?php $apples = 12; $oranges = 14; $total_fruit = $apples + $oranges; print ("The total number of fruit is $total_fruit"); ?> These PHP statements would output “The total number of fruit is 26.”

Common PHP Numeric Operators:

Copyright © 2003 Pearson Education, Inc. Slide 2- 11 Common PHP Numeric Operators

A Full Example:

Copyright © 2003 Pearson Education, Inc. Slide 2- 12 A Full Example 1. <html> 2. <head> <title>Variable Example </title> </head> 3. <body> 4. <?php 5. $columns = 20; 6. $rows = 12; 7. $total_seats = $rows * $columns; 8. 9. $ticket_cost = 3.75; 10. $total_revenue = $total_seats * $ticket_cost; 11. 12. $building_cost = 300; 13. $profit = $total_revenue - $building_cost; 14. 15. print ("Total Seats are $total_seats <br>"); 16. print ("Total Revenue is $total_revenue <br>"); 17. print ("Total Profit is $profit"); 18. ?> </body> </html>

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 13 A Full Example ... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/numops.php

WARNING: Using Variables with Undefined Values:

Copyright © 2003 Pearson Education, Inc. Slide 2- 14 WARNING: Using Variables with Undefined Values If you accidentally use a variable that does not have a value assigned to it will have no value (called a null value).When a variable with a null value is used in an expression PHP, PHP may not generate an error and may complete the expression evaluation. For example, the following PHP script will output x= y=4. <?php $y = 3; $y=$y + $x + 1; // $x has a null value print ("x=$x y=$y"); ?>

Writing Complex Expressions:

Copyright © 2003 Pearson Education, Inc. Slide 2- 15 Writing Complex Expressions Operator precedence rules define the order in which the operators are evaluated. For example, $x = 5 + 2 * 6; The value of $x is either 42 or 17 depending on order of evaluation. Since multiplication evaluated before addition operations, this expression evaluates to 17.

PHP Precedence Rules:

Copyright © 2003 Pearson Education, Inc. Slide 2- 16 PHP Precedence Rules PHP follows the precedence rules listed below. First it evaluates operators within parentheses. Next it evaluates multiplication and division operators. Finally it evaluates addition and subtraction operators.

PHP Precedence Rules:

Copyright © 2003 Pearson Education, Inc. Slide 2- 17 PHP Precedence Rules For example, the first 2 statements evaluate to 80 while the last to 180. $x = 100 - 10 * 2; $y = 100 - (10 * 2); $z = (100 - 10) * 2;

A Full Example:

Copyright © 2003 Pearson Education, Inc. Slide 2- 18 A Full Example 1. <html> 2. <head> <title>Expression Example </title> </head> 3. <body> 4. <?php 5. $grade1 = 50; 6. $grade2 = 100; 7. $grade3 = 75; 8. $average = ($grade1 + $grade2 + $grade3) / 3; 9. print ("The average is $average"); 10. ?> </body> </html>

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 19 A Full Example ... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/complex1.php

Working with PHP String Variables:

Copyright © 2003 Pearson Education, Inc. Slide 2- 20 Working with PHP String Variables Character strings are used in scripts to hold data such as customer names, addresses, product names, and descriptions. Consider the following example. $name="Christopher"; $preference="Milk Shake"; $name is assigned “Christopher” and the variable $preference is assigned “Milk Shake”.

WARNING: Be Careful Not to Mix Variable Types:

Copyright © 2003 Pearson Education, Inc. Slide 2- 21 WARNING: Be Careful Not to Mix Variable Types Be careful not to mix string and numeric variable types. For example, you might expect the following statements to generate an error message, but they will not. Instead, they will output “ y=1 ”. <?php $x ="banana"; $sum = 1 + $x; print ("y=$sum"); ?>

Using the Concatenate Operator:

Copyright © 2003 Pearson Education, Inc. Slide 2- 22 Using the Concatenate Operator The concatenate operator combines two separate string variables into one. For example, $fullname = $firstname . $lastname; $fullname will receive the string values of $firstname and $lastname connected together. For example, $firstname = "John"; $lastname = "Smith"; $fullname = $firstname . $lastname; print ("Fullname=$fullname");

TIP; An Easier Way to Concatenate Strings:

Copyright © 2003 Pearson Education, Inc. Slide 2- 23 TIP; An Easier Way to Concatenate Strings You can also use double quotation marks to create concatenation directly, For example, $Fullname2 = "$FirstName $LastName"; This statement has the same effect as $Fullname2 = $FirstName . " " . $LastName;

Creating HTML Input Forms:

Copyright © 2003 Pearson Education, Inc. Slide 2- 24 HTML Forms and not part of PHP language but important way to send data to scripts Creating HTML Input Forms Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button

Starting And Ending HTML Forms:

Copyright © 2003 Pearson Education, Inc. Slide 2- 25 Starting And Ending HTML Forms You can create HTML forms by using the HTML <form> and </form> tags.

Creating Form Buttons:

Copyright © 2003 Pearson Education, Inc. Slide 2- 26 Creating Form Buttons You can create submit and reset buttons by placing the following within <form> & </form> tags. The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase and Restart”.

Another Full Script Example:

Copyright © 2003 Pearson Education, Inc. Slide 2- 27 Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 4.<form action="http://webwizard.aw.com/~phppgm/First.php" method="post" > 5. Click submit to start our initial PHP program. 6. <br> <input type="submit" value="Click To Submit"> 7. <input type="reset" value="Erase and Restart"> 8. </form> 9. </body> </html>

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 28 A Full Example ... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/form1.html

Warning: Password Boxes Not Secure:

Copyright © 2003 Pearson Education, Inc. Slide 2- 29 Warning: Password Boxes Not Secure When the user submits the form, any data input is sent in clear text (nonencrypted) just like any other HTML form field. Someone with network access could, therefore, read the password being transferred. For this reason, most Web applications do not use this approach to receive and transmit passwords.

Receiving Form Input into PHP Scripts:

Copyright © 2003 Pearson Education, Inc. Slide 2- 30 Receiving Form Input into PHP Scripts To receive HTML form input into a PHP script: Use the form element’s name argument as an index to the arrays $_GET or $_POST. For example, on a form using the post method: <input type= " radio " name= " contact " value= " Yes " > The form-handling PHP script would then use a variable called $_POST[“contact”]. If the user selected the radio button, $_POST[“contact”] would = “Yes”

From Input:

Copyright © 2003 Pearson Education, Inc. Slide 2- 31 From Input To receive data you use the special variables $_POST or $_GET. $name = $_POST[“name”]; Enclose in square bracket and then quotes Name of HTML form variable (note do not use $) Special PHP Global variable. Technically it is an associative array (covered in chptr 5.) PHP variable name that you want to receive the HTML form input.

Full Example:

Copyright © 2003 Pearson Education, Inc. Slide 2- 32 Full Example On a form using the get method: Enter email address: <input type="text" size="16" maxlength="20" name="email"> May we contact you?: <input type= " radio " name= " contact " value= " Yes " > <input type= " radio " name= " contact " value= “ No " > Input can be received as follows: 1. <html><head><title>Receiving Input</title></head> 2 . <body><font size=5>Thank You: Got Your Input.</font> 3. <?php 4. print(“<br>Your email address is “ . $_GET[“email”] ); 5. print(“<br> Contact Preference is “ . $_GET[“contact”] ); 6. ?>

A Full Example ...:

Copyright © 2003 Pearson Education, Inc. Slide 2- 33 A Full Example ... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/Form4Radio.html

Register_Globals?:

Copyright © 2003 Pearson Education, Inc. Slide 2- 34 Register_Globals? PHP Versions prior to 4.2.1 require a different mechanism to receive input (the default was changed for security reasons). The book refers to form variables simply by prefixing the field name with a dollar sign – THIS IS BAD PRACTICE! (use $_GET, $_POST, etc) Technical Details: The REGISTER_GLOBALS directive was changed from the old default of ON to new default of OFF

PHP Versions < 4.1.0:

Copyright © 2003 Pearson Education, Inc. Slide 2- 35 PHP Versions < 4.1.0 In PHP Versions prior to 4.1.0, use following arrays to get data: $HTTP_GET_VARS for $_GET $HTTP_POST_VARS for $_POST $HTTP_COOKIE_VARS for $_COOKIE, $HTTP_SERVER_VARS for $_SERVER $HTTP_ENV_VARS for $_ENV variables

Summary:

Copyright © 2003 Pearson Education, Inc. Slide 2- 36 Summary Variables are used to store and access data in computer memory. You can associate a value with a variable, change that value, print it out, and perform many different operations on it. PHP supports both numeric and string variables. String variables use different methods for value manipulation (for example, concatenation) than numeric variables do.

Summary:

Copyright © 2003 Pearson Education, Inc. Slide 2- 37 Summary You can use HTML forms to pass data to PHP scripts. HTML form elements include text boxes, text areas, password boxes, check boxes, radio buttons, and selection lists. PHP scripts can receive form element input values by using a special PHP variable matching the type of request (i.e. $_GET or $_POST) with an index equal to the form element’s name argument

authorStream Live Help