introduction to php and mysql kirkwood center for continuing education by fred mcclurg,...

33
on to PHP and PHP and MySQL MySQL Kirkwood Center for Kirkwood Center for Continuing Education Continuing Education By Fred McClurg, [email protected] © Copyright 2010, All Rights Reserved 1

Post on 20-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Introduction Introduction toto

PHP and PHP and MySQLMySQL

Kirkwood Center for Kirkwood Center for Continuing EducationContinuing Education

By Fred McClurg, [email protected]

© Copyright 2010, All Rights Reserved 1

Chapter Chapter ThreeThree

PHP FundamentalsPHP Fundamentals

http://webcert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter03.ppt

2

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=P1wJRrE8KjYC

PHP ReferencesPHP References

3

Description: Output much information regarding the PHP server

<?php phpinfo();

?>

Or ...

<?php phpinfo(); ?>

Your First PHP ProgramYour First PHP Program

4

Step One: Write HTML source “helloWorld.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>

Getting HTML to say, “Hello Getting HTML to say, “Hello World”World”

5

Step Two: Modify file “helloWorld.html” by inserting PHP code and renaming “helloWorld.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.

Getting PHP to say, “Hello Getting PHP to say, “Hello World”World”

6

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 CommentsPHP Comments

7

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?>

Slash Slash (//) CommentsSlash Slash (//) Comments

8

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?>

Crunch (#) CommentsCrunch (#) Comments

9

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 */?>

Slash Splat & Splat Slash Slash Splat & Splat Slash CommentsComments

10

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++, Python, Java. and JavaScript. For more information: www.doxygen.org or http://www.stack.nl/~dimitri/doxygen/

Slash Splat Splat Doxygen Slash Splat Splat Doxygen CommentsComments

11

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 CommentsNested Comments

12

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 RulesVariable Rules

15

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 TypesVariable Types

16

Description:Variables declared within the same block, have the same scope

Example:<body> <?php $warCry = "Cowabunga"; ?>

<!-- HTML Code Here --> <b>The war cry is:</b>

<?php echo $warCry; ?></body>

Variable Scope

17

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 "\nOne\nTwo\nThree";

?>

Double Quoted Strings Double Quoted Strings """"

19

Definition: Placing a backslash (\) in front of certain characters gives them special meaning.

Escaped “Special” CharactersEscaped “Special” Characters

Character Definition

\n Newline

\t Tab

\$ Dollar Sign

\" Double Quote

\' Single Quote

\\ Backslash20

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

/>'; echo '\nOne\nTwo\nThree';

?>

Single Quoted Strings Single Quoted Strings ''''

21

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;?>

ConcatenationConcatenation

23

Description: Unary operators take one operand.

Examples:$x = - $x; // negation operator$x = -1 * $x; // equivalent code$x++; // increment (postfix)--$x; // decrement (prefix)

Unary OperatorsUnary Operators

28

Discussion: Binary operators combine two expressions into a result (e.g. +, -, *, /).

Example:

$result = $x + $y;

Binary OperatorsBinary Operators

operandoperator

operand

expression

statement 29

Example:$a = 4; $b = 2;

Mathematical OperatorsMathematical Operators

Op. Name Type Definition Example Ans.

- Negation Unary Opposite of $a -$a -4

+ Addition Binary Sum of $a and $b $a + $b 6

- Subtraction Binary Difference of $a and $b $a - $b 2

* Multiplication Binary Product of $a and $b $a * $b 8

/ Division Binary Quotient of $a and $b $a / $b 2

% Modulus Binary Remainder of $a / $b $a % $b 0 32

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 OperatorModulo Operator

33

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 />" );?>

Assignment Operator ExampleAssignment Operator Example

34

Example:$x = 4; // for every case below

Assignment Operators ListedAssignment Operators Listed

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

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 );?>

Increment OperatorIncrement Operator

36

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 );?>

Decrement OperatorDecrement Operator

37

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. Arithmetic2. Assignment

Postfix (e.g. $var++) order of operations:1. Assignment2. Arithmetic

Prefix and Postfix ContrastedPrefix and Postfix Contrasted

38

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 ExamplesPrefix and Postfix Examples

39

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 ConversionData Type Conversion

40

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 CastingType Casting

41

Description: Some operators have a higher order of precedence which means they are processed first in an expression

Examples:echo 2 + 4 * 2; // 10echo (2 + 4) * 2; // 12

Note: See Chapter 4, page 65 & 66 for complete precedence table.

Order of PrecedenceOrder of Precedence

42

to be to be continued ...continued ...

http://webcert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter04a.conditions.ppt