30
PHP Teppo Räisänen LIIKE/OAMK 2011

PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Embed Size (px)

Citation preview

Page 1: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP

Teppo Räisänen

LIIKE/OAMK 2011

Page 2: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP

PHP is a programming language for making dynamic and interactive Web pages

Page 3: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP

PHP stands for PHP: Hypertext Preprocessor

PHP files end with .php, .php3, .php4, .php5 or .phtml In most cases its .php

Page 4: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP

It is a server-side scripting language This means that PHP code is executed

on the server, not on the browser JavaScript is executed on the browser

This also means that you have to put you PHP files into the Web server They will not work e.g. From desktop

Page 5: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP

PHP is open source Free to download and use

PHP supports many databases MySQL, Oracle, Informix, … When you do dynamic Web pages you

want to use databases In practice you always need

databases

Page 6: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP example1

<?phpCode here

?>

Page 7: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP example2

<?Code here

?>

Page 8: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP example3

<html><head><title>PHP example</title></head><body>

<?phpprint ”<h1>Hello World</h1>";?>

</body></html>

Page 9: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

PHP example3 (in browser)

<html><head><title>PHP example</title></head><body>

<h1>Hello World</h1>

</body></html>

Page 10: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Datatypes and variables PHP is a ”loosely-typed” language When you declare a variable, you don’t

give datatype (but still variables do have datatypes)

When code is executed data type is defined according the contents

Example$somevariable=0;$somestring=”Teppo”;

Page 11: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Basic datatypes

String Integer Float Boolean

Page 12: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Datatypes and variables

Variable declaring starts with $-character

You invent the name for the variable Good practise is to give initial value Example:

$somenumber=0;$sometext=””;

Page 13: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Operators = = Operator $firstname=”Charlie”; $lastname=”Brown”; $name=$firstname . $lastname; $somenumber=1; $someothernumber=3; $total=$somenumber +

$someothernumber; $formvalue=$_POST[’fieldname’];

Page 14: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Arithmetic operations

+ addition - subtraction / division * multiplication % remainder

Page 15: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Control structures Conditional structures

If Switch

Loops While For

Functions

Page 16: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Basic syntax

if (expression){...}else{...}

Page 17: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example<?//Read value passed from HTML-form.$age=$_POST[’age’];

//Found out if user is minor or adult according to age.if ($age<18){

print ”Minor”;}else{

print ”Adult”;}?>

Page 18: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Nested statements<?$grade=$_POST[’grade’];If ($grade==0){ print ”F”;}else{ if ($grade<3) { print ”C”; } else { if ($grade<5) { print ”B”; } else { if ($gade==5) { print ”A”; } else { print ”Not on scale”; } } }}?>

Page 19: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Logical operations

|| OR && AND ! NOT

Page 20: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example

<?$grade=$_POST[’grade’];//If grade is not between 0 and 5 it is //not on scale.if ($grade<0 || $grade>5){

print ”Not on scale”;}?>

Page 21: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example<?//Read value passed from HTML-form.$age=$_POST[’age’];

//Found out if user is minor or adult according to age.if (!$age<18){

print ”Adult”;}else{

print ”Minor”;}?>

Page 22: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Loops

While For …

Page 23: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

While

initializationwhile (condition){

//one or more statemensincrement

}

Page 24: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example (calculating compound interest using while)<?$loan=100;$interest=10;$year=3;$i=0;

while ($i < $year){

$loan=$loan+ ($loan / 100) * $interest;$i++;

}

print ”Total amount of loan is $loan”;?>

Page 25: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

For

For (initialization;condition;increment){

//one or more statemens}

Page 26: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example (calculating compound interest using for)<?$loan=100;$interest=10;$year=3;

for ($i=0;$i<$year;$i++);{

$loan=$loan + ($loan / 100) * $interest;}

print ”Total amount of loan is $loan”;?>

Page 27: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Functions

Two types of functions Built-in (in PHP more that 700

prewritten functions available) Custom

Gives well-defined structure to software

Adds reusability

Page 28: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Function syntax

Return value Name Parameters

Page 29: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Example: mail() Send email from you application without

any spesific knowledge about email-protocols or low-level details

<? $to="[email protected]";$from=”[email protected]”;$subject=”The subject”;$message=”Test”;$headers='From:' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();if (mail($to,$subject,$message,$headers))…

?>

Page 30: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages

Input validation with functions

Check if input given by user is numerical

Validation user following PHP-functions floatval(), intval(), strval()