You are on page 1of 21

PHP

PHP
• PHP: Hypertext Preprocessor
• A powerful tool for making dynamic and
interactive Web pages.
• PHP is a server-side scripting language, like
ASP, JSP, CGI, Perl
• open source software
• free to download and use
• runs on different platforms (Windows, Linux,
Unix, etc.)
• AppServ.exe provides easy
installation
(http://www.appservnetwork.com/)
• Directory structure after
installation
• Create first.php file under
www folder
• To run, Open browser
• Writwe localhost/first.php
• A PHP scripting block always starts with <?php
and ends with ?>.
• A PHP scripting block can be placed anywhere
in the document.
• The file must have a .php extension.
• If the file has a .html extension, the PHP code
will not be executed.
Single-line & Multi-line Comment
• Similar to C,
C++,Java, C#
Variables
• A variable is used to store information.
• Variables start with a $ sign (attention!)
• If you forget to use $ sign at the beginning of the variable, your program
will not work
• In a strongly typed programming language like C, you have to declare
(define) the type and name of the variable before using it.
• In PHP, the variable is declared automatically when you use it (implicit).
• In PHP, a variable does not need to be declared before adding a value to it.
• PHP automatically converts the variable to the correct data type,
depending on its value.
• Naming Rules:
– A variable name must start with a letter or an underscore "_"
– A variable name can only contain alpha-numeric characters and underscores
(a-z, A-Z, 0-9, and _ )
– A variable name should not contain spaces.
– If a variable name is more than one word, it should be separated with an
underscore ($total_students), or with capitalization ($ totalStudents)
PHP Operators
• Arithmetic:
+, -, *, /, %, ++, --
• Assignment
=, +=, -=, *=, .=
• Comparison
==, !=, >, <, >=, <=
• Logical
&&, ||, !
• String
. dot sign for concatenation
Concatenation operator converts all parameters to string
Conditional Statements
• If...Else Statements

• Switch Statement
Expression can be any value even string!
LOOP STATEMENTS

• For statement

• While Statement

• Do...while Statement
FORM PROCESSING

When ADD button clicked


Address bar change to !!!

Output
• Change method GET as POST
POST AND GET METHODS
• Two methods that browsers send form data to
php file
Data Flow: GET method
1) requests form.html Server
Client 139.179.10.12
GET /form.html HTTP/1.0
www.bilkent.edu.tr
2) Respond the request as
HTTP/1.0 200 OK form.html
Date: Fri, 30 Dec 2012 12:45:33 total.php
Content-Type:text/html
<html>
<form ... > ... </form>
...
</html>

3) client display form.html.


User fills form and click submit button
4) request total.php with input data by GET method
GET /total.php?n1=10&n2=12 HTTP/1.0

5) Web server executes total.php,


n1=10&n2=12 will be sent to php.exe
6) php.exe parses n1=10&n2=12 and
stores in $_GET array
6) sends output of total.php

7) browser(client) displays result In location bar, you will see that


http://www.bilkent.edu.tr/total.php?n1=10&n2=12
Data Flow: POST Method
1) requests form.html Server
Client 139.179.10.12
GET /form.html HTTP/1.0
www.bilkent.edu.tr
2) Respond the request as
HTTP/1.0 200 OK form.html
Date: Fri, 30 Dec 2012 12:45:33 total.php
Content-Type:text/html
<html>
<form ... > ... </form>
...
</html>
3) client display form.html.
User fills form and click submit button
4) request total.php with input data by POST method
POST /total.php HTTP/1.0
n1=10&n2=12
5) Web server executes total.php,
n1=10&n2=12 will be sent to php.exe
6) php.exe parses n1=10&n2=12 and
stores in $_POST array
6) sends output of total.php

7) browser(client) displays result


Form elements
• TextField
• Password Box
• TextArea
• RadioButton
• CheckBox
• ListBox
• Image Submit Button
• SUBMIT/RESET Button
Form Elements
•These objects have readonly, disabled properties

•These objects’ appearance can be changed using CSS


Design Following Form

When Calculate button is pressed display following Output

Rule to calculate ticket price:


•İstanbul 500, Munich 600, New York 1000 TL
•Each kg after 10kg is charged as 5 TL
•Free beverage is extra 50 TL
•Business class is extra 150 TL
•If age is less then 8, %30 discount to total price
ATTENTION
• To create a POST request, we need a form and press a
submit button. However, form data is not visible!
• GET method is normal file request method, therefore we
can call php with parameters as a link
• Form dat can not be longer than 100 characters

forum.php file content

photo.php file content


Some functions
• print_r($arr): to display array
• isset(variable): to test if a checkbox is checked
• isset is used to check if a variable is created
before. When browser sends data in a form, if
checkbox it is not checked, it does NOT send
variable for a checkbox.

You might also like