Presentation is loading. Please wait.

Presentation is loading. Please wait.

SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP.

Similar presentations

Presentation on theme: "SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP."— Presentation transcript:

1 SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

2 5/7/2015Wendi Jollymore, ACES2 PHP PHP: Hypertext Preprocessor A recursive acronym Currently on version 5.4.4 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

3 5/7/2015Wendi Jollymore, ACES3 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

4 5/7/2015Wendi Jollymore, ACES4 Testing PHP See Notes for this lesson, “Testing PHP” 1. Make sure your web server is running! 2. Open a new PHP Project in Aptana

5 5/7/2015Wendi Jollymore, ACES5 PHP Syntax The tags contain the php code All executing statements end in a semi- colon You can put your entire page in the tags You can embed the PHP throughout your XHTML code See examples in the notes

6 5/7/2015Wendi Jollymore, ACES6 PHP Syntax Echo statement Sends output to the page Double-quotes and single-quotes mean different things You can use escape sequences like \n

7 5/7/2015Wendi Jollymore, ACES7 PHP Example echo " This is a PHP Page by Kaluha "; echo " Today I'm beginning to learn some PHP. I'm liking it so far! "; echo " "; echo " Email me at ! ";

8 5/7/2015Wendi Jollymore, ACES8 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

9 5/7/2015Wendi Jollymore, ACES9 PHP Example Add this: echo " Current Date and Time: "; echo date("l jS \of F Y h:i:s A"); echo " \n"; Do the date() exercise in the notes

10 5/7/2015Wendi Jollymore, ACES10 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 multi- line comment. */

11 5/7/2015Wendi Jollymore, ACES11 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

12 5/7/2015Wendi Jollymore, ACES12 Variables Called “scalars” You don’t have to declare variables But you can To declare a variable, just initialize it: $userName=""; $dateFlag = 1;

13 5/7/2015Wendi Jollymore, ACES13 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

14 5/7/2015Wendi Jollymore, ACES14 Constants 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: $pstAmt = $subTotal * PST_RATE;

15 5/7/2015Wendi Jollymore, ACES15 Escape Sequences \bbackspace \fForm feed \nNew-line \rCarriage return \tTab \’Single-quote \”Double-quote \\Backslash

16 5/7/2015Wendi Jollymore, ACES16 Operators Arithmetic Operators + addition - subtraction *multiplication / division % modulus Pre- and post- unary operators: ++ unary increment -- unary decrement

17 5/7/2015Wendi Jollymore, ACES17 Operators Assignment Operators =equals += plus-equals -= minus-equals *= multiply-equals /= divide-equals %= modulus-equals

18 5/7/2015Wendi Jollymore, ACES18 Operators Relational Operators == equal to != not equal to >greater than >= greater than or equal to { "@context": "", "@type": "ImageObject", "contentUrl": "", "name": "5/7/2015Wendi Jollymore, ACES18 Operators Relational Operators == equal to != not equal to >greater than >= greater than or equal to greater than >= greater than or equal to

19 5/7/2015Wendi Jollymore, ACES19 Operators String Operators. Concatenation operator.=Assignment-Concatenation Examples: echo “Value: “.$value.” \n”; $value.= $newValue;

20 5/7/2015Wendi Jollymore, ACES20 Operators Conditional Operator condition ? retValueTrue : retValueFalse; If condition is true, retValueTrue is returned If condition is false, retValueFalse is returned Example: $validNum = ($intValue > 0) ? “valid” : “invalid”;

21 5/7/2015Wendi Jollymore, ACES21 String Interpolation Double-Quotes: Variables and escape sequences are parsed Example: $catName = "Mr. Bibs"; echo "Wendi's cat's name is $catName.\nThis 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.

22 5/7/2015Wendi Jollymore, ACES22 String Interpolation Single-Quotes: Variables and escape sequences are not interpreted, except where it might cause a syntax error Example: echo 'My variable is $catName and it\'s \n case-sensitive.'; Output: My variable is $catName and it's \n case- sensitive.

23 5/7/2015Wendi Jollymore, ACES23 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

24 5/7/2015Wendi Jollymore, ACES24 String Interpolation Heredoc Example:

25 5/7/2015Wendi Jollymore, ACES25 Control Structures If-Statements if (condition){ // code body } if (condition){ // code body } else { // code body }

26 5/7/2015Wendi Jollymore, ACES26 Exercise Do the Tip Calculator exercise in the notes Pass the input values using GET method name=value pairs E.g. localhost/webtech/projetName/ex1.p hp?billAmt=35.55&tipPercent=15

27 5/7/2015Wendi Jollymore, ACES27 Iteration Pre-Test and Post-Test Loop while(condition) { // code body } do { // code body } while (condition);

28 5/7/2015Wendi Jollymore, ACES28 Iteration For Loops: for (init; condition; cont) { // code body } foreach (item as collectionType) { // code body }

29 5/7/2015Wendi Jollymore, ACES29 Exercises Do the Multiplication Table exercise in the notes

30 5/7/2015Wendi Jollymore, ACES30 Arrays Arrays in PHP work similarly to arrays in other languages Arrays are 0-based by default Array elements are identified with indexes or subscripts

31 5/7/2015Wendi Jollymore, ACES31 Creating Arrays Using the array() function: $arrayName = array(value1, value2, value3, …); Example: $languages = array(“Java”, “PHP”, “Perl”, “C#”, “Visual Basic”); Creates an array called $languages with 5 elements (indexed 0 to 4)

32 5/7/2015Wendi Jollymore, ACES32 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..

33 5/7/2015Wendi Jollymore, ACES33 Iterating Through Arrays Foreach loop: foreach($arrayName as $arrayElement) { // code } Example: foreach($languages as $aLang) { echo $aLang.” ”; }

34 5/7/2015Wendi Jollymore, ACES34 Length of Arrays count() function: foreach($grades as $g) { $totalGrade += $g; } $avg = $totalGrade / count($grades); echo "Average Grade: ".$avg;

35 5/7/2015Wendi Jollymore, ACES35 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[“prog10082”] = 75;

36 5/7/2015Wendi Jollymore, ACES36 Associative Arrays Creating an associative array: $myGrades = array("PROG10082" => 89.5, "PROG24178" => 85.0, "INFO16029" => 91.5, "SYST13416" => 80.5); $myGrades array contains 4 elements “PROG10082”89.5 “PROG24178”85.0 “INFO16029”91.5 “SYST13416”80.5

37 5/7/2015Wendi Jollymore, ACES37 Associative Arrays Using foreach() with associative arrays: foreach(array_expr as $key => $value) { // statements } Example: echo " My Grades: "; foreach($myGrades as $course => $mark) { echo “ $course: ”; echo “$mark \n”; } echo " \n";

38 5/7/2015Wendi Jollymore, ACES38 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”; $myGrades[“SYST28043”] = 79.1;

39 5/7/2015Wendi Jollymore, ACES39 Adding Array Elements array_push($arrayName, $element) function Adds an element to the end of an array You can add multiple elements: array_push($arrayName, $el1, $el2, $el3); array_unshift($arrayName, $element) function Adds an element to the beginning of an array You can add multiple elements just as you can with array_push array_push($languages, “Delphi”, “C++”); array_unshift($languages, “COBOL”);

40 5/7/2015Wendi Jollymore, ACES40 Removing Array Elements $element = array_pop($arrayName) function Removes the last element and returns it $element = array_shift($arrayName) function Removes the first element and returns it $lastEl = array_pop($languages); $firstEl = array_shift($languages);

41 5/7/2015Wendi Jollymore, ACES41 Array Exercises Do the array exercises in the notes.

42 5/7/2015Wendi Jollymore, ACES42 Next Class: Functions Variable Scope Form Processing Form Validation

Download ppt "SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP."

Similar presentations

Ads by Google