Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2010, All Rights Reserved 1.

Similar presentations


Presentation on theme: "Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2010, All Rights Reserved 1."— Presentation transcript:

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

2 Chapter Three PHP Fundamentals http://webcert.kirkwood.edu/~fmcclurg /courses/php/slides/chapter03.ppt 2

3 PHP Home Page (excellent): http://www.php.net PHP Manual: http://www.php.net/manual/en/ PHP Tutorial: http://www.w3schools.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=P1wJRrE 8KjYC PHP References 3

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

5 Step One: Write HTML source “helloWorld.html” Hello World! Hello World! Does anybody really know what time it is? Getting HTML to say, “Hello World” 5

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

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

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

9 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: <?php # shell like, single line comment echo "you say "; # also be placed echo "goodbye "; # at statement end ?> Crunch (#) Comments 9

10 Defined: The symbols “ /* ” is used to begin a comment block and the symbols “ */ ” is used to end that block. Examples: <?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 */ ?> Slash Splat & Splat Slash Comments 10

11 Defined: The symbol “ /** ” denotes the beginning and the symbol “*/” denotes the end of a Doxygen comment block. Example: <?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++, Python, Java. and JavaScript. For more information: www.doxygen.org or http://www.stack.nl/~dimitri/doxygen/www.doxygen.orghttp://www.stack.nl/~dimitri/doxygen/ Slash Splat Splat Doxygen Comments 11

12 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! */ */ ?> Nested Comments 12

13 Defined: Storing a value in a named memory location. Purpose: 1. More convenient handle for accessing data. 2. Meaningful names aid in documenting the code. 3. Useful when requiring multiple references to same value. 4. Useful tracking values that change. Examples: Variables <?php $age = 48; ?> <?php $age = 48; ?> Memory NameValueLocation $age 48 7FF 13

14 Defined: Storing a value in a named memory location. Examples: $age = 48; // integer $firstName = "Fred"; // string /* allow variable expansion */ /* result: Fred McClurg */ $varExpand = "$firstName McClurg"; /* allow no variable expansion */ /* result: $firstName McClurg */ $noVarExpand = '$firstName McClurg'; $pi = 3.14159265358979323846; // float Note: A variable can contain one and only one value. Variable Examples 14

15 Syntax: $varName = constantOrExpression; 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: $variableName; $variablename; $VariableName; Variable Rules 15

16 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 (dynamically- typed) language Variable Types 16

17 Description: Variables declared within the same block, have the same scope Example: <?php $warCry = "Cowabunga"; ?> The war cry is: <?php echo $warCry; ?> Variable Scope 17

18 Definition: A collection of individual characters Example: $string = "word"; Note: Memory is automatically allocated as needed. String 18

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

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

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

22 Examples: <?php echo " Fireproof "; echo " Expelled "; echo ' Bella '; ?> Using Escaped Characters 22

23 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. " "; $count = 70 * 7; $forgiveness = "Forgive ". $count. " times"; echo $forgiveness; ?> Concatenation 23

24 Defined: Variable that contains a value that can not be modified. Example: <?php define( "SQRT2", 1.41421356 ); echo SQRT2. " "; $varName = "SQRT2"; echo constant( $varName ); ?> Constants 24

25 Recommendations and Characteristics: Constant variables should be capitalized Constants not prefixed by dollar sign ( $ ) Constants are defined using define() Constants are defined globally Once set, constants can not be redefined Constants can evaluate only scalar values Constant Best Practices 25

26 Description: Besides FALSE and TRUE, the language defines several built-in constants. Predefined Constants: __LINE__ returns the line number __FILE__ returns the pathname __FUNCTION__ returns function name Predefined Constants 26

27 Example: 1 2 ", __FILE__ ); 6 printf( "Line: %d ", __LINE__ ); 7 printf( "Funct: %s", __FUNCTION__ ); 8 } 9 10 foo(); 11 ?> 12 Predefined Constant Examples 27

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

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

30 Description: Ternary operators take three operands and provide abbreviated in-line syntax for an “if” statement. Syntax: cond ? stmtTrue : stmtFalse ; Ternary Operators 30

31 Example: <?php $val = 4; $isEven = (($val % 2) == 0) ? TRUE : FALSE; var_dump( $isEven ); ?> Ternary Operator Example 31

32 Example: $a = 4; $b = 2; Mathematical Operators Op.NameTypeDefinitionExampleAns. - NegationUnaryOpposite of $a -$a-4 + AdditionBinarySum of $a and $b $a + $b6 - SubtractionBinary Difference of $a and $b $a - $b2 * MultiplicationBinaryProduct of $a and $b $a * $b8 / DivisionBinaryQuotient of $a and $b $a / $b2 % ModulusBinaryRemainder of $a / $b $a % $b0 32

33 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 ); } ?> Modulo Operator 33

34 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 ", $a ); $b += 2; // result: $b = 6 echo( "\$b = $b " ); ?> Assignment Operator Example 34

35 Example: $x = 4; // for every case below Assignment Operators Listed Assignment Operator NameEquivalent 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

36 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 ", $pre ); $post++; // postfix increment printf( "\$post = %d ", $post ); ?> Increment Operator 36

37 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 ", $pre ); $post--; // postfix decrement printf( "\$post = %d ", $post ); ?> Decrement Operator 37

38 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 Prefix and Postfix Contrasted 38

39 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 statement and not used in an assignment. Prefix and Postfix Examples 39

40 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" * "McClurg"; // 0 $value = "Me". 2; // Me2 Note: Automatic data type conversion is called “implicit casting” Data Type Conversion 40

41 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); Type Casting 41

42 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. Order of Precedence 42

43 to be continued... http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter04a.conditions.ppt


Download ppt "Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2010, All Rights Reserved 1."

Similar presentations


Ads by Google