PHP Teppo Risnen LIIKEOAMK 2011 PHP n PHP
















![Example <? //Read value passed from HTML-form. $age=$_POST[’age’]; //Found out if user is minor Example <? //Read value passed from HTML-form. $age=$_POST[’age’]; //Found out if user is minor](https://slidetodoc.com/presentation_image/7eecdd0d1f61d743eafc00984ddafb42/image-17.jpg)
![Nested statements <? $grade=$_POST[’grade’]; If ($grade==0) { print ”F”; } else { if ($grade<3) Nested statements <? $grade=$_POST[’grade’]; If ($grade==0) { print ”F”; } else { if ($grade<3)](https://slidetodoc.com/presentation_image/7eecdd0d1f61d743eafc00984ddafb42/image-18.jpg)

![Example <? $grade=$_POST[’grade’]; //If grade is not between 0 and 5 it is //not Example <? $grade=$_POST[’grade’]; //If grade is not between 0 and 5 it is //not](https://slidetodoc.com/presentation_image/7eecdd0d1f61d743eafc00984ddafb42/image-20.jpg)
![Example <? //Read value passed from HTML-form. $age=$_POST[’age’]; //Found out if user is minor Example <? //Read value passed from HTML-form. $age=$_POST[’age’]; //Found out if user is minor](https://slidetodoc.com/presentation_image/7eecdd0d1f61d743eafc00984ddafb42/image-21.jpg)









- Slides: 30
PHP Teppo Räisänen LIIKE/OAMK 2011
PHP n PHP is a programming language for making dynamic and interactive Web pages
PHP n PHP stands for n n PHP: Hypertext Preprocessor PHP files end with n n . php, . php 3, . php 4, . php 5 or. phtml In most cases its. php
PHP n It is a server-side scripting language n n n This means that PHP code is executed on the server, not on the browser Java. Script is executed on the browser This also means that you have to put you PHP files into the Web server n They will not work e. g. From desktop
PHP n PHP is open source n n Free to download and use PHP supports many databases n n n My. SQL, Oracle, Informix, … When you do dynamic Web pages you want to use databases In practice you always need databases
PHP example 1 <? php Code here ? >
PHP example 2 <? Code here ? >
PHP example 3 <html> <head><title>PHP example</title></head> <body> <? php print ”<h 1>Hello World</h 1>"; ? > </body> </html>
PHP example 3 (in browser) <html> <head><title>PHP example</title></head> <body> <h 1>Hello World</h 1> </body> </html>
Datatypes and variables PHP is a ”loosely-typed” language n When you declare a variable, you don’t give datatype (but still variables do have datatypes) n When code is executed data type is defined according the contents n Example $somevariable=0; $somestring=”Teppo”; n
Basic datatypes n n String Integer Float Boolean
Datatypes and variables n n Variable declaring starts with $character You invent the name for the variable Good practise is to give initial value Example: $somenumber=0; $sometext=””;
Operators = n n n n = Operator $firstname=”Charlie”; $lastname=”Brown”; $name=$firstname. $lastname; $somenumber=1; $someothernumber=3; $total=$somenumber + $someothernumber; $formvalue=$_POST[’fieldname’];
Arithmetic operations n n n + addition - subtraction / division * multiplication % remainder
Control structures n Conditional structures n n n Loops n n n If Switch While For Functions
Basic syntax if (expression) {. . . } else {. . . }
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”; } ? >
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”; } } ? >
Logical operations n n n || && ! OR AND NOT
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”; } ? >
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”; } ? >
Loops n n n While For …
While initialization while (condition) { //one or more statemens increment }
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”; ? >
For (initialization; condition; increment) { //one or more statemens }
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”; ? >
Functions n Two types of functions n n Built-in (in PHP more that 700 prewritten functions available) Custom Gives well-defined structure to software Adds reusability
Function syntax Return value Name Parameters
Example: mail() n 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. "rn". 'X-Mailer: PHP/'. phpversion(); if (mail($to, $subject, $message, $headers)) … ? >
Input validation with functions n n Check if input given by user is numerical Validation user following PHP-functions n floatval(), intval(), strval()