Sie sind auf Seite 1von 31

Web Programming

PHP: An Introduction

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

HTML is Static and Boring


HTML is great if you just want to share picture of your pet... but not so great if you want to interact with visitors to your site. It is like forming a line without interaction to other people

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

PHP brings web pages to LIFE


PHP is a server-side scripting language that runs its application in a server. It adds functionality to a static web page. PHP files can contain text, HTML tags and scripts PHP runs on different platforms (Windows, Linux, Unix, etc.) To get access to a web server with PHP support, you can Install Apache (or IIS) on your own server, install PHP, and MySQL
Information Technology Department

FEU EAST ASIA COLLEGE

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

FEU EAST ASIA COLLEGE

Web Programming

Dynamic Web Site


A dynamic website is one that has
frequently changing information (e.g. Content Management System) interacts with the user from various methods (HTTP cookies or database variables e.g., previous history, session variables, server side variables, e.g., environmental data) direct interaction (form elements, mouseovers, simulation, etc.).

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Tools and Language


Macromedia CSS Dreamweaver8.0 Java Script Adobe Photoshop Coffee Cup Menu HTML Builder PHP XAMPP MySQL Apache Cute FTP
Information Technology Department

FEU EAST ASIA COLLEGE

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.)

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

PHP vs. C/C++


Differences:
Variables begin with $ sign ($name = John Doe;) No explicit declaration of variable types Introduction of lazy functions (foreach, explode, mail) No Function Overloading Hidden functions-within-a-function Compiled/interpreted during every page load Echo for output

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

PHP vs. C/C++


Web Specific:
Cookies and Sessions Dynamic HTML based on user-defined logic Interact and process a forms action Process URL Parameters Easy Database Integration Cross-Site-Scripting (XSS) security hacks - taken care of by PHP 5
Allows code injection by web users into web pages viewed by other users (e.g. phishing attacks)
Information Technology Department

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

How to start PHP?

Information Technology Department

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Preliminaries
2. Create your folder.
c:\xampp\htdocs\xampp Use this folder to save all your files.

Information Technology Department

FEU EAST ASIA COLLEGE

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.

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

The <script> tag:

Other Structure: Escaping the HTML

... <script language = php> echo This is a PHP basic syntax; </script> ...

ASP style():
... <% echo This is a PHP basic syntax; %> ...

Information Technology Department

FEU EAST ASIA COLLEGE

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

FEU EAST ASIA COLLEGE

Web Programming

Using and
Compare:
... <?php echo echo echo echo ?> ...

This This This This

is is is is

using using using using

double quotes; single quotes; both; both;

Information Technology Department

FEU EAST ASIA COLLEGE

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

FEU EAST ASIA COLLEGE

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

FEU EAST ASIA COLLEGE

Web Programming

The Difference of and


... $num1 = 10; echo The value is $num1; echo <br />; echo The value is $num1; ...

Output: The value is 10 The value is $num1

Information Technology Department

FEU EAST ASIA COLLEGE

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

Comma (,) can also be used to concatenate series of strings.

Information Technology Department

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

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

Other type: echo (int) $testData; echo (float) $testData;

Information Technology Department

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Operators
Operators are used to operate on values.
Arithmetic Operators Assignment Operators Comparison Operators Logical Operators

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Arithmetic Operators
Operator Description Example
$x=2 $x+2 4

Result

Addition

* / % ++ -Information Technology Department

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

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

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

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Logical Operators
Operator && || ! Description and or not

Information Technology Department

FEU EAST ASIA COLLEGE

Web Programming

Seat Work: What is the output?


<?php $x = 10; echo $x++; echo <br/>; echo $x-2; echo <br/>; echo --$x; echo <br/>; $x += 6; echo $x;

?>

Information Technology Department

FEU EAST ASIA COLLEGE

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

Das könnte Ihnen auch gefallen