0% found this document useful (0 votes)
54 views

Lecture Five Presented By: Mohamed Ahmed

<input type="radio" id="male" name="gender" value="male"> <label for="male">Male</label><br> <input type="radio" id="female" name="gender" value="female"> <label for="female">Female</label><br> <input type="submit" value="Submit"> </form> Chapter 2 Slide 41

Uploaded by

Fadxiya Muxumed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Lecture Five Presented By: Mohamed Ahmed

<input type="radio" id="male" name="gender" value="male"> <label for="male">Male</label><br> <input type="radio" id="female" name="gender" value="female"> <label for="female">Female</label><br> <input type="submit" value="Submit"> </form> Chapter 2 Slide 41

Uploaded by

Fadxiya Muxumed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Lecture Five

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.

 There are two parts which should be clear to you :-


1. Creating a PHP Function
2. Calling a PHP Function

Chapter 2
Functions
Two types:
Built in functions
Custom defined functions

Functions minimize the amount of repetition of code.

function consists of function name followed by


parentheses.
Some functions may need values passed to it.

Chapter 2
Syntax of Functions
 function functionName() {
    code to be executed;
}

 Note: A function name can start with a letter or


underscore (not a number).

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!";
}

writeMsg(); // call the function


?>

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..

 isset() tests if a variable is set and not null:

 empty() can return true value if a variable has not been


set, contain a null value or contains a empty string.

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

The require function


require('index.php'); // index.php in the current
// directory

The exit function


exit; // parentheses are optional
exit();
exit('Unable to connect to DB.');
// passes a message to the browser

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.

 If change require contain it display error but is not


continually running the other parts of program.

Chapter 2 23
Control statements

Chapter 2 Slide 24
How to code control statements

Like all programming languages, PHP provides control


statements that let you control how the statements in an
application are executed.
To start, this topic shows how to code the conditional
expressions that are used in control statements.

Chapter 2 Slide 25
Conditional Statements

Conditional statements are used to perform different actions


based on different conditions.
Very often when you write code, you want to perform different
actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:

 if statement - use this statement to execute some code only


if a specified condition is true

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.

if...else if....else statement - use this statement to select


one of several blocks of code to be executed.

switch statement - use this statement to select one of


many blocks of code to be executed.

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;}

else code to be executed if condition is false;


Example
<?php
$x=10;
If($x==10){
echo $x;}
else
echo "x is not equal 10";
?>
Chapter 2 29
The if...elseif....else Statement
Use the if....elseif...else statement to select one of several
blocks of code to be executed.
Syntax if (condition) code to be executed if condition is
true;
elseif (condition)
code to be executed if condition is true;
else code to be executed if condition is false;

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

for($i=0; $i<5; $i++){


echo "$a[$i]" ."<br />";
}
?>

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'];

if($x==" " or $y==" "){


echo "Type your values..";
exit;
}
else{
switch($group1){
case "add":
echo $x+$y;
break;

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

You might also like