Learn with Yasir
Share Your Feedback

PHP Basics Coding Exercises – Practice PHP with Hands-On Code Examples


🧪 Coding Exercises

🟢 Beginner Exercises

  1. Hello World
  2. Write a PHP script that outputs "Hello, World!" using the `echo` statement.

    Requirements

    • - The script should output "Hello, World!" when executed.
    Input
    (No input is required for this task)
    
    Expected Output
    Hello, World!
    

    📚 View Solution


  3. Simple Arithmetic
  4. Write a PHP script that calculates the sum, difference, and product of two numbers and prints the results.

    Requirements

    • - Use the variables `$a` and `$b` to store numbers. - Output the sum, difference, and product of `$a` and `$b`.
    Input
    $a = 5;
    $b = 3;
    
    Expected Output
    Sum: 8
    Difference: 2
    Product: 15
    

    📚 View Solution


  5. If-Else Condition
  6. Write a PHP script that checks if a number is positive, negative, or zero, and prints an appropriate message.

    Requirements

    • - Use an `if-else` statement to check if the number is positive, negative, or zero. - Output the correct message for each case.
    Input
    $num = -3;
    
    Expected Output
    The number is negative.
    

    📚 View Solution


  7. String Manipulation
  8. Write a PHP script that accepts a string and prints the following: - The string in all uppercase. - The string in all lowercase. - The length of the string.

    Requirements

    • - Use `strtoupper()`, `strtolower()`, and `strlen()` functions.
    Input
    $str = "Hello PHP!";
    
    Expected Output
    Uppercase: HELLO PHP!
    Lowercase: hello php!
    Length: 10
    

    📚 View Solution


  9. For Loop Example
  10. Write a PHP script that uses a `for` loop to print numbers from 1 to 10.

    Requirements

    • - Use a `for` loop to iterate from 1 to 10. - Print each number in a new line.
    Input
    (No input is required for this task)
    
    Expected Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    📚 View Solution


🟡 Intermediate Exercises

  1. Array Operations
  2. Write a PHP script that creates an array of five integers, and then prints the sum and the largest number in the array.

    Requirements

    • - Use the `array_sum()` function to calculate the sum of the array. - Use `max()` to find the largest number.
    Input
    $arr = [1, 2, 3, 4, 5];
    
    Expected Output
    Sum: 15
    Largest number: 5
    

    📚 View Solution


🔴 Advanced Exercises


🧠 Practice & Progress

Explore More Topics

PHP Basics

More ...