Saturday, March 9, 2024

What is PHP if Statement? Learn with Simple Examples

PHP if statements


What is PHP if Statement?

 A PHP if statement is a conditional statement used to execute a block of code only when a specified condition is true. It helps control the flow of a program by making decisions based on variables, values, or expressions. 

    The if statement is widely used in PHP to perform actions like validating user input, checking login credentials, or displaying dynamic content. Its basic syntax includes a condition inside parentheses followed by code inside curly braces. If the condition evaluates to true, the code runs; otherwise, it is skipped. PHP if statements are essential for building interactive and responsive web applications.

Syntax :-

      statement ;
          if (condition) 
        
         {
          /*block of code */

         }


👉 Related topic for better understanding:
PHP Arithmetic Operators Guide

Why is PHP if Statement Important?


✔ Enable logical decision-making
✔ Make web applications dynamic
✔ Help validate user data
✔ Control program execution flow
✔ Improve coding efficiency and flexibility
✔ Helps in decision making in PHP
✔ Makes websites dynamic and interactive
✔ Supports logical conditions and expressions
✔ Essential for real-world PHP applications
✔ Improves problem-solving and coding skills

Type is PHP Statement 

1] if statement
2] if ... else  Statement
3] if ... elseif...else statement

PHP if Statements Example :-


Q] Accept name of student from user and show student name in same page.


<html>
   
    <form action="collect_data_student.php"
method="get">
    <label> Enter name</label><br>    
    <input type="text" name="snm" require>
<br><br>
    <input type="submit" value="show" name="b1"
>
   <?php
        if (isset($_GET["b1"]))
        {
                $name=$_GET["snm"];
                echo " <br> hi :-". $name;
        }
   ?>

</form>
</html> 

PHP if statement example output

php if statement /output


  • PHP conditional statements  Example 
  • 2]   Accept price and amount the user from and calculate bill .price and bill amount person 

          get 10% discount of total bill amount 5000. 


        <html>
            <head>
        <title>calculate the bile </title>
    </head>
        <form action="bill.php" method="get">
            <label>Price</label>
            <input type="number" name="price"
    required><br><Br>

            <label>quantity</label>
            <input type ="number" name="quantity"
    required><br><br>
            <input type="submit" value="show"
    name="b1"required>
    </form>

    <?php
    if(isset($_GET["b1"]))
    {
        /*input */
        $pr=$_GET["price"];
        $qty=$_GET["quantity"];

        /* operator */
        $bill=$pr*$qty;
        $discount= ($bill>5000)?10:0;
        $disamt=($bill*$discount)/100;
        $tbill=$bill-$disamt;
        /* output*/
        echo" price=" .$pr;
        echo "<br>quantity =" .$qty;
        echo "<br>Bill=" .$bill;
        echo "<br> discount =" .$discount;
        echo "<br>discount amount =". $disamt;
        echo "<br>total bill = ". $tbill;
    }
    ?>
    </html>




    PHP if statement example output

    php if statement use program/output


    FAQ (Frequently Asked Questions)


    1]. What is PHP if statement in simple words?
    It is a way to tell the program to run code only if a condition is true.

    2]. Why do we use if statement in PHP?
    We use it to make decisions and control how the program behaves.

    3]. Is PHP if statement necessary for beginners?
    Yes, it is one of the basic and most important concepts in PHP.

    4]. Can PHP work without if statement?
    Yes, but it will not be able to make decisions or handle conditions.

    Conclusion


    PHP if statements are essential for building smart and interactive web applications. They allow your code to think, decide, and act based on conditions. Learning and understanding if statements is a key step for every PHP beginner.

    No comments:

    Post a Comment