Lecture Five Presented By: Mohamed Ahmed
Lecture Five Presented By: Mohamed Ahmed
Presented by:
Mohamed Ahmed.
Functions
PHP functions are similar to other programming
languages.
A function is a block of statements that can be used
repeatedly in a program.
A function will be executed by a call to the function.
Chapter 2
Functions
Two types:
Built in functions
Custom defined functions
Chapter 2
Syntax of Functions
function functionName() {
code to be executed;
}
Chapter 2
How to use some built-in functions
Chapter 2 Slide 5
Chapter 2 Slide 6
Chapter 2 Slide 7
Examples of Functions
<?php
$date=date("h:i:s A");
echo $date;
?>
<?php
echo "The time is : .date("H:i:sa");
?>
Chapter 2 8
Examples of Functions
<?php
//function
Function myname(){
echo"Sahardiid";
}
Myname();
?>
Chapter 2 Slide 9
Examples of Functions
<?php
function writeMsg() {
echo "Hello world!";
}
Chapter 2 Slide 10
Example of Add Function
<?php
//function
Function add($x , $y){
echo $x+$y;
}
add(8,4);
?>
Chapter 2 11
<?php
Function add(){
$x=10;
$y=14;
$z=$x+$y;
Return $z;
}
echo add();
?>
Chapter 2 12
Some of string functions
Strlen() function
The strlen() function is used to return the length of a string
<?php
Echo strlen("Hello Somaliland!");
?>
Strpos() function
The strpos() function is used to search for character within a
string.
<?php
Echo strpos("Hello world!","world");
?>
Chapter 2 13
Substr() function
Substr() function is used to cute some of string
<?php
$text="How are you , I hope to be a good
health";
$cut=substr($text,0,22);
echo $cut;
?>
Chapter 2 Slide 14
Gmdate() is used to get day_name and month_name
and the year.
Example
<?php
//gmdate
echo gmdate("D M Y");
?>
Chapter 2 15
Example: Y/m/d g:i:s becomes
[year] => Y
[month] => m
[day] => d
[hour] => g
[minute] => i
[second] => s
<?php
echo today is: .date("Y-m-d");
?>
Chapter 2 16
isset() function
The isset () function is used to check whether a variable is set
or not..
Chapter 2 17
Example:
<form name="new user" method="post"
action="step2_check.php"> <input type="text"
name="mail"/> <br /> <input type="password"
name="password"/><br /> <input type="submit"
value="continue"/>
</form>
Checking example:
if (isset($_POST["mail"])) {
echo "Yes, mail is set"; }
else{
echo "N0, mail is not set";
}
Chapter 2 18
The include function
include 'index.php'; // parentheses are optional
include('index.php'); // index.php in the current
// directory
Chapter 2 Slide 19
Different between Include and Require
<?php
// include and require
function add(){
$x=10;
$y=20;
$z=$x+$y;
return $z;
}
echo add();
?>
Chapter 2 20
Different between Include and Require
<?php
include("test.php");
echo "<br /> Hello Students";
?>
Chapter 2 21
Different between Include and Require
<?php
require("test.php");
echo "<br /> Hello Students";
?>
Chapter 2 22
If change include contain it display error but
continually running the other parts of program.
Chapter 2 23
Control statements
Chapter 2 Slide 24
How to code control statements
Chapter 2 Slide 25
Conditional Statements
Chapter 2 26
if...else statement - use this statement to execute some
code if a condition is true and another code if the condition is
false.
Chapter 2 Slide 27
The if Statement
Use the if statement to execute some code only if a specified
condition is true.
Syntax
if (condition) {code to be executed if condition is true;}
Example:
<?php
// if condition
$user="sahardiid";
$pass="123";
If ($user=="sahardiid" and $pass=="123"){
Echo "welcome $user , your password is $pass";
}
?>
Chapter 2 Slide 28
The if...else Statement
Use the if....else statement to execute some code if a
condition is true and another code if a condition is false.
Syntax if (condition) {
code to be executed if condition is true;}
Chapter 2 Slide 30
Example
<?php
$x=10;
if ($x==10)
echo "Have a nice 10";
else if ($x==20)
echo "Have a nice 20";
else echo "Have a nice Number";
?>
Chapter 2 31
Selection Statements
The PHP Switch Statement
Use the switch statement to select one of many blocks of code to
be executed.
Syntax
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2: code to be executed if n=label2;
break;
default: code to be executed if n is different from both label1 and
label2; }
Chapter 2 32
<?php
$x=3;
Switch($x){ Example of Switch
Case 1:
echo "x equal to 1";
Break;
Case 2:
echo "x equal to 2";
Break;
Case 3:
echo "x equal to 3";
Break;
default:
echo "Error";
}
?>
Chapter 2 33
Chapter 2 Slide 34
PHP Loops
Often when you write code, you want the same block of
code to run over and over again in a row.
In PHP, we have the following looping statements:
for - loops through a block of code a specified number of
times
while - loops through a block of code while a specified
condition is true
do...while - loops through a block of code once, and then
repeats the loop as long as a specified condition is true
foreach - loops through a block of code for each element in
an array
Chapter 2 Slide 35
Example of For Loop
<?php
for($x=0; $x<11; $x++){
echo "Hello students"."<br />";
}
?>
Chapter 2 Slide 36
Example of While Loop
<?php
$x=0;
while($x<10){
echo "Hello for all Students"."<br/>";
$x++;
}
?>
Chapter 2 Slide 37
Example of Do - While Loop
<?php
$x=1;
do {
echo "Hello Somalilanders"."<br />";
$x++;
}
while($x<10)
?>
Chapter 2 Slide 38
Example of foreach Loop
<?php
// foreach loop
$x=array(1,2,3,4,5);
foreach($x as $y) {
echo $y."<br />";
}
?>
Chapter 2 Slide 39
Example of foreach Loop array
<ol>
<?php
// foreach loop array
$city=array("Hargeisa“. "Gabiley”.
"Burco","Borama","Berbera","Ceerigaabo","Laascaanood")
;
foreach($city as $x) {
echo "<li> $x </li>";
}
?>
</ol>
Chapter 2 Slide 40
Radio Button is single choice
<form action="page.php" method=POST>
Select product
<br>
<select name="a[]" multiple>
<option> Radio </option>
<option> TV </option>
<option> Keyboard </option>
<option> DVD </option>
<option> Screen </option>
</select>
<br>
<input type="submit" value="Go!">
</form>
Chapter 2 41
<html>
your choice is :<br />
<?php
Chapter 2 42
<html>
Example of calculator
<body>
<center>
<form action="result.php" method="POST">
<table bgcolor="skyblue">
<tr>
<td align="left">Num1:</td>
<td><input type="text" value="" name="x"> </td>
</tr>
<tr>
<td align="left">Num2:</td>
<td><input type="text" value="" name="y"> </td>
</tr>
<tr>
<td><input type="submit" value="Calc"> </td>
</tr>
</table>
<input type="radio" name="group1" value="add" checked > add
<input type="radio" name="group1" value="sub" > sub
<input type="radio" name="group1" value="multi" > multi
<input type="radio" name="group1" value="dev" > dev
</form>
</center>
</body>
Chapter 2
</html> 43
<?php
function calc(){
$x=$_POST['x'];
$y=$_POST['y'];
$group1=$_POST['group1'];
case "sub":
echo $x-$y;
break;
case "multi":
echo $x*$y;
break;
case "dev":
echo $x/$y;
break;
default:
echo "Error";
}}}
calc();
?>
Chapter 2 44
Assignment_5 Calculation Project
Build in the following form then calculate the future value
Chapter 2 Slide 45
Output of Assignment_5
Chapter 2 Slide 46
Any Question??
Thanks