PHP Interview Questions & Answers for Freshers

PHP interview questions for freshers: what PHP is, data types, variables, operators, strings, arrays, include vs require, GET vs POST, cookies, sessions and file basics.

What is the use of in_array() function in PHP?

in_array() checks if a value exists in an array. It returns true if the value is found, and false if not.

<?php
$fruits = ["Apple", "Banana", "Cherry"];

var_dump(in_array("Banana", $fruits));
var_dump(in_array("Mango", $fruits));

// Third parameter true = check the data type also
var_dump(in_array("1", [1, 2, 3]));
var_dump(in_array("1", [1, 2, 3], true));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(false)
bool(true)
bool(false)

By default in_array() compares loosely, so the string “1” matches the number 1. Pass true as the third parameter for a strict comparison. To get the position of the value, use array_search(), and to check a key use array_key_exists().

Which PHP function counts all the values of an array?

array_count_values() counts how many times each value appears in an array. It returns a new array where the values become keys and the counts become values.

<?php
$fruits = ["apple", "banana", "apple", "orange", "apple"];

print_r(array_count_values($fruits));
?>

Output:

Tutorials Class - Output Window

Array
(
[apple] => 3
[banana] => 1
[orange] => 1
)

It works only with strings and integers. If you only need the total number of elements, use count().

What is the latest version of PHP?

The current series of PHP is PHP 8. At the time of writing (November 2026) the latest stable branch is PHP 8.5. A new minor version (8.x) comes out every year, near the end of November.

Do not depend on a number written in an article, because it gets old. The official pages show the truth: php.net for the latest release, and php.net/supported-versions.php for the versions that still get fixes.

  • Old versions like PHP 5 and 7 do not get security fixes anymore, so a website on them is not safe.
  • You can see the version of your own server with echo PHP_VERSION; or php -v.
  • Read what changed in PHP 8 in new features of PHP 8.
Note: Before you upgrade a live website, test it on a copy. Old plugins and old code can break on a new PHP version.

What is the full form of PHP?

The full form of PHP is PHP: Hypertext Preprocessor.

When Rasmus Lerdorf created it in 1994, it was a set of small tools for his personal website, so the name meant Personal Home Page. Later the name was changed. PHP is called a recursive acronym, because the first letter P of the short form stands for PHP itself.

Start learning in the PHP Introduction chapter.

Which is the PHP function to delete a file from server?

Use the unlink() function. It deletes a file from the server and returns true on success. It is better to check first if the file exists.

<?php
$file = "sample-file.txt";

if (file_exists($file) && unlink($file)) {
    echo "File deleted";
} else {
    echo "File not found or could not be deleted";
}
?>

Output:

Tutorials Class - Output Window

File deleted

  • The deleted file can not be taken back.
  • PHP needs permission to delete the file, otherwise you get a warning.
  • Never pass a file name from user input without checking it.
  • Do not mix it with unset(), see unlink() vs unset().

How can we create a session in php?

A PHP session stores information about a user on the server, so you can use it on many pages. Start it with session_start() and save the data in the $_SESSION array.

<?php
// page1.php
session_start();

$_SESSION["username"] = "Robin";

echo "Session created.";
?>
<?php
// page2.php
session_start();

echo "Hello " . $_SESSION["username"];
?>

Output:

When page2.php is opened after page1.php

Hello Robin

Call session_start() at the top of every page that uses the session, before any output. Read PHP Session for login examples and security.

What is single line comment in PHP? Explain with syntax.

A single line comment tells PHP to ignore everything on that line after the comment symbol. In PHP you can start it with // or #. You do not need to close it.

<?php
// This is a single line comment
# This is also a single line comment

echo "Hello World!";   // comment after the code
?>

Output:

Tutorials Class - Output Window

Hello World!

The comment ends at the end of the line (or at the closing ?> tag). For many lines use /* ... */. In PHP 8 the text #[ starts an attribute, so write a space after the hash. See all types of comments in PHP Comments.

What is the difference between == and === Operators in PHP?

== (equal) compares only the values, and PHP may change the data type before comparing. === (identical) compares the value and the data type, so it is strict.

<?php
$num_1 = 12;      // integer
$num_2 = "12";    // string

var_dump($num_1 == $num_2);
var_dump($num_1 === $num_2);
var_dump(0 == "abc");
var_dump(null == false);
var_dump(null === false);
?>

Output:

Output in PHP 8

bool(true)
bool(false)
bool(false)
bool(true)
bool(false)

  • The first comparison is true because the values are same, but the second is false because one is an integer and the other is a string.
  • 0 == "abc" was true in PHP 7, but PHP 8 changed it to false.
  • null == false is true, because both are false-like. With === it is false.

Use === in your conditions. The opposite operators are != and !==, see != vs !==. All operators are in PHP Operators.

What are the arithmetic operators in PHP?

Arithmetic operators are used to do maths on numbers. PHP has these arithmetic operators:

OperatorNameExampleResult
+Addition10 + 313
–Subtraction10 – 37
*Multiplication10 * 330
/Division10 / 33.3333333333333
%Modulus (remainder)10 % 31
**Exponentiation (power)10 ** 31000
<?php
$a = 10;
$b = 3;

echo ($a + $b) . "<br>";
echo ($a - $b) . "<br>";
echo ($a * $b) . "<br>";
echo ($a / $b) . "<br>";
echo ($a % $b) . "<br>";
echo ($a ** $b);
?>

Output:

Tutorials Class - Output Window

13
7
30
3.3333333333333
1
1000

Division always gives a float when the result is not a whole number. Use intdiv() if you need only the whole part. Read all operators in PHP Operators.

What is the use of the ternary operator in PHP?

The ternary operator is a short way to write a simple if-else statement in one line. The syntax is condition ? value_if_true : value_if_false.

<?php
$age = 20;

$result = ($age >= 18) ? "Adult" : "Minor";

echo $result;
?>

Output:

Tutorials Class - Output Window

Adult

There is also a shorter form ?: (called Elvis operator). It returns the first value if it is not empty, otherwise the second one.

<?php
$username = "";

echo $username ?: "Guest";
?>

Output:

Tutorials Class - Output Window

Guest

Use it only for short and simple conditions. For long conditions a normal if-else is easier to read. More details in PHP Ternary Operator.

What is the difference between division (/) and modulus (%) in PHP?

The division operator (/) gives the result of dividing one number by another. The modulus operator (%) gives only the remainder of that division.

<?php
$a = 10;
$b = 3;

echo $a / $b . "<br>";        // division
echo $a % $b . "<br>";        // modulus (remainder)
echo intdiv($a, $b);          // whole number part only
?>

Output:

Tutorials Class - Output Window

3.3333333333333
1
3

Modulus is often used to check even or odd numbers. If $number % 2 is 0, the number is even.

Note: In PHP 8, dividing or using modulus with zero throws a DivisionByZeroError. Check the divisor before the calculation.

What is the difference between != and !== in PHP?

!= (not equal) checks only the value. !== (not identical) checks the value and the data type. So !== is more strict.

<?php
$a = 5;       // integer
$b = "5";     // string

var_dump($a != $b);    // values are same, so false
var_dump($a !== $b);   // data types are different, so true
?>

Output:

Tutorials Class - Output Window

bool(false)
bool(true)

In PHP, <> works same as !=. The same logic applies to the opposite operators, see difference between == and ===.

What is the difference between $var++ and $var + 1 in PHP?

$var + 1 only calculates a new value and does not change $var. $var++ is the increment operator, it changes the value of $var by adding 1.

There is one more thing to remember. $var++ (post-increment) returns the old value first and then increases. ++$var (pre-increment) increases first and then returns the new value.

<?php
$a = 5;
$b = $a + 1;      // $a is not changed
echo $a . " " . $b . "<br>";

$x = 5;
$y = $x++;        // $y gets old value 5, then $x becomes 6
echo $x . " " . $y . "<br>";

$m = 5;
$n = ++$m;        // $m becomes 6 first, then $n gets 6
echo $m . " " . $n;
?>

Output:

Tutorials Class - Output Window

5 6
6 5
6 6

Which special characters are allowed in PHP variable names?

Only the underscore (_) is allowed as a special character in a PHP variable name. Characters like -, @, # and a space are not allowed.

Here are the full rules:

  • A variable name starts with the dollar sign $.
  • After the dollar sign, the first character must be a letter or an underscore, not a digit.
  • The rest of the name can have letters, digits and underscores.
  • Variable names are case-sensitive. $name and $Name are two different variables.
Variable nameValid?
$nameYes
$_nameYes
$user_name2Yes
$2nameNo, starts with a digit
$first-nameNo, hyphen is not allowed
$first nameNo, space is not allowed

PHP also accepts some non-English letters, but it is better to use only a to z in names. Read more in PHP Variables.

Can we print multiple variables in a single echo statement in PHP?

Yes. You can print many variables in one echo statement. There are three common ways: separate them with a comma, join them with the dot (.) operator, or put them inside double quotes.

<?php
$name = "Robin";
$city = "Delhi";

echo $name, " lives in ", $city, "<br>";      // comma
echo $name . " lives in " . $city . "<br>";   // dot operator
echo "$name lives in $city";                  // double quotes
?>

Output:

Tutorials Class - Output Window

Robin lives in Delhi
Robin lives in Delhi
Robin lives in Delhi

Variables are replaced by their values only inside double quotes. Single quotes print the text as it is.

How to check the data type of a variable in PHP?

Use var_dump() to see the type and value, gettype() to get the type name, or the is_ functions (like is_int(), is_string(), is_array()) to test a type.

<?php
$a = 343;
$b = "Hello";
$c = 3.14;
$d = [1, 2];

var_dump($a);
echo gettype($b) . "<br>";
var_dump(is_float($c));
echo gettype($d);
?>

Output:

Tutorials Class - Output Window

int(343)
string
bool(true)
array

Note: For a float, gettype() returns the word “double” (an old name). From PHP 8 you can use get_debug_type(), which returns “float”.

In PHP you do not write the data type when creating a variable, PHP decides it from the value. Learn more in PHP Data Types.

Can a single variable store both digits and characters in PHP?

Yes. If you put digits and characters together inside quotes, PHP stores them as a string. PHP is a loosely typed language, so you do not need to declare the type.

<?php
$variable = "Contact:24/7";

echo $variable . "<br>";
echo gettype($variable) . "<br>";

// The same variable can store another type later
$variable = 24;
echo gettype($variable);
?>

Output:

Tutorials Class - Output Window

Contact:24/7
string
integer

Remember that the value in quotes, like "24", is a string and not a number, even if it has only digits. Read PHP Data Types for more.

What is NULL in PHP?

NULL is a special data type in PHP that has only one value, NULL. It means “no value”. NULL is not same as 0, empty string or false.

A variable is NULL in three cases: when you assign NULL to it, when it was never created, or when you remove it with unset().

<?php
$x = "Hello PHP!";
$x = null;

var_dump($x);
var_dump(is_null($x));
var_dump(isset($x));
?>

Output:

Tutorials Class - Output Window

NULL
bool(true)
bool(false)

To check for NULL use is_null() or === null. The isset() function returns false for a NULL variable.

What is the difference between echo and print in PHP?

echo and print both are used to show output in PHP. They are language constructs, so brackets are optional. The main differences are in the table below.

echoprint
Returns nothingAlways returns 1
Can take many values separated by commaTakes only one value
Can not be used inside an expressionCan be used inside an expression
<?php
echo "Hello", " ", "World", "<br>";   // many parameters

$r = print "Hi";                      // print returns 1
echo "<br>" . $r;
?>

Output:

Tutorials Class - Output Window

Hello World
Hi
1

People say echo is a little faster than print, but the difference is so small that you will never notice it. Use echo in most cases.

What is the constant() function in PHP?

The constant() function returns the value of a constant when you have the name of the constant as a string. It is useful when the constant name is stored in a variable or created at run time.

<?php
define("SITE_NAME", "Tutorials Class");

class App {
    const VERSION = "2.0";
}

echo constant("SITE_NAME") . "<br>";

$name = "SITE_NAME";
echo constant($name) . "<br>";

echo constant("App::VERSION");
?>

Output:

Tutorials Class - Output Window

Tutorials Class
Tutorials Class
2.0

If the constant does not exist, PHP 8 throws an Error. Check it first with defined(). Read PHP Constants for the basics.