practice questions on PHP Basics

🧾 PHP Basics – Practice Questions


🔹 Level 1: Beginner

1️⃣ Create variables for:

  • Your name
  • Your age
  • Your city
    Then print them using echo.

2️⃣ Create two numbers and:

  • Add them
  • Subtract them
  • Multiply them

3️⃣ Write a PHP script to:

  • Store a string "Welcome to PHP"
  • Print it using both echo and print

4️⃣ Create a constant named SITE_URL and assign your website URL. Then print it.


5️⃣ Identify the data type of these variables using var_dump():

$a = 100;
$b = "Hello";
$c = 10.5;
$d = true;

🔹 Level 2: Intermediate

6️⃣ Compare two variables:

$a = 10;
$b = "10";
  • Check using == and ===
  • Explain the output

7️⃣ Write a program to check:

  • If a number is greater than 50 or not (use comparison + echo result)

8️⃣ Use logical operators:

  • Create two variables $x = true, $y = false
  • Test AND, OR, NOT

9️⃣ Create a calculator using variables:

  • Perform all arithmetic operations (+, -, *, /, %)

🔟 Write a PHP script with:

  • Single-line comment
  • Multi-line comment
    Explain what your code does

🔹 Level 3: Challenge

1️⃣1️⃣ Create 3 variables:

  • Name (string)
  • Marks (integer)
  • Pass status (boolean)

Print all values with proper labels.


1️⃣2️⃣ Swap two variables without using a third variable

$a = 5;
$b = 10;

1️⃣3️⃣ Check if a number is even or odd using %


1️⃣4️⃣ Create a simple bill:

  • Product price = 100
  • GST = 18%
  • Calculate total price

1️⃣5️⃣ Write a script that:

  • Uses all operators (arithmetic + comparison + logical) in one example

🚀 Bonus (Important for Interviews)

👉 What will be the output?

$x = 5;
echo $x++;
echo ++$x;

💡 Tip

Try to run these on your server (XAMPP / localhost)