IT420: Database Management and Organization PHP 28 February 2006

advertisement
IT420: Database Management
and Organization
PHP
28 February 2006
Adina Crainiceanu
www.cs.usna.edu/~adina
Web Applications
 Choose:




Operating system – Windows or Linux
Web server software – Apache
Database Management System – MySQL
Programming or scripting language – PHP
PHP
 PHP: PHP Hypertext Preprocessor
 Server-side scripting language
 Browser never sees PHP - only the web
server sees it
 PHP pages require a web server with PHP
support
 Competitors: Perl, ASP.NET, JSP
PHP Strengths









High performance
Interface to many different database systems
Built-in libraries
Ease of learning and use
Object-oriented support
Portability
Open source
Free
Availability of support
PHP References
 Online references
 http://www.php.net
 Online Tutorial
 http://www.w3schools.com/php/default.asp
 PHP and MySQL Web Development by
Luke Welling and Laura Thomson
 MySQL/PHP Database Applications by
Brad Bulger, Jay Greenspan, David Wall
Example PHP Page – Server Side
Code as it appears on the Server side
<h2>Today's Headline:</h2>
<p>
<?php echo "World Peace Declared"; ?>
</p>
Begin and end PHP code to be interpreted by server PHP engine
Example PHP Page Processed –
Client Side
Code as it appears on the client browser side
<h2>Today's Headline:</h2>
<p>
"World Peace Declared“
</p>
Sokkit demo
 Save .php files in C:\Sokkit\site
 Run http://localhost/yourphpfile.php
Basic PHP Syntax
<html>
<body>
<?php echo "Hello World"; # this is a PHP test program ?>
</body>
</html>





HTML code
PHP tags
PHP statements
White space
Comments
PHP Tags
 Preferred style:
 <?php echo “Hello World”; ?>
 Other styles:
 <? echo “Hello World”; ?>
 <SCRIPT LANGUAGE=‘php’> echo “Hello
World”; </SCRIPT>
 <% echo “Hello World”; %>
PHP Statements
 Tell PHP interpreter what to do
 Example: echo, if, while …
<?php echo “Hello World"; ?>
 Separated by semicolon
White space And Comments
 White space is ignored
 Comments:
 /* multi-line
comment here */
 // comment here
 # comment here
# here is a comment ?> here is no comment
PHP Variables




Variable names start with $
$testvariable = 0;
Case sensitive
Variables do not have to be declared
 Created when a value is assigned
 Data types:






Integer
Float
String
Boolean
Array
Object
PHP Variables (cont.)
 Variables type determined by content




$test1 = 0;
$test2 = 0.0;
$test2 = $test1;
$test2 = “Hello”;
//integer
//float
//integer
//Is this possible?
 Variable variables
 $varname = ‘test1’;
 $$varname = 5; //result: $test1 = 5
PHP Constants
 define(‘CONST1’, 5)
Constant Name – No $
 $test2 = CONST1 * $test1;
Strings
 String concatenation operator: . (dot)
 $test1 = ‘I’m’;
 Echo $test1.’ John’
Concatenate
 Single quotes strings:
 True literals
 Double quotes strings – variables are
evaluated (interpolation):
 Echo “$test1 John”
Lab Exercise
 Start Sokkit
 Create and run helloworld.php
 Modify helloworld.php to use variables
 Assign some text to two variables
 Print the text obtained by concatenation
Arithmetic Operators
Operator
Description
Example
Result
+
Addition
x=2
x+2
4
-
Subtraction
x=2
5-x
3
*
Multiplication
x=4
x*5
20
/
Division
15/5
5/2
3
2.5
%
Modulus (division remainder)
5%2
10%8
10%2
1
2
0
++
Increment
x=5
x++
x=6
--
Decrement
x=5
x--
x=4
Assignment Operators
Operator
Example
Is The Same As
=
x=y
x=y
+=
x+=y
x=x+y
-=
x-=y
x=x-y
*=
x*=y
x=x*y
/=
x/=y
x=x/y
%=
x%=y
x=x%y
Comparison Operators
Operator
Description
Example
==
is equal to
5==8 returns false
!=
is not equal
5!=8 returns true
<>
is not equal
5 <> 8 returns true
>
is greater than
5>8 returns false
<
is less than
5<8 returns true
>=
is greater than or equal to
5>=8 returns false
<=
is less than or equal to
5<=8 returns true
Logical Operators
Operator
Description
Example
&&
and
||
or
x=6 y=3
(x==5 || y==5) returns false
!
not
x=6 y=3
!(x==y) returns true
x=6 y=3
(x < 10 && y > 1) returns true
Control Statements
 If (condition) {do something}
else { do something else}
 Switch(variable) {
case value1: someCode2; break;
case value2: someCode2; break;
…
default: someCode; break;
}
Control Statements (cont)




while(condition){ do something}
do {something} while (condition)
for(init; condition; increment){something}
foreach(array as value){something}
Used for arrays
Arrays
 $numbers = array(‘one’, ‘two’, ‘three’);
 $arr[0] == ‘one’ is true
 foreach($numbers as $current){
echo “Current element is $current”;
}
Lab Exercise
 Create an array containing the following values:
(1900, 2000, 2004, 2005 ).
 Use the array in a “foreach” loop to test if the
value is a leap year.
 If it is a leap year print “XXXX is a leap year”
 Else print “XXXX is not a leap year”
 A year is a leap year if it is divisible by 4. If the
year is also divisible by 100 then it is NOT a leap
year unless it also divisible by 400. Thus, 1900
is not a leap year (divisible by 100 but not 400)
while 2000 is a leap year (divisible by 400).
Form Processing
Browser
Web Server
User enters
information into
the form then
“Submits” the
form
• Server receives form.
• The form’s action is the
name of the PHP script
• PHP engine parser
decodes the form contents
and variables then
processes the script
User sees the
results.
Results sent back to the
browser. Connection closes.
Input Form
<html>
<body>
<form action=“name_age.php" method="POST">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Accessing the Form Variables
 name_age.php needs to know the values
of the input fields
 $age
//short style
 Requires ‘register_globals’
 $_POST[‘age’]
//medium style
 $_GET[‘age’] if method is GET
 $_REQUEST[‘age’]
 $HTTP_POST_VARS[‘age’] //long style
name_age.php – Called on Submit
<html>
<body>
Welcome <?php echo $_POST[‘name’]; ?>.<br />
You are <?php echo $_POST[‘age’]; ?> years old!
</body>
</html>
Sample output:
Welcome John.
You are 28 years old!
Download