420 likes | 612 Views
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. PHP - Advantages.
E N D
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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
See Notes for this lesson, “Testing PHP” Make sure your web server is running! Open a new PHP Project in Aptana Testing PHP Wendi Jollymore, ACES
<?php /* all your php code here */ ?> The <?php ?> 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 PHP Syntax Wendi Jollymore, ACES
Echo statement Sends output to the page Double-quotes and single-quotes mean different things You can use escape sequences like \n PHP Syntax Wendi Jollymore, ACES
PHP 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:kaluha@cat.com\"> kaluha@cat.com</a>! </p>"; Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
PHP 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 Wendi Jollymore, ACES
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. */ Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
Variables • Called “scalars” • You don’t have to declare variables • But you can • To declare a variable, just initialize it: $userName=""; $dateFlag = 1; Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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; Wendi Jollymore, ACES
Escape Sequences Wendi Jollymore, ACES
Operators • Arithmetic Operators • + addition • - subtraction • * multiplication • / division • % modulus • Pre- and post- unary operators: • ++ unary increment • -- unary decrement Wendi Jollymore, ACES
Assignment Operators = equals += plus-equals -= minus-equals *= multiply-equals /= divide-equals %= modulus-equals Operators Wendi Jollymore, ACES
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 Operators Wendi Jollymore, ACES
String Operators . Concatenation operator .= Assignment-Concatenation Examples: echo “Value: “.$value.”</p>\n”; $value .= $newValue; Operators Wendi Jollymore, ACES
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”; Operators Wendi Jollymore, ACES
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. String Interpolation Wendi Jollymore, ACES
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. String Interpolation Wendi Jollymore, ACES
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 String Interpolation Wendi Jollymore, ACES
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; ?> String Interpolation Wendi Jollymore, ACES
Control Structures • If-Statements if (condition){ // code body } if (condition){ // code body } else { // code body } Wendi Jollymore, ACES
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.php?billAmt=35.55&tipPercent=15 Wendi Jollymore, ACES
Iteration • Pre-Test and Post-Test Loop while(condition) { // code body } do { // code body } while (condition); Wendi Jollymore, ACES
Iteration • For Loops: for (init; condition; cont) { // code body } foreach (item as collectionType) { // code body } Wendi Jollymore, ACES
Exercises • Do the Multiplication Table exercise in the notes Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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) Wendi Jollymore, ACES
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.. Wendi Jollymore, ACES
Iterating Through Arrays • Foreach loop: foreach($arrayName as $arrayElement) { // code } • Example: foreach($languages as $aLang) { echo $aLang.”<br />”; } Wendi Jollymore, ACES
Length of Arrays • count() function: foreach($grades as $g) { $totalGrade += $g; } $avg = $totalGrade / count($grades); echo "Average Grade: ".$avg; Wendi Jollymore, ACES
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; Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
Associative 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"; Wendi Jollymore, ACES
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; Wendi Jollymore, ACES
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”); Wendi Jollymore, ACES
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); Wendi Jollymore, ACES
Array Exercises • Do the array exercises in the notes. Wendi Jollymore, ACES
Next Class: • Functions • Variable Scope • Form Processing • Form Validation Wendi Jollymore, ACES