![Page 1: PHP Teppo Räisänen LIIKE/OAMK 2011. PHP PHP is a programming language for making dynamic and interactive Web pages](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022081515/56649f315503460f94c4c228/html5/thumbnails/30.jpg)
Input validation with functions
Check if input given by user is numerical
Validation user following PHP-functions floatval(), intval(), strval()