Category: Examples

  • If-Else Example

    <?php
    $marks = 75;
    
    if ($marks >= 50) {
    
    echo "You passed!";
    } else {
    echo "You failed!";
    } ?>

    Explanation:

    • If condition is true, “You passed!” will be printed.
    • Otherwise, “You failed!” is shown.
  • Data Types

    <?php
    $number = 100;        // Integer
    $price = 99.99;       // Float
    $name = "PHP";        // String
    $is_active = true;    // Boolean
    ?>
    

    Explanation:
    PHP supports int, float, string, boolean, array, object, null types.

  • Variables in PHP

    <?php
    $name = "Saim";
    $age = 25;
    
    echo "My name is $name and I am $age years old.";
    ?>
    

    Explanation:

    • Variables in PHP start with $.
    • Strings inside double quotes can include variables.
    • Output: My name is Saim and I am 25 years old.
  • Hello World Example

    <?php
    echo "Hello, World!";
    ?>
    

    Explanation:

    • <?php ... ?> is used to write PHP code.
    • echo outputs text to the browser.
    • When you run this, it will display Hello, World!.