Beruflich Dokumente
Kultur Dokumente
PHP: An Introduction
Web Programming
Web Programming
Web Programming
What is PHP?
Written as a set of CGI binaries in C in 1994 by Rasmus Lerdorf
Created to display resume and collect data about page traffic, e.g. dynamic web pages Personal Home Page tools publicly released 1995 In 1998 became PHP: Hypertext Preprocessor
PHP is among the most stable and efficient tools available for creating dynamic, interactive Web page. PHP creates Dynamic Web Sites
Information Technology Department
Web Programming
Web Programming
Web Programming
PHP vs C/C++
Similarities:
Compiled Language Syntax nearly the same (For/While/If) Requires semicolons after each statement ; Assignment is right to left ($num = 56;) Object-Oriented (Class support, inheritance, virtuals, polymorphism) Functions! Types are nearly the same (booleans, integers, strings, etc.)
Web Programming
Web Programming
Web Programming
Client/Server Interaction
For Web pages, the client requests a page the server returns it: theres no permanent connection, just a short conversation Details of the conversation are specified by HTTP
Server
Server Server Server Client
Server
request response Server
Client
Client Server
Client
Client Client
Server
Client
Client
Web Programming
Web Programming
Preliminaries
1. Open the XAMPP Control Panel.
c:\xampp Check the Apache module and click start. make sure that the Apache module is in Running mode
Web Programming
Preliminaries
2. Create your folder.
c:\xampp\htdocs\xampp Use this folder to save all your files.
Web Programming
PHP Delimeters
<html> <head><title> My First PHP Program </title></head> <body> <?php echo This is a PHP basic syntax; ?> </body> </html> The command print can also be used in displaying texts. <? ?> can also be used.
Web Programming
... <script language = php> echo This is a PHP basic syntax; </script> ...
ASP style():
... <% echo This is a PHP basic syntax; %> ...
Web Programming
Comments in PHP
PHP supports C, C++ and Unix shell-type (Perl style) comments.
... <?php // This is a comment. # This is a comment. /* This is a block of comment */ ?> ...
Information Technology Department
Inline comments
Block of comments
Web Programming
Using and
Compare:
... <?php echo echo echo echo ?> ...
is is is is
Web Programming
Using Variables
All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers or arrays. Example:
... $num1 = 10; echo The number value is $num1 <br/>; $real = 2.50; echo The float value is $real <br/>; $txt1 = I am a string; echo The string value is $txt1 <br/>; $bool = TRUE; echo The boolean value is $bool <br/>; $hex = 0x1A; echo The hexadecimal value is $hex <br/>; $y = &$num1; echo $y is a reference to the original variable; ...
Information Technology Department
Web Programming
Using Variables
Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script. Rules
The first character must be a letter or an underscore _. A variable can only contain alpha-numeric character and underscores (a-Z, 0-9, and _) A variable containing more than one word should be separated with underscore ($my_string), or with capitalization ($myString).
Information Technology Department
Web Programming
Web Programming
Concatenation in PHP
To concatenate two or more variables together, use the dot (.) operator.
... $txt2 = This is concatenated with the \$txt1.; echo $txt1 . . $txt2; ...
Web Programming
Escape Characters
\n \r \t \v \\ \$ \" Next Line Carriage Return Horizontal Tab Vertical Tab Backslash Dollar Sign Double-Quote
echo This is a text. \n echo This is a text. \r echo \t This is a text. echo \v This is a text. echo This is a \\ backslash. echo Your total bill is \$98.00. echo You can call me \Jeff\ ;
Web Programming
Casting
<?php $testData = 10.98 degrees; echo As string: . (string)$testData. <br/>; echo As double: . (double)$testData. <br/>; echo As integer: . (integer)$testData. <br/>; ?>
Web Programming
Constant Values
... define(WORLD_INFO, 92897000); define(SUN_INFO, 72000000); define(MOON_INFO, 3456); echo The World is . WORLD_INFO. miles from the Sun; echo The Suns core temperature is approximately. SUN_INFO. degrees Fahrenheit. <br/>; echo The Moon is .MOON_INFO. miles in diameter; ...
Web Programming
Operators
Operators are used to operate on values.
Arithmetic Operators Assignment Operators Comparison Operators Logical Operators
Web Programming
Arithmetic Operators
Operator Description Example
$x=2 $x+2 4
Result
Addition
Subtraction
Multiplication Division Modulus Increment Decrement
$x=2 5-$x
$x=4 $x*5 15/5 5/2 5%2 10%8 $x=5 $x++ or ++$x x=5 $x-- or --$x
3
20 3 2.5 1 2 6 4
Web Programming
Assignment Operators
Operator = += -= *= /= %= Example $x=$y $x += $y $x -= $y $x *= $y $x /= $y $x %= $y Is the Same As $x=$y $x = $x + $y $x = $x - $y $x = $x * $y $x = $x / $y $x = $x % $y
Web Programming
Comparison Operator
Operator == != > < >= <= Example Is equal to Is not equal to Is greater than Is less than Is greater than or equal to Is less than or equal to Is the Same As 5==8 returns false 5!=8 returns true 5>8 returns false 5<8 returns true 5>= 8 returns false 5<=8 returns true
Web Programming
Logical Operators
Operator && || ! Description and or not
Web Programming
?>
Web Programming
Exercise No. 1
Create a PHP Program that will make use of variables for Name, Course, Birthdate and Age. For the value of age, it should be computed by the year of birth and the current year. Use variables for 3 Quizzes and compute its average (by percentage). Put it all in a table. Name: Course: Birthdate: Age: Quiz 1: Quiz 2: Quiz 3: Quiz Ave:
Information Technology Department
Joseph Allan dela Cruz BS Computer Science January 1, 1984 26 45 / 50 82 / 100 17 / 20 84.71 FEU EAST ASIA COLLEGE