SYST 28043 Web Technologies Intro to PHP PHP
SYST 28043 Web Technologies Intro to PHP
PHP PHP: Hypertext Preprocessor A recursive acronym Currently on version 5. 4. 4 http: //www. php. net Developed as a set of “Personal Home page Tools” Rasmus Lerdorf Small scripts to do things like track visitors Eventually became the PHP we know today 11/27/2020 Wendi Jollymore, ACES 2
PHP - Advantages Free, Open Source Fast Runs on most operating systems Easy to learn – has C/Java/Perl type of syntax Loosely-typed Procedure-oriented or object-oriented Powerful string processing and regular expression libraries Very large and active developer community 11/27/2020 Wendi Jollymore, ACES 3
Testing PHP See Notes for this lesson, “Testing PHP” 1. 2. 11/27/2020 Make sure your web server is running! Open a new PHP Project in Aptana Wendi Jollymore, ACES 4
PHP Syntax <? php /* all your php code here */ ? > The <? php ? > tags contain the php code All executing statements end in a semicolon You can put your entire page in the tags You can embed the PHP throughout your XHTML code See examples in the notes 11/27/2020 Wendi Jollymore, ACES 5
PHP Syntax Echo statement Sends output to the page Double-quotes and single-quotes mean different things You can use escape sequences like n 11/27/2020 Wendi Jollymore, ACES 6
PHP Example echo "<h 3>This is a PHP Page by Kaluha</h 3>"; echo "<p>Today I'm beginning to learn some PHP. I'm liking it so far!</p>"; echo "<hr>"; echo "<p>Email me at <a href="mailto: kaluha@cat. com"> kaluha@cat. com</a>! </p>"; 11/27/2020 Wendi Jollymore, ACES 7
PHP Example Load the page in your browser View the browser source! Use the n sequence to break up long lines Makes browser source easier to read Add the n to your code statements and reload 11/27/2020 Wendi Jollymore, ACES 8
PHP Example Add this: echo "<p>Current Date and Time: "; echo date("l j. S of F Y h: i: s A"); echo "</p>n"; Do the date() exercise in the notes 11/27/2020 Wendi Jollymore, ACES 9
PHP Commenting Single-line comments can start with // or # # this is a comment // this is a comment Multi-line comments can be enclosed in /* and */ /* this is a multiline comment. */ 11/27/2020 Wendi Jollymore, ACES 10
Identifiers Rules for Identifiers: identifier names are case-sensitive identifiers must start with a letter or underscore character identifier names may not contain spaces identifiers must be made up of letters and numbers and symbols from ASCII 127 to ASCII 255 11/27/2020 Wendi Jollymore, ACES 11
Variables Called “scalars” You don’t have to declare variables But you can To declare a variable, just initialize it: $user. Name=""; $date. Flag = 1; 11/27/2020 Wendi Jollymore, ACES 12
Variables Scalars Variables that hold a single value Always starts with a $ Value can be one of: Boolean True or false False = 0; any other integer is true Integer Floating-point string 11/27/2020 Wendi Jollymore, ACES 13
Constants are declared using the define() method: define(“PST_RATE”, . 08); Defines a constant called PST_RATE with an initial value of. 08 Use it just like you would use any other scalar: $pst. Amt = $sub. Total * PST_RATE; 11/27/2020 Wendi Jollymore, ACES 14
Escape Sequences b f n r t ’ ” \ 11/27/2020 backspace Form feed New-line Carriage return Tab Single-quote Double-quote Backslash Wendi Jollymore, ACES 15
Operators Arithmetic Operators + * / % addition subtraction multiplication division modulus Pre- and post- unary operators: ++ -11/27/2020 unary increment unary decrement Wendi Jollymore, ACES 16
Operators Assignment Operators = += -= *= /= %= 11/27/2020 equals plus-equals minus-equals multiply-equals divide-equals modulus-equals Wendi Jollymore, ACES 17
Operators Relational Operators == != > >= < <= equal to not equal to greater than or equal to less than or equal to Logical Operators &&, AND ||, OR ! , NOT 11/27/2020 AND Operator OR Operator NOT Operator Wendi Jollymore, ACES 18
Operators String Operators. . = Concatenation operator Assignment-Concatenation Examples: echo “Value: “. $value. ”</p>n”; $value. = $new. Value; 11/27/2020 Wendi Jollymore, ACES 19
Operators Conditional Operator condition ? ret. Value. True : ret. Value. False; If condition is true, ret. Value. True is returned If condition is false, ret. Value. False is returned Example: $valid. Num = ($int. Value > 0) ? “valid” : “invalid”; 11/27/2020 Wendi Jollymore, ACES 20
String Interpolation Double-Quotes: Variables and escape sequences are parsed Example: $cat. Name = "Mr. Bibs"; echo "Wendi's cat's name is $cat. Name. n. This is a new line. "; Output: Wendi's cat's name is Mr. Bibs. This is a new line. Browser Source: Wendi's cat's name is Mr. Bibs. This is a new line. 11/27/2020 Wendi Jollymore, ACES 21
String Interpolation Single-Quotes: Variables and escape sequences are not interpreted, except where it might cause a syntax error Example: echo 'My variable is $cat. Name and it's n case-sensitive. '; Output: My variable is $cat. Name and it's n casesensitive. 11/27/2020 Wendi Jollymore, ACES 22
String Interpolation Heredoc: Using labels to mark larger passages of text Content between labels is interpreted as if in double-quotes But you don’t have to escape double-quotes Starting and ending labels must match Labels must be in upper-case Starting label must be preceeded by <<< Ending label must be first on a blank line Not even spaces in front of it 11/27/2020 Wendi Jollymore, ACES 23
String Interpolation Heredoc Example: <? php $website = "http: //www. thinkgeek. com"; echo <<<GEEK <p>One of my favourite web sites is <a href = "$website">Thinkgeek. com</a>. This is a great site for geeks and coders because they have lots of caffeine products (drinks, candy, coffee mugs, etc) and lots of what they refer to as <i>Cube Goodies</i>. Cube goodies are little toys that keep you amused in your office cube. </p> GEEK; ? > 11/27/2020 Wendi Jollymore, ACES 24
Control Structures If-Statements if (condition){ // code body } else { // code body } 11/27/2020 Wendi Jollymore, ACES 25
Exercise Do the Tip Calculator exercise in the notes Pass the input values using GET method name=value pairs E. g. localhost/webtech/projet. Name/ex 1. p hp? bill. Amt=35. 55&tip. Percent=15 11/27/2020 Wendi Jollymore, ACES 26
Iteration Pre-Test and Post-Test Loop while(condition) { // code body } do { // code body } while (condition); 11/27/2020 Wendi Jollymore, ACES 27
Iteration For Loops: for (init; condition; cont) { // code body } foreach (item as collection. Type) { // code body } 11/27/2020 Wendi Jollymore, ACES 28
Exercises Do the Multiplication Table exercise in the notes 11/27/2020 Wendi Jollymore, ACES 29
Arrays in PHP work similarly to arrays in other languages Arrays are 0 -based by default Array elements are identified with indexes or subscripts 11/27/2020 Wendi Jollymore, ACES 30
Creating Arrays Using the array() function: $array. Name = array(value 1, value 2, value 3, …); Example: $languages = array(“Java”, “PHP”, “Perl”, “C#”, “Visual Basic”); Creates an array called $languages with 5 elements (indexed 0 to 4) 11/27/2020 Wendi Jollymore, ACES 31
Creating Arrays Hard coding the arrays $languages[0] = “Java”; $languages[1] = “PHP”; … You can actually do this without the indexes: $languages[] = “Java”; $languages[] = “PHP”; Indexes will default 0, 1, 2. . 11/27/2020 Wendi Jollymore, ACES 32
Iterating Through Arrays Foreach loop: foreach($array. Name as $array. Element) { // code } Example: foreach($languages as $a. Lang) { echo $a. Lang. ” ”; } 11/27/2020 Wendi Jollymore, ACES 33
Length of Arrays count() function: foreach($grades as $g) { $total. Grade += $g; } $avg = $total. Grade / count($grades); echo "Average Grade: ". $avg; 11/27/2020 Wendi Jollymore, ACES 34
Associative Arrays Elements consist of a key and value pair The key is the index The value is the content of the array element Keys must be unique Keys can be strings E. g. $grades[“prog 10082”] = 75; 11/27/2020 Wendi Jollymore, ACES 35
Associative Arrays Creating an associative array: $my. Grades = array("PROG 10082" => 89. 5, "PROG 24178" => 85. 0, "INFO 16029" => 91. 5, "SYST 13416" => 80. 5); $my. Grades array contains 4 elements 11/27/2020 “PROG 10082” 89. 5 “PROG 24178” 85. 0 “INFO 16029” 91. 5 “SYST 13416” 80. 5 Wendi Jollymore, ACES 36
Associative Arrays Using foreach() with associative arrays: foreach(array_expr as $key => $value) { // statements } Example: echo "<p>My Grades: "; foreach($my. Grades as $course => $mark) { echo “<p><b>$course: </b>”; echo “$mark n”; } echo "</p>n"; 11/27/2020 Wendi Jollymore, ACES 37
Adding Array Elements Arrays in PHP are dynamic The number of elements can change while the program executes You can add elements to an array during run-time: $languages[] = “Delphi”; $my. Grades[“SYST 28043”] = 79. 1; 11/27/2020 Wendi Jollymore, ACES 38
Adding Array Elements array_push($array. Name, $element) function Adds an element to the end of an array You can add multiple elements: array_push($array. Name, $el 1, $el 2, $el 3); array_unshift($array. Name, $element) function Adds an element to the beginning of an array You can add multiple elements just as you can with array_push($languages, “Delphi”, “C++”); array_unshift($languages, “COBOL”); 11/27/2020 Wendi Jollymore, ACES 39
Removing Array Elements $element = array_pop($array. Name) function Removes the last element and returns it $element = array_shift($array. Name) function Removes the first element and returns it $last. El = array_pop($languages); $first. El = array_shift($languages); 11/27/2020 Wendi Jollymore, ACES 40
Array Exercises Do the array exercises in the notes. 11/27/2020 Wendi Jollymore, ACES 41
Next Class: Functions Variable Scope Form Processing Form Validation 11/27/2020 Wendi Jollymore, ACES 42
- Slides: 42