Download presentation
Presentation is loading. Please wait.
Published byBrittney Allen Modified over 9 years ago
1
PHP Teppo Räisänen LIIKE/OAMK 2011
2
PHP PHP is a programming language for making dynamic and interactive Web pages
3
PHP PHP stands for PHP: Hypertext Preprocessor PHP files end with.php,.php3,.php4,.php5 or.phtml In most cases its.php
4
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
5
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
6
PHP example1 <?php Code here ?>
7
PHP example2 <? Code here ?>
8
PHP example3 PHP example <?php print ” Hello World "; ?>
9
PHP example3 (in browser) PHP example Hello World
10
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) datatype When code is executed data type is defined according the contents Example $somevariable=0; $somestring=”Teppo”;
11
Basic datatypes String Integer Float Boolean
12
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=””;
13
Operators = = Operator $firstname=”Charlie”; $lastname=”Brown”; $name=$firstname. $lastname; $somenumber=1; $someothernumber=3; $total=$somenumber + $someothernumber; $formvalue=$_POST[’fieldname’];
14
Arithmetic operations + addition - subtraction / division * multiplication % remainder
15
Control structures Conditional structures If Switch Loops While For Functions
16
Basic syntax if (expression) {... } else {... }
17
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”; } ?>
18
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”; } ?>
19
Logical operations || OR &&AND !NOT
20
Example <? $grade=$_POST[’grade’]; //If grade is not between 0 and 5 it is //not on scale. if ($grade 5) { print ”Not on scale”; } ?>
21
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”; } ?>
22
Loops While For …
23
While initialization while (condition) { //one or more statemens increment }
24
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”; ?>
25
For For (initialization;condition;increment) { //one or more statemens }
26
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”; ?>
27
Functions Two types of functions Built-in (in PHP more that 700 prewritten functions available)700 Custom Gives well-defined structure to software Adds reusability
28
Function syntax Return valueNameParameters
29
Example: mail() Send email from you application without any spesific knowledge about email-protocols or low-level details <? $to="jouni.juntunen@students.oamk.fi"; $from=”teppo.raisanen@oamk.fi”; $subject=”The subject”; $message=”Test”; $headers='From:'. $from. "\r\n". 'X-Mailer: PHP/'. phpversion(); if (mail($to,$subject,$message,$headers)) … ?>
30
Input validation with functions Check if input given by user is numerical Validation user following PHP-functions floatval(), intval(), strval()
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.