PHP_Dessalew_Slide_1
PHP_Dessalew_Slide_1
PROGRAMMING II
BY: Dessalew G.
CHAPTER ONE
Introduction
2
List of Contents
3
Introduction To Server Side Scripting
7
How do webserver works?
8
How do webserver works?
10
How do webserver works?
◉ For this reason, you cannot see the PHP code of a website,
only the resulting HTML that the PHP scripts have
produced.
11
Server side scripting languages
PHP(Hypertext Preprocessor)
12
PHP (Hypertext Preprocessor)
14
What Can PHP Do?
15
PHP Features
16
Why PHP ?
18
What you need to get started
20
How to run PHP programs in XAMPP
21
Using PHP Basic syntax
◉ A PHP scripting block always starts with <?php and ends with ?>.
◉ A PHP scripting block can be placed anywhere in the document.
◉ A PHP script is executed on the server, and the plain HTML
result is sent back to the browser.
<?php
//Body of the script
?>
◉ A PHP file normally contains HTML tags, just like an HTML file,
and some PHP scripting code.
22
PHP Output Statements
24
PHP Output Statements
26
PHP Output Statements
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>"; // displaying text
$txt2 = "W3Schools.com"; echo statement does not return
$x = 5; any value. It will generate an error if you
$y = 4; try to display its return value.
print "Study PHP at " . $txt2 . "<br>"; $lang = "PHP";
print $x + $y; $ret = echo $lang." web development ";
?> echo "</br>";
echo "Value return by print statement: “
.$ret;
<?php Output= errors
$lang = "PHP";
$ret = print $lang." is a web development language.";
print "</br>";
print "Value return by print statement: ".$ret;
27
?>
PHP Comments
28
PHP Comments
<html><head>
<title>comments</title> </head>
<body bgcolor=“#a54353”>
<?php
//This is the first types comment
#This is the second types comment
/* This is the third types comment for multiple line
*/
?>
</body> </html> 29
PHP Variables/Working with Variables
31
Some Rule for Naming Variables
32
Some Rule for Naming Variables
33
Data types
35
Data types
36
Data types
◉ Example:
$testvar = 3 + 4;
echo “The value is $testvar”; //output: The value
is 7
$testvar = “three”;
echo “The value is $testvar”; //output: The value
is three
37
Displaying variable values
39
Checking Variable Content
42
Checking Variable Content
<?php
$a = "test";
$b = "anothertest";
echo isset($a); // TRUE
echo isset($a, $b); //TRUE
unset ($a); //cleared/removed so it undefined
echo isset($a, $b); //FALSE
$foo = NULL;
print isset($foo); // FALSE
$var = 0;
if (empty($var)) // evaluates TRUE
echo '$var is either 0 or not set at all';
?> 43
Type Casting
$foo = "321.456number";
settype($foo, "float");
print("<br>Float: $foo"); //output: Float: 321.456
$foo = "321.456number";
settype($foo, "integer");
print("<br>Integer: $foo"); //output: Integer: 321
$foo = 321.456;
settype($foo, "string");
print("<br>String: $foo"); //output: String: 321.456
<?php
function local_var()
{ <?php
53
PHP Global Variables
56
PHP Global Variables
58
PHP Static Variables
59
PHP Static Variables
60
PHP Static Variables
<?
function keep_track() {
STATIC $count = 0;
$count++; // print $count; then $count++;
print $count;
print “<br> "; This will produce the following result.
1
} 2
keep_track(); 3
Then, each time the function is called,
keep_track(); that variable will still have the information
keep_track(); it contained from the last time the
function was called.
?> 61
PHP Static Variables
Example:
<?php
function static_var() {
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in static variable You have to notice that $num1
$num1++; regularly increments after each
//increment in non-static variable function call, whereas $num2
$num2++;
echo "Static: " .$num1 ."</br>";
does not.
echo "Non-static: " .$num2 ."</br>"; This is why because $num2 is
} not a static variable, so it freed its
//first function call memory after the execution of
static_var();
//second function call
each function call.
static_var(); ?> 62
Working with Constants
64
Working with Constants
73
Pre- and Post-Increment and Decrement operators
74
Comparison Operators
<?php
$var = "30";
$num = 30;
if($var == $num)
print "They are equal.<br>";
if($var === $num)
print "They are equal and same data type.<br>";
$var = (int) “30”;
if($var === $num) Output:
They are equal
print "2. They are equal and same data type.<br>";
2. They are equal and same data type.
?> 76
Logical Operators
◉ The logical operators are used to combine the results of logical conditions.
operator description
xor Is true if either (but not both) of its arguments are true
! Is true if its single argument (to the right) is false and false if its argument is
true
&& Same as and
|| Same as or
77
The Ternary operator
78
The Ternary operator
79
PHP Strings
◉ Reversing Strings
○ Use the strrev() function reverses a string
◉ Counting Words in a String
○ Use str_word_count() function counts the number of
words in a string
◉ length of the string
○ Use strlen() Function returns the length of a string.
◉ Replace Text Within a String
○ The PHP str_replace() function replaces some characters
with some other characters 81
in a string.
PHP Strings
<?php
echo str_replace("world","Peter","Hello world!");
?>
◉ To concatenate use dot (.)
<? php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
82
PHP Strings
<?php
// Input string
$subjectVal = "It was nice meeting you. May you shine
bright.";
// Using str_replace() function
$resStr = str_replace('you', 'him', $subjectVal);
print_r($resStr); ?>
83
PHP Strings
87
Use Conditionals
$num=12;
if($num<100){
echo "$num is less than 100";
}
<?php
$date = date("H");
if ($date<20) Output "Have a good day!"
{ if the current time (HOUR)
echo "have a good day"; is less than 20:
}
?>
88
Use Conditionals
<?php
$t = date("H");//");//24-hour format of an hour (00 to 24)
if ($t < "20") { Output "Have a good day!" if the
echo "Have a good day!"; current time is less than 20, and
} else { "Have a good night!" otherwise:
echo "Have a good night!";
} <?php Note: The date() function
?> $num=12; formats a local date and time,
if($num%2==0){
echo "$num is even number";
and returns the formatted
}else{ date string.
echo "$num is odd number";
} ?> 90
Use Conditionals
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") { Output "Have a good morning!" if the
echo "Have a good day!"; current time is less than 10,
and "Have a good day!“
} else { if the current time is less than 20.
echo "Have a good night!"; Otherwise it will output "Have a good night!":
}
92
Use Conditionals
93
Use Conditionals
switch (expression)
{
case label1:
code to be executed if expression =label1;
break;
case label2:
code to be executed if expression =label2;
break;
...
default:
code to be executed if expression is different from all labels;}
94
Use Conditionals
96
Use Conditionals
97
Use Conditionals
<?php <?php
$favcolor = "red"; $num=20;
switch ($favcolor) { switch($num){
case "red": case 10:
echo "Your favorite color is red!"; echo("number is equals to 10");
break;
break;
case "blue":
echo "Your favorite color is blue!"; case 20:
break; echo("number is equal to 20");
default: break;
echo "Your favorite color is default:
neither red, blue, nor green!"; echo("number is not equal to 10, 20
}?> 98
or 30");}?>
PHP Loops
◉ PHP while loop can be used to traverse set of code like for
loop.
◉ The while loop executes a block of code repeatedly until
the condition is FALSE. Once the condition gets FALSE, it
exits from the body of loop.
◉ It should be used if the number of iterations is not known.
◉ The while loop is also called an Entry control loop because
the condition is checked before entering the loop body.
This means that first the condition is checked. If the
condition is true, the block of
101 code will be executed.
PHP Loops
102
PHP Loops
105
PHP Loops
107
PHP Array
108
PHP Array
109
PHP Array
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
<?php
<?php
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
110
PHP Array
<?php
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";$salary["Sonoo"]="350000";
$salary["John"]="450000";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
$salary["Kartik"]="200000";
?> echo "Sonoo salary:
".$salary["Sonoo"]."<br/>";
echo "John salary:
".$salary["John"]."<br/>";
echo "Kartik salary:
".$salary["Kartik"]."<br/>";
112
Create a Simple Form using PHP
<?php
echo “<form action=’login.php’ method=’POST’>\n
<input type=’text’ name=’Username’ />\n
<input type=’password’ name=’Passwd’ />\n
<input type=’submit’ value=’Login’ />\n
</form>\n”;
?>
113
Create a simple Form using HTML
114
Get and Post Methods in PHP
117
Get and Post Methods in PHP
120
Get and Post Methods in PHP
◉ $_REQUEST variable
○ The $_REQUEST variable is a superglobal variable,
which can hold the content of both $_GET and $_POST
variable.
○ In other words, the PHP $_REQUEST variable is used to
collect the form data sent by either GET or POST
methods.
○ It can also collect the data for $_COOKIE variable
because it is not a method-specific variable.
122
Get and Post Methods in PHP
<?php
echo “User Name ". $_REQUEST[‘uname']. "<br />";
echo “Password ". $_REQUEST[‘passwd']. " ";
?>
<form action=“pos.php" method=“POST/GET">
User Name: <input type="text" name=“uname" />
Password: <input type=“password" name=“passwd" />
<input type="submit" />
</form> 123