Introduction to PHP and My SQL Kirkwood Center

  • Slides: 33
Download presentation
Introduction to PHP and My. SQL Kirkwood Center for Continuing Education By Fred Mc.

Introduction to PHP and My. SQL Kirkwood Center for Continuing Education By Fred Mc. Clurg, frmcclurg@gmail. com © Copyright 2010, All Rights Reserved 1

Chapter Three PHP Fundamentals http: //webcert. kirkwood. edu/~fmcclur g/courses/php/slides/chapter 03. ppt 2

Chapter Three PHP Fundamentals http: //webcert. kirkwood. edu/~fmcclur g/courses/php/slides/chapter 03. ppt 2

PHP References PHP Home Page (excellent): http: //www. php. net PHP Manual: http: //www.

PHP References PHP Home Page (excellent): http: //www. php. net PHP Manual: http: //www. php. net/manual/en/ PHP Tutorial: http: //www. w 3 schools. com/php/ PHP Manual Search (Firefox plugin): https: //addons. mozilla. org/en. US/firefox/addon/8984 PHP Cookbook (762 page book): http: //books. google. com/books? id=P 1 w. JRr. E 8 Kj. YC 3

Your First PHP Program Description: Output much information regarding the PHP server <? phpinfo();

Your First PHP Program Description: Output much information regarding the PHP server <? phpinfo(); ? > Or. . . <? phpinfo(); ? > 4

Getting HTML to say, “Hello World” Step One: Write HTML source “hello. World. html”

Getting HTML to say, “Hello World” Step One: Write HTML source “hello. World. html” <html> <head> <title> Hello World! </title> </head> <body> <p> Hello World! </p> <p> Does anybody really know what time it is? </p> </body> </html> 5

Getting PHP to say, “Hello World” Step Two: Modify file “hello. World. html” by

Getting PHP to say, “Hello World” Step Two: Modify file “hello. World. html” by inserting PHP code and renaming “hello. World. php” <html> <head> <title> Hello World! </title> </head> <body> <p> Hello World! </p> <p> Current time: <? php echo date('r'); ? > </p> </body> </html> Note: A semi-colon ends each code statement. 6

PHP Comments Defined: Comments are a method of documenting information inside the source code.

PHP Comments Defined: Comments are a method of documenting information inside the source code. Note: Unlike Java. Script, PHP comments and code are not displayed in the HTML source. Only HTML comments and tags are displayed when performing a 7

Slash (//) Comments Defined: The double slash “//” is used to start a comment

Slash (//) Comments Defined: The double slash “//” is used to start a comment that continues to the end of the line. Example: <!-- HTML Comment --> <? php // C++ like, single line comment echo "I don't "; // can be placed at echo "know why "; // end of statement ? > 8

Crunch (#) Comments Defined: The crunch symbol “#” (aka number, pound, hash, sharp, hex,

Crunch (#) Comments Defined: The crunch symbol “#” (aka number, pound, hash, sharp, hex, tic-tac-toe sign) is used to begin a comment to the end of the line. Examples: <!-- HTML Comment --> <? php # shell like, single line comment echo "you say "; # also be placed echo "goodbye "; # at statement end 9 ? >

Slash Splat & Splat Slash Comments Defined: The symbols “/*” is used to begin

Slash Splat & Splat Slash Comments Defined: The symbols “/*” is used to begin a comment block and the symbols “*/” is used to end that block. Examples: <!-- HTML Comment --> <? php /* C like, single line comments */ /* C like, multiple line comments spanning more than one line */ echo "I say "; /* use caution at echo "hello!"; statement end */ ? > 10

Slash Splat Doxygen Comments Defined: The symbol “/**” denotes the beginning and the symbol

Slash Splat Doxygen Comments Defined: The symbol “/**” denotes the beginning and the symbol “*/” denotes the end of a Doxygen comment block. Example: <!-- HTML Comment --> <? php /** * @brief Javadoc like documentation * via doxygen commands (tags) * @see http: //www. doxygen. nl */ ? > Note: Doxygen is an auto documentation generation tool similar to “javadoc”. It supports a number of languages including PHP, C++, 11 Python, Java. and Java. Script. For more information: www. doxygen. org or http: //www. stack. nl/~dimitri/doxygen/

Nested Comments Discussion: Nested comments can have unexpected consequences and are often the cause

Nested Comments Discussion: Nested comments can have unexpected consequences and are often the cause errors. Example: <? php // echo "Goodbye"; /* nesting permitted */ /* * echo "Goodbye"; // nesting permitted */ /* * echo "Goodbye"; /* results in error! */ */ ? > 12

Variable Rules Syntax: $var. Name = constant. Or. Expression; Rules: 1. Dollar sign ($)

Variable Rules Syntax: $var. Name = constant. Or. Expression; Rules: 1. Dollar sign ($) must be the first character. 2. The second character must be a letter or underscore “_” (not a numeral). 3. Variable names contain only alphanumeric or underscore characters. 4. Variables are case sensitive. The following represent completely different variables: $variable. Name; $variablename; 15

Variable Types Description: PHP creates the variable data type based on the value assigned

Variable Types Description: PHP creates the variable data type based on the value assigned (no worries mate!) Example: $misc = 1; // integer $misc = "Davey"; // string $misc = 'Goliath'; // str $misc = 2. 7182818; // float Note: PHP is a weakly-typed 16

Variable Scope Description: Variables declared within the same block, have the same scope Example:

Variable Scope Description: Variables declared within the same block, have the same scope Example: <body> <? php $war. Cry = "Cowabunga"; ? > <!-- HTML Code Here --> <b>The war cry is: </b> <? php echo $war. Cry; ? > </body> 17

Double Quoted Strings "" Purpose: Allows the value of a variable to be placed

Double Quoted Strings "" Purpose: Allows the value of a variable to be placed within a string (variable expansion). Example: <? php $hello = "Greetings Earthling!"; echo "He said, "$hello" <br />"; echo "Variable is $hello <br />"; echo "Have you read, Pilgrim's Progress? <br />"; echo "n. Onen. Twon. Three"; ? > 19

Escaped “Special” Characters Definition: Placing a backslash () in front of certain characters gives

Escaped “Special” Characters Definition: Placing a backslash () in front of certain characters gives them special meaning. Character Definition n Newline t Tab $ Dollar Sign " Double Quote ' Single Quote \ Backslash 20

Single Quoted Strings '' Purpose: Disables the expansion of variables and all characters except

Single Quoted Strings '' Purpose: Disables the expansion of variables and all characters except escaped single quotes ('). Example: <? php $hello = 'Greetings Earthling!'; echo 'He said, "$hello" <br />'; echo 'I said, $hello <br />'; echo 'Pilgrim's Progress <br />'; 21 echo 'n. Onen. Twon. Three';

Concatenation Definition: The dot character (. ) is used for combining one or more

Concatenation Definition: The dot character (. ) is used for combining one or more text strings and variables. Example: <? php $triad = "Faith ". "Hope "; $triad. = "Love"; // append love echo $triad. "<br />"; $count = 70 * 7; $forgiveness = "Forgive ". $count. " times"; echo $forgiveness; ? > 23

Unary Operators Description: Unary operators take one operand. Examples: $x = - $x; //

Unary Operators Description: Unary operators take one operand. Examples: $x = - $x; // negation operator $x = -1 * $x; // equivalent code $x++; // increment (postfix) --$x; // decrement (prefix) 28

Binary Operators Discussion: Binary operators combine two expressions into a result (e. g. +,

Binary Operators Discussion: Binary operators combine two expressions into a result (e. g. +, -, *, /). Example: expression $result = $x + $y; operand operator operand statement 29

Mathematical Operators Example: $a = 4; $b = 2; Op. Name Type Definition Example

Mathematical Operators Example: $a = 4; $b = 2; Op. Name Type Definition Example Ans. - Negation Unary Opposite of $a -4 + Addition Binary Sum of $a and $b $a + $b 6 $a - $b 2 Difference of $a and $b - Subtraction Binary * Multiplicatio Product of $a and Binary n $b $a * $b 8 / Division Binary Quotient of $a and $b $a / $b 2 % Modulus Binary Remainder of $a / $b $a % $b 0 32

Modulo Operator Defined: Modulo is the integer remainder returned from a division Discussion: It

Modulo Operator Defined: Modulo is the integer remainder returned from a division Discussion: It is often used for determining an evenly divisible multiple Example: <? php $count = 15; $multiple = $count % 5; if ($multiple == 0) { printf( "%d is a multiple of five", $count ); } 33

Assignment Operator Example Discussion: Assignment operators provide an abbreviated syntax for operations that are

Assignment Operator Example Discussion: Assignment operators provide an abbreviated syntax for operations that are performed frequently. They are used when a variable is combined with an arithmetic operation and then the results are assigned back to that original variable. Example: <? php $a = $b = 4; // multiple assignments $a = $a + 2; // result: $a = 6 printf( "$a = %d<br />", $a ); $b += 2; // result: $b = 6 echo( "$b = $b<br />" ); ? > 34

Assignment Operators Listed Example: $x = 4; // for every case below Assignment Operator

Assignment Operators Listed Example: $x = 4; // for every case below Assignment Operator Name Equivalent Statement Result $x += 2; Addition $x = $x + 2; 6 $x -= 2; Subtraction $x = $x - 2; 2 $x *= 2; Multiplication $x = $x * 2; 8 $x /= 2; Division $x = $x / 2; 2 $x %= 2; Modulus $x = $x % 2; 0 $x. = 2; Concatenation $x = $x. 2; "42" 35

Increment Operator Discussion: A commonly performed operation is to add one to a variable.

Increment Operator Discussion: A commonly performed operation is to add one to a variable. PHP also has abbreviated syntax for this operation. Example: <? php $pre = $post = 4; ++$pre; // prefix increment printf( "$pre = %d<br />", $pre ); $post++; // postfix increment printf( "$post = %d<br />", $post ); ? > 36

Decrement Operator Discussion: Another commonly performed operation is to subtract one from a variable.

Decrement Operator Discussion: Another commonly performed operation is to subtract one from a variable. PHP has an abbreviated syntax for this as well. Example: <? php $pre = $post = 4; --$pre; // prefix decrement printf( "$pre = %d<br />", $pre ); $post--; // postfix decrement printf( "$post = %d<br />", $post ); ? > 37

Prefix and Postfix Contrasted Discussion: The main difference between the prefix and postfix operators

Prefix and Postfix Contrasted Discussion: The main difference between the prefix and postfix operators is the order the arithmetic operation is performed in relation to the assignment. Prefix (e. g. ++$var) order of operations: 1. Arithmetic 2. Assignment Postfix (e. g. $var++) order of operations: 1. Assignment 2. Arithmetic 38

Prefix and Postfix Examples: // initialize for every statement $a = $b = 4;

Prefix and Postfix Examples: // initialize for every statement $a = $b = 4; // prefix operators $a = ++$b; // $a=5 $b=5 $a = --$b; // $a=3 $b=3 // postfix operator (side effects) $a = $b++; // $a=4 $b=5 $a = $b--; // $a=4 $b=3 Note: Because of the likelihood of introducing errors, Best Practices or Style Guidelines may mandate that only prefix operators be used. Other standards may permit either postfix or prefix but only as a standalone 39 statement and not used in an assignment.

Data Type Conversion Description: PHP performs automatic data type conversions between strings, integers and

Data Type Conversion Description: PHP performs automatic data type conversions between strings, integers and floats. Examples: $value = "10" * "10"; // 100 $value = "3. 14" / 2; // 1. 57 $value = "Fred" * "Mc. Clurg"; // 0 $value = "Me". 2; // Me 2 Note: Automatic data type conversion is called “implicit casting” 40

Type Casting Description: Variables can be forced (or cast) to a specific type. Cast

Type Casting Description: Variables can be forced (or cast) to a specific type. Cast Types : (int) or (integer) (bool) or (boolean) (float), (double), or (real) (string) Examples: $value = (int) 3. 95; // truncate decimal $value = (bool) -1; // TRUE $value = (float) 3; // 3. 0 // round to nearest integer $value = (int)($number + 0. 5); 41

Order of Precedence Description: Some operators have a higher order of precedence which means

Order of Precedence Description: Some operators have a higher order of precedence which means they are processed first in an expression Examples: echo 2 + 4 * 2; // 10 echo (2 + 4) * 2; // 12 Note: See Chapter 4, page 65 & 66 for complete precedence table. 42

to be continued. . . http: //webcert. kirkwood. edu/~fmcclurg/c ourses/php/slides/chapter 04 a. conditions. ppt

to be continued. . . http: //webcert. kirkwood. edu/~fmcclurg/c ourses/php/slides/chapter 04 a. conditions. ppt