KEMBAR78
PHP Lecture 02.pptx PHP Lecture 02 .pptx
PHP
Lecture 02
Conditional Statements
if Statement
• if statement executes some code if one condition is true.
if (condition) {
code to be executed if condition is true;
}
if...else Statement
• The if....else statement executes some code if a condition is true and another
code if that condition is false.
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Conditional Statements (Cont…)
if...elseif....else Statement
• The if....elseif...else statement executes different codes for more than
two conditions.
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
Conditional Statements (Cont…)
switch Statement
• Use the switch statement to select one of many blocks of code to be
executed.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Loops
• Loops execute a block of code while the specified condition is true.
while Loop
<?php
$x = 1;
while($x <= 5) {
echo $x ,"<br>";
$x++;
}
?>
Loops (Cont…)
do...while Loop
• The do...while loop will always execute the block of code once, it
will then check the condition, and repeat the loop while the specified
condition is true.
<?php
$x = 10;
do {
echo $x ."<br>";
$x++;
} while ($x <= 5);
?>
Loops (Cont…)
for Loop
• The for loop is used when you know in advance how many times the
script should run.
<?php
for ($x = 0; $x <= 5; $x++) {
echo $x. "<br>";
}
?>
Loops (Cont…)
foreach Loop
• The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
• Syntax
foreach ($array_name as $variable_name) {
code to be executed;
}
• Example
<?php
$colors = array("Apple", "Orange", "Mango");
foreach ($colors as $value) {
echo "$value <br>"; }
?>
Functions
• PHP has more than 1000 built-in functions.
• Besides the built-in PHP functions, we can create our own functions.
• A function is a block of statements that can be used repeatedly in a
program.
• A function will not execute immediately when a page loads.
• A function will be executed by a call to the function.
Creating a User Defined Function
• Syntax
function functionName() {
code to be executed;
}
• A function name can start with a letter or underscore (not a number).
• Function names are NOT case-sensitive.
• Example
<?php
function display() {
echo "Welcome to PHP";
}
display(); // call the function
?>
Function Arguments
• Information can be passed to functions through arguments.
• Arguments are specified after the function name, inside the
parentheses. Separate multiple arguments with a comma.
<?php
function display($msg) {
echo $msg;
}
display("Welcome to PHP"); // call the function
?>
Default Argument Value
• If we call the function display() without arguments it takes the default
value as argument:
<?php
function display($msg="Hello")
{
echo $msg."<br>";
}
display("Welcome to PHP"); // call the function
display();
?>
Returning values
• To let a function return a value, use the return statement.
• Value is returned back to the calling function/location.
<?php
function add($a, $b) {
$c = $a + $b;
return $c;
}
$result= add(5, 10);
echo $result. "<br>";
echo add(2,5);
?>

PHP Lecture 02.pptx PHP Lecture 02 .pptx

  • 1.
  • 2.
    Conditional Statements if Statement •if statement executes some code if one condition is true. if (condition) { code to be executed if condition is true; } if...else Statement • The if....else statement executes some code if a condition is true and another code if that condition is false. if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 3.
    Conditional Statements (Cont…) if...elseif....elseStatement • The if....elseif...else statement executes different codes for more than two conditions. if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
  • 4.
    Conditional Statements (Cont…) switchStatement • Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 5.
    Loops • Loops executea block of code while the specified condition is true. while Loop <?php $x = 1; while($x <= 5) { echo $x ,"<br>"; $x++; } ?>
  • 6.
    Loops (Cont…) do...while Loop •The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. <?php $x = 10; do { echo $x ."<br>"; $x++; } while ($x <= 5); ?>
  • 7.
    Loops (Cont…) for Loop •The for loop is used when you know in advance how many times the script should run. <?php for ($x = 0; $x <= 5; $x++) { echo $x. "<br>"; } ?>
  • 8.
    Loops (Cont…) foreach Loop •The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. • Syntax foreach ($array_name as $variable_name) { code to be executed; } • Example <?php $colors = array("Apple", "Orange", "Mango"); foreach ($colors as $value) { echo "$value <br>"; } ?>
  • 9.
    Functions • PHP hasmore than 1000 built-in functions. • Besides the built-in PHP functions, we can create our own functions. • A function is a block of statements that can be used repeatedly in a program. • A function will not execute immediately when a page loads. • A function will be executed by a call to the function.
  • 10.
    Creating a UserDefined Function • Syntax function functionName() { code to be executed; } • A function name can start with a letter or underscore (not a number). • Function names are NOT case-sensitive. • Example <?php function display() { echo "Welcome to PHP"; } display(); // call the function ?>
  • 11.
    Function Arguments • Informationcan be passed to functions through arguments. • Arguments are specified after the function name, inside the parentheses. Separate multiple arguments with a comma. <?php function display($msg) { echo $msg; } display("Welcome to PHP"); // call the function ?>
  • 12.
    Default Argument Value •If we call the function display() without arguments it takes the default value as argument: <?php function display($msg="Hello") { echo $msg."<br>"; } display("Welcome to PHP"); // call the function display(); ?>
  • 13.
    Returning values • Tolet a function return a value, use the return statement. • Value is returned back to the calling function/location. <?php function add($a, $b) { $c = $a + $b; return $c; } $result= add(5, 10); echo $result. "<br>"; echo add(2,5); ?>