DIWE - Fundamentals of PHP

rasansamarasinghe 743 views 45 slides Feb 04, 2017
Slide 1
Slide 1 of 45
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45

About This Presentation

Esoft Metro Campus - Diploma in Web Engineering - (Module VI) Fundamentals of PHP

(Template - Virtusa Corporate)

Contents:

Introduction to PHP
What PHP Can Do?
PHP Environment Setup
What a PHP File is?
PHP Syntax
Comments in PHP
echo and print Statements
PHP Variables
PHP Data Types
Changing Type...


Slide Content

Diploma in Web Engineering
Module VI: Fundamentals of PHP
RasanSamarasinghe
ESOFT Computer Studies (pvt) Ltd.
No 68/1, Main Street, Pallegama, Embilipitiya.

Contents
1.Introduction to PHP
2.What PHP Can Do?
3.PHP Environment Setup
4.What a PHP File is?
5.PHP Syntax
6.Comments in PHP
7.echo and print Statements
8.PHP Variables
9.PHP Data Types
10.Changing Type by settype()
11.Changing Type by Casting
12.PHP Constants
13.Arithmetic Operators
14.String Operators
15.Assignment Operators
16.Comparison Operators
17.Logical Operators
18.Operators Precedence
19.If Statement
20.If… Else Statement
21.If… Else if… Else Statement
22.Switch Statement
23.The ? Operator
24.While Loop
25.Do While Loop
26.For Loop
27.break Statement
28.continue Statement
29.Functions
30.User Defined Functions
31.Functions -Returning values
32.Default Argument Value
33.Arguments as Reference
34.Existence of Functions
35.Variable Local and Global Scope
36.The global Keyword
37.GLOBALS Array
38.Superglobals
39.Static Variables

Introduction to PHP
•PHP is a server-side scripting
language widely used for web
development.
•PHP is an acronym for "PHP
Hypertext Preprocessor“.
•Originally created by RasmusLerdorf
in 1994.
•PHP is open source and free to
download and use.

What PHP Can Do?
•PHP can generate dynamic page content.
•PHP can create, open, read, write, delete, and
close files on the server.
•PHP can collect form data.
•PHP can send and receive cookies.
•PHP can add, delete, modify data in your database.
•PHP can restrict users to access some pages on
your website.
•PHP can encrypt data.

PHP Environment Setup
Get a web host with PHP and MySQL support
Or
Install a web server on your own PC, and then
install PHP and MySQL

What a PHP File is?
•PHP files can contain HTML, CSS, JavaScript, and
PHP code.
•PHP files have extension ".php“
•PHP code are executed on the server, and the
result is returned to the browser as plain HTML.

PHP Syntax
<html>
<head><title>PHP Demo</title></head>
<body>
<?php
echo"Hello World!";
?>
</body>
</html>
index.php

Comments in PHP
// This is a single line comment
# This is also a single line comment
/*
This is a
multiple line
comment
*/

echo and print Statements
echo can output one or more strings
echo"Hello world!<br>";
echo"Hello”, “ world!”;
print can only output one string, and returns 1
print"Hello world!<br>";

PHP Variables
Variables are temporary memory locations storing
data
$science =50;
$maths=60;
$total =$science +$maths;
echo“Total is: $total”;

PHP Variables
•A variable starts with the $ sign
•A variable name must start with a letter or the
underscore character
•A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
•Variable names are case sensitive

PHP Data Types
•PHP is a Loosely Typed Language
•PHP automatically converts the variable to the
correct data type, depending on its value.
•It supports with String, Integer, Floating point,
Boolean, Array, Object and NULL data types.

PHP Data types
$pi =3.14;
$week =7;
$name =“RoshanPerera";
$city=‘kandy’;
$valid =10>5;
var_dump($pi);
var_dump($week);
var_dump($name);
var_dump($city);
var_dump($valid);

Changing Type by settype()
Syntax:
settype(mixed var, string type)
Ex:
$x =3.14;
settype($x,"integer");
var_dump($x);

Changing Type by Casting
Syntax:
var= (datatype) var
Ex:
$x =3.14;
$y =(integer)$x;
var_dump($y);

PHP Constants
Once they are defined they cannot be changed
define("GREETING", “Hello world!");
echoGREETING;
//Case insensitive constant
define("GREETING", “Hello world!", true);
echogreeting;

Arithmetic Operators
OperatorDescription Example
+ Addition A + B will give 30
- Subtraction A -B will give -10
* Multiplication A * B will give 200
/ Division B / A will give 2
% Modulus B % A will give 0
++ Increment B++ gives 21
-- Decrement B--gives 19
A = 10, B = 20

String Operators
OperatorName Example
. Concatenation
$str1 = "Hello"
$str2 = $txt1 . " world!"
.=
Concatenation
assignment
$str1 = "Hello"
$str1 .= " world!"

Assignment Operators
Operator Example
= C = A + B will assign value of A + B into C
+= C += A is equivalent to C = C + A
-= C -= A is equivalent to C = C -A
*= C *= A is equivalent to C = C * A
/= C /= Ais equivalent to C = C / A
%= C %= A is equivalent to C = C % A

Comparison Operators
OperatorName Example
== Equal (A == B) is false.
=== Identical (A == B) is false.
!= Not equal (A != B) is true.
<> Not equal (A<> B) is true
!== Not identical (A != B) is true.
> Greater than (A > B) is false.
< Less than (A < B) is true.
>= Greater than or equal (A >= B) is false.
<= Less than or equal (A <= B) is true.
A = 10, B = 20

Logical Operators
OperatorName Example
and And (A and B) is False
or Or (A or B) is True
&& And (A && B) is False
|| Or (A || B) is True
xor Exclusive Or(A xorB) is True
! Not !(A && B) is True
A = True, B = False

Operators Precedence
++, --
/, *, %
+, -
<, <=, >=, >
==, ===, !=
&&
||
=, +=, -=, /=, *=, %=, .=
and
xor
or
Priority

If Statement
if(Boolean_expression){
//Statements will execute if the
Boolean expression is true
}
Boolean
Expression
Statements
True
False

If… Else Statement
if(Boolean_expression){
//Executes when the Boolean expression
is true
}else{
//Executes when the Boolean
expression is false
}
Boolean
Expression
Statements
True
False
Statements

If… Else if… Else Statement
if(Boolean_expression1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression3){
//Executes when the Boolean expression 3 is true
}else{
//Executes when the none of the above condition is
true.
}

If… Else if… Else Statement
Boolean
expression 1
False
Statements
Boolean
expression 2
Boolean
expression 3
Statements
Statements
False
False
Statements
True
True
True

Switch Statement
switch(value) {
caseconstant:
//statements
break;
caseconstant:
//statements
break;
default:
//statements
}

The ? Operator
If condition is true ? Then Value one : Otherwise Value two
$h =10;
$x =$h<12?"morning" :"afternoon";
echo$x;

While Loop
while(Boolean_expression){
//Statements
}
Boolean
Expression
Statements
True
False

Do While Loop
do{
//Statements
}while(Boolean_expression);
Boolean
Expression
Statements
True
False

For Loop
for(initialization; Boolean_expression; update){
//Statements
}
Boolean
Expression
Statements
True
False
Update
Initialization

break Statement
Boolean
Expression
Statements
True
False
break

continue Statement
Boolean
Expression
Statements
True
False
continue

Functions
A function is a block of statements which
performing a specific task that can be executed by a
call to the function.
Two Categories of Functions:
1.PHP Built-in Functions
2.User Defined Functions

User Defined Functions
functionfunctionName(parameters…) {
//statements
}
functionName(arguments…); //calling function

Functions -Returning values
functionfunctionName(parameters…) {
//statements
returnvalue;
}
//calling function
$variableName= functionName(arguments…);

Functions –Default Argument Value
functionfunctionName($parameter =value) {
//statements
}
functionName(argument); //calling function
functionName(); //calling function without argument

Functions –Arguments as Reference
functionfunctionName(&$parameters…) {
//statements
}
functionName($arguments…); //calling function

Existence of Functions
functiontestFunction(){
//statements
}
$exist =function_exists("testFunction");
var_dump($exist);

Variable Local and Global Scope
$x =10; // global scope
functiontestScope() {
$y =20; // local scope
echo"Variable x: $x <br/>";
echo"Variable y: $y <br/>";
}
testScope();
echo"Variable x: $x <br/>";
echo"Variable y: $y <br/>";

The global Keyword
$x =10; // global scope
functiontestScope() {
global$x;
$x =20;
}
testScope();
echo "Variable x: $x <br/>";

GLOBALS Array
$x =10; // global scope
function testScope() {
$GLOBALS['x'] = 20;
}
testScope();
echo"Variable x: $x <br/>";

Superglobals
Superglobalsare built-in variables that are always available
in all scopes.
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

Static Variables
functiontestScope() {
static$x = 0;
$x++;
echo"$x<br/>";
}
testScope();
testScope();
testScope();

The End
http://twitter.com/rasansmn