PHP and MySQL

Download Report

Transcript PHP and MySQL

Introduction to
PHP and MySQL
Kirkwood Center for
Continuing Education
By Fred McClurg, [email protected]
© Copyright 2010, All Rights Reserved
1
Chapter Three
PHP Fundamentals
http://webcert.kirkwood.edu/~fmcclurg
/courses/php/slides/chapter03.ppt
2
PHP References
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/enUS/firefox/addon/8984
PHP Cookbook (762 page book):
http://books.google.com/books?id=P1wJRrE
8KjYC
3
Your First PHP Program
Description: Output much information regarding
the PHP server
<?php
phpinfo();
?>
Or ...
<?php phpinfo(); ?>
4
Getting HTML to say, “Hello World”
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>
5
Getting PHP to say, “Hello World”
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.
6
PHP Comments
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.
7
Slash 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 ";
echo "know why ";
// can be placed at
// end of statement
?>
8
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 ";
echo "goodbye ";
?>
# also be placed
# at statement end
9
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 ";
echo "hello!";
?>
/* use caution at
statement end */
10
Slash Splat 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++, Python, Java. and JavaScript.
For more information: www.doxygen.org or http://www.stack.nl/~dimitri/doxygen/ 11
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:
$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;
15
Variable Types
Description:
PHP creates the variable data type based
on the value assigned (no worries mate!)
Example:
$misc =
$misc =
$misc =
$misc =
1; // integer
"Davey"; // string
'Goliath'; // str
2.7182818; // float
Note: PHP is a weakly-typed (dynamicallytyped) language
16
Variable Scope
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>
17
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 "\nOne\nTwo\nThree";
?>
19
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
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';
21
?>
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; // 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. +, -, *, /).
expression
Example:
$result
=
$x
+
$y;
operand
operator
operand
statement
29
Mathematical Operators
Example:
$a = 4; $b = 2;
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
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 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;
Assignment
Operator
// for every case below
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. 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. 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 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
Examples:
// initialize for every statement
$a = $b = 4;
// prefix operators
$a = ++$b; // $a=5
$a = --$b; // $a=3
$b=5
$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
39
assignment.
Data Type Conversion
Description: PHP performs automatic data type
conversions between strings, integers and floats.
Examples:
$value
$value
$value
$value
=
=
=
=
"10" *
"3.14"
"Fred"
"Me" .
"10"; // 100
/ 2; // 1.57
* "McClurg"; // 0
2; // Me2
Note: Automatic data type conversion is called
“implicit casting”
40
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 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/co
urses/php/slides/chapter04a.conditions.ppt