42
SYST 28043 Web Technologies Intro to PHP

SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

Embed Size (px)

Citation preview

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

SYST 28043

Web Technologies

SYST 28043

Web Technologies

Intro to PHP

Page 2: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 2

PHPPHP

PHP: Hypertext PreprocessorA recursive acronym

Currently on version 5.4.4http://www.php.netDeveloped as a set of “Personal Home page Tools”

Rasmus LerdorfSmall scripts to do things like track visitorsEventually became the PHP we know today

Page 3: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 3

PHP - AdvantagesPHP - Advantages

Free, Open SourceFastRuns on most operating systemsEasy to learn – has C/Java/Perl type of syntaxLoosely-typedProcedure-oriented or object-orientedPowerful string processing and regular expression librariesVery large and active developer community

Page 4: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 4

Testing PHPTesting PHP

See Notes for this lesson, “Testing PHP”1. Make sure your web server is running!2. Open a new PHP Project in Aptana

Page 5: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 5

PHP SyntaxPHP Syntax

<?php /* all your php code here */?>

The <?php ?> tags contain the php codeAll executing statements end in a semi-colonYou can put your entire page in the tagsYou can embed the PHP throughout your XHTML code

See examples in the notes

Page 6: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 6

PHP SyntaxPHP Syntax

Echo statementSends output to the pageDouble-quotes and single-quotes mean different thingsYou can use escape sequences like \n

Page 7: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 7

PHP ExamplePHP Example

echo "<h3>This is a PHP Page by Kaluha</h3>";

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:[email protected]\">

[email protected]</a>! </p>";

Page 8: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 8

PHP ExamplePHP Example

Load the page in your browserView 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

Page 9: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 9

PHP ExamplePHP Example

Add this:echo "<p>Current Date and Time: ";

echo date("l jS \of F Y h:i:s A");

echo "</p>\n";

Do the date() exercise in the notes

Page 10: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 10

PHP CommentingPHP 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.

*/

Page 11: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 11

IdentifiersIdentifiers

Rules for Identifiers:identifier names are case-sensitiveidentifiers must start with a letter or underscore characteridentifier names may not contain spacesidentifiers must be made up of letters and numbers and symbols from ASCII 127 to ASCII 255

Page 12: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 12

VariablesVariables

Called “scalars”You don’t have to declare variables

But you can

To declare a variable, just initialize it:

$userName="";

$dateFlag = 1;

Page 13: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 13

VariablesVariables

ScalarsVariables that hold a single valueAlways starts with a $Value can be one of:

BooleanTrue or falseFalse = 0; any other integer is true

IntegerFloating-pointstring

Page 14: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 14

ConstantsConstants

Constants are declared using the define() method:define(“PST_RATE”, .08);

Defines a constant called PST_RATE with an initial value of .08Use it just like you would use any other scalar:

$pstAmt = $subTotal * PST_RATE;

Page 15: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 15

Escape SequencesEscape Sequences

\b backspace

\f Form feed

\n New-line

\r Carriage return

\t Tab

\’ Single-quote

\” Double-quote

\\ Backslash

Page 16: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 16

OperatorsOperators

Arithmetic Operators+ addition- subtraction* multiplication/ division% modulus

Pre- and post- unary operators:++ unary increment -- unary decrement

Page 17: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 17

OperatorsOperators

Assignment Operators= equals+= plus-equals-= minus-equals*= multiply-equals/= divide-equals%= modulus-equals

Page 18: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 18

OperatorsOperators

Relational Operators== equal to!= not equal to> greater than>= greater than or equal to< less than<= less than or equal to

Logical Operators&&, AND AND Operator||, OR OR Operator! , NOT NOT Operator

Page 19: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 19

OperatorsOperators

String Operators . Concatenation operator .= Assignment-Concatenation

Examples:echo “Value: “.$value.”</p>\n”;

$value .= $newValue;

Page 20: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 20

OperatorsOperators

Conditional Operatorcondition ? retValueTrue : retValueFalse;If condition is true, retValueTrue is returnedIf condition is false, retValueFalse is returned

Example:$validNum = ($intValue > 0) ? “valid” : “invalid”;

Page 21: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 21

String InterpolationString Interpolation

Double-Quotes:Variables and escape sequences are parsedExample:

$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.

Page 22: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 22

String InterpolationString Interpolation

Single-Quotes:Variables and escape sequences are not interpreted, except where it might cause a syntax errorExample:

echo 'My variable is $catName and it\'s \n case-sensitive.';

Output:My variable is $catName and it's \n case-sensitive.

Page 23: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 23

String InterpolationString Interpolation

Heredoc:Using labels to mark larger passages of textContent between labels is interpreted as if in double-quotes

But you don’t have to escape double-quotes

Starting and ending labels must matchLabels must be in upper-caseStarting label must be preceeded by <<<Ending label must be first on a blank line

Not even spaces in front of it

Page 24: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 24

String InterpolationString 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;

?>

Page 25: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 25

Control StructuresControl Structures

If-Statementsif (condition){

// code body

}

if (condition){

// code body

} else {

// code body

}

Page 26: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 26

ExerciseExercise

Do the Tip Calculator exercise in the notesPass the input values using GET method name=value pairsE.g.

localhost/webtech/projetName/ex1.php?billAmt=35.55&tipPercent=15

Page 27: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 27

IterationIteration

Pre-Test and Post-Test Loop

while(condition) {

// code body

}

do {

// code body

} while (condition);

Page 28: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 28

IterationIteration

For Loops:

for (init; condition; cont) {

// code body

}

foreach (item as collectionType) {

// code body

}

Page 29: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 29

ExercisesExercises

Do the Multiplication Table exercise in the notes

Page 30: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 30

ArraysArrays

Arrays in PHP work similarly to arrays in other languages

Arrays are 0-based by defaultArray elements are identified with indexes or subscripts

Page 31: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 31

Creating ArraysCreating 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)

Page 32: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 32

Creating ArraysCreating 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..

Page 33: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 33

Iterating Through ArraysIterating Through Arrays

Foreach loop:foreach($arrayName as $arrayElement) {

// code

}

Example:foreach($languages as $aLang) {

echo $aLang.”<br />”;

}

Page 34: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 34

Length of ArraysLength of Arrays

count() function:

foreach($grades as $g) {

$totalGrade += $g;

}

$avg = $totalGrade / count($grades);

echo "Average Grade: ".$avg;

Page 35: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 35

Associative ArraysAssociative Arrays

Elements consist of a key and value pair

The key is the indexThe value is the content of the array element

Keys must be uniqueKeys can be strings

E.g. $grades[“prog10082”] = 75;

Page 36: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 36

Associative ArraysAssociative 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

Page 37: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 37

Associative ArraysAssociative Arrays

Using foreach() with associative arrays:foreach(array_expr as $key => $value) { // statements}

Example:echo "<p>My Grades:<br />"; foreach($myGrades as $course => $mark) {echo “<p><b>$course: </b>”;

echo “$mark<br />\n”;}echo "</p>\n";

Page 38: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 38

Adding Array ElementsAdding Array Elements

Arrays in PHP are dynamicThe 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;

Page 39: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 39

Adding Array ElementsAdding Array Elements

array_push($arrayName, $element) functionAdds an element to the end of an arrayYou can add multiple elements:array_push($arrayName, $el1, $el2, $el3);

array_unshift($arrayName, $element) functionAdds an element to the beginning of an arrayYou can add multiple elements just as you can with array_push

array_push($languages, “Delphi”, “C++”);

array_unshift($languages, “COBOL”);

Page 40: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 40

Removing Array ElementsRemoving 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);

Page 41: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 41

Array ExercisesArray Exercises

Do the array exercises in the notes.

Page 42: SYST 28043 Web Technologies SYST 28043 Web Technologies Intro to PHP

04/18/23 Wendi Jollymore, ACES 42

Next Class:Next Class:

FunctionsVariable ScopeForm ProcessingForm Validation