Upload
brittney-allen
View
222
Download
3
Embed Size (px)
Citation preview
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
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
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
PHP example1
<?phpCode here
?>
PHP example2
<?Code here
?>
PHP example3
<html><head><title>PHP example</title></head><body>
<?phpprint ”<h1>Hello World</h1>";?>
</body></html>
PHP example3 (in browser)
<html><head><title>PHP example</title></head><body>
<h1>Hello World</h1>
</body></html>
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”;
Basic datatypes
String Integer Float Boolean
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=””;
Operators = = Operator $firstname=”Charlie”; $lastname=”Brown”; $name=$firstname . $lastname; $somenumber=1; $someothernumber=3; $total=$somenumber +
$someothernumber; $formvalue=$_POST[’fieldname’];
Arithmetic operations
+ addition - subtraction / division * multiplication % remainder
Control structures Conditional structures
If Switch
Loops 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
|| 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
While For …
While
initializationwhile (condition){
//one or more statemensincrement
}
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
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
Two types of functions 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() 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))…
?>
Input validation with functions
Check if input given by user is numerical
Validation user following PHP-functions floatval(), intval(), strval()