Tuesday, April 9, 2024

PHP While loop

 

PHP While loop

PHP While loop


 Information :-

1] PHP while loop can be used to return from the same process as the loop.

2] The while loop repeats a block of code until the condition is FALSE. 

3] Exit the body loop when the condition is FALSE.

4]
This
 loop should be used if the number of iterations is unknown.

5] Element
 loop is also called Entry check loop because 
the condition is checked          before entering the loop body. 

6] This means diagnosing the condition first. If the condition is true,           the code block is executed.


Syntax :-



Flowchart :-

PHP while loop /output




PHP While Loop 

Example :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
/*decending order in php*/
 $no =10;
 while ($no>0)
 {
    echo $no."<br>";
    $no --;
 }

 /**10,9,8,7,6,5,4,3,2,1 */
 
 
 
?>

</body>
</html>


PHP While Loop 

OUTPUT :-


Wednesday, April 3, 2024

PHP switch statement

 switch statement in PHP

PHP switch statement


1] switch case is use in menu driven programming.

2] The value of the switch expression is calculated once's then this
    value is match  which case in switch block---- if there is match
    associated case is executed.

3] use break keyword to transfer keyword outside they switch block
    after case execution.

4] switch case value is the expression is case sensitive.

5] The switch statement in PHP is an effective construct that lets you carry out various operations depending on various circumstances.
 
6]  It's especially helpful when you have to assess several scenarios and run various code blocks according to the first real situation that appears. Now let's see how it functions.

      Syntax :-

statement 
switch (expression/value)
{
  case value:  
 statement;
  statement;
     break; 

  case value: 
 statement;
 statement;
    break;

      default :   
 statement;
 statement;
}         


PHP switch statement 
    
    Program

<?php
$no1=20;
$no2=5;
$choice=4;
switch($choice)
 {
    case 1 : $result =$no1+$no2;
     echo"Addition =".$result;
     break;
     case 2 : $result =$no1-$no2;
     echo"Substraction =".$result;
     break;
     case 3 : $result =$no1*$no2;
     echo"Multiplication =".$result;
     break;
     case 4 : $result =$no1/$no2;
     echo"division =".$result;
     break;
     default : echo "wrong choice";
 }
?>

PHP switch statement 
Output :- 

PHP switch statement  Example /output



Read more

Tuesday, April 2, 2024

PHP else if ladder

 PHP else if ladder

PHP else if ladder


PHP else if ladder :-


     PHP If ladder is a specific kind of nested if statement, where nesting only occurs in the else part. This statement is useful when an action needs to be chosen depending on a range of values.
      PHP, an “else if ladder” (also known as a “chain of if-else statements”) is a construct that allows you to evaluate multiple conditions in sequence. It’s particularly useful when you want to check different conditions one after the other and execute specific code blocks based on the first true condition encountered.


    Syntax :-

statement;

if (condition)

{
statement;
statement;

}
 else if (condition)
      statement;
     statement;
}
else
{
statement;
statement;
}


PHP else if ladder

Example :-
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.css">

</head>
<body class="bg-primary">
    <div class="bg-white col-md-4 mx-auto mt-5 p-4">
<h2>Positive Negative or zero number check </h2>
<form action="else-if-ladder-p-n.php" method="get">
   
    <input type="number" name="no" class="form-control" required>
</form>

<?php
$score = 85;

if ($score >= 90) {
    echo "Grade: A";
} elseif ($score >= 80) {
    echo "Grade: B";
} elseif ($score >= 70) {
    echo "Grade: C";
} elseif ($score >= 60) {
    echo "Grade: D";
} else {
    echo "Grade: F";
}
?>

</div>
</body>
</html>


PHP else if ladder :-
Output :-


else if ladder output



PHP While loop