PHP Basics Interview Questions

🧾 PHP Basics – Interview Questions


🔹 1. Basic Questions (Very Important)

1️⃣ What is PHP?
👉 PHP (Hypertext Preprocessor) is a server-side scripting language used to build dynamic websites.


2️⃣ What are variables in PHP?
👉 Variables store data and start with $
Example:

$name = "Aditya";

3️⃣ Is PHP case-sensitive?
👉 Variables are case-sensitive ($name$Name)
👉 Keywords are NOT case-sensitive (echo = ECHO)


4️⃣ What are data types in PHP?
👉 Common types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object

5️⃣ Difference between echo and print?

echoprint
FasterSlower
No return valueReturns 1
Can print multiple valuesOnly one

🔹 2. Intermediate Questions

6️⃣ What is a constant in PHP?
👉 A constant is a fixed value that cannot be changed.

define("SITE", "SOA Technology");

7️⃣ What is the difference between == and ===?

  • == → compares value
  • === → compares value + data type
10 == "10"   // true
10 === "10" // false

8️⃣ What are operators in PHP?
👉 Symbols used to perform operations.

Types:

  • Arithmetic (+ - * / %)
  • Comparison (== != > <)
  • Logical (&& || !)

9️⃣ What is the use of var_dump()?
👉 It shows data type + value

var_dump(10); // int(10)

🔟 What are comments in PHP?
👉 Used to explain code

// single line
# single line/*
multi-line
*/

🔹 3. Tricky Questions (Asked in Interviews)

1️⃣1️⃣ What will be the output?

$x = 5;
echo $x++;

👉 Output: 5 (post-increment)


1️⃣2️⃣ What will be the output?

$x = 5;
echo ++$x;

👉 Output: 6 (pre-increment)


1️⃣3️⃣ Difference between single quotes and double quotes?

  • ' ' → No variable parsing
  • " " → Variables are parsed
$name = "Aditya";echo 'Hello $name'; // Hello $name
echo "Hello $name"; // Hello Aditya

1️⃣4️⃣ Can we redeclare a constant?
👉 ❌ No


1️⃣5️⃣ What happens if you use an undefined variable?
👉 PHP shows a warning/notice


🔹 4. Practical / Coding Questions

1️⃣6️⃣ Write a program to check even or odd

$num = 4;
if($num % 2 == 0){
echo "Even";
} else {
echo "Odd";
}

1️⃣7️⃣ Swap two numbers

$a = 5;
$b = 10;$a = $a + $b;
$b = $a - $b;
$a = $a - $b;

1️⃣8️⃣ Find largest of two numbers

$a = 10;
$b = 20;echo ($a > $b) ? $a : $b;

🔥 Pro Interview Tips

  • Always explain why, not just code
  • Be clear on == vs === (very important)
  • Know basic syntax + output prediction
  • Practice small programs