PHP Interview - Basic Level
PHP stands as one of the top languages because of its ability to have a large impact on the outcome with very little code. This amount of efficiency has been a requirement for the past few years in the industry.
On seeing this, companies are investing a good amount of money in hiring proficient PHP developers to fit these shoes and work effectively.
Today, this article will walk you through the most commonly asked PHP interview questions for freshers and experienced in the industry.
1) What is PHP?
PHP stands for Hypertext Preprocessor. It is an open-source server-side scripting language that is widely used for web development. It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc. It was created by Rasmus Lerdorf in 1994.
2) What are PEAR and PECL in PHP?
PEAR stands for "PHP Extension and Application Repository". It has libraries and code written in PHP. Those you can simply download, install and include in your code. It also provides a command line interface to install "packages" automatically.
PECL stands for "PHP Extension Community Library". It is a repository for PHP Extensions, providing a directory of all known extensions and hosting facilities for downloading and developing PHP extensions.
PEAR packages are written in PHP and are plain-text scripts, while PECL extensions are written in C/C++ and are compiled binaries in their consumable form.
3) What is the common usage of PHP?
- PHP can generate the dynamic page content.
- PHP can create, open, read, write, delete, and close files on the server.
- PHP can collect form data.
- PHP can send and receive cookies.
- PHP can add, delete, and modify data in your database.
- PHP can be used to control user access.
- PHP can encrypt data.
4) What is the difference between PHP 4 and PHP 5?
- PHP4 doesn't support the oops concept and uses Zend Engine 1.
- PHP5 supports the oops concept and uses Zend Engine 2.
5) What is the difference b/w static and dynamic websites?
A static website is a basic website with fixed content while dynamic websites are advanced websites that provide different content according to client requests.
In Static Web Pages, the database is not used, So it takes less time for loading than dynamic web pages.
In dynamic web pages, the database is used, So it takes more time for loading.
6) What are the popular Content Management Systems (CMS) in PHP
- WordPress
- Joomla
- Magento
- Drupal
7) What are the popular frameworks in PHP?
- Laravel
- CodeIgniter
- CakePHP
- Yii 2
- Symfony
8) What is the difference between "echo" and "print" in PHP?
- echo has no return value while print has a return value of 1.
<?php$res=print 'Hello World<br>';print $res; //OUTPUT : 1$res=echo 'Hello World<br>'; //syntax error, unexpected token "echo"?>
- echo can take multiple parameters while print can take one argument.
<?phpecho "Hello", "I am", "World" ; //validprint "Hello", "I am", "World" ; //invalid?>
- echo is marginally faster than print.
9) How do you execute a PHP script from the command line?
Open your CMD and run the below command.
php filename.phpEx: php test.php
10) How to define variables in PHP.
In PHP, You can define a variable using the $ sign.
php
$variable_name
EX:
$x=10;
$y="Hello World"
?>
11) What is the difference between $ and $$?
$ is used to declare a variable in PHP while $$ is used as a reference variable that stores the value of a normal variable.
Example 1:
<?php$$x="Hello";?>
OUTPUT
PHP Warning: Undefined variable $x
Example 2:
<?php$x="Hello";echo $$x; //$($x)=$(Hello)=$Hello?>
OUTPUT
PHP Warning: Undefined variable $Hello
Example 3:
<?php$x="Hello";$Hello="World";echo $$x; //$($x)=$(Hello)=$Hello?>
OUTPUT
World
Example 4:
<?php$x="Hello";$$x="World"; //$($x)=$(Hello)=$Helloecho $x."\n"; //Helloecho $$x."\n"; //Worldecho $Hello."\n"; //World?>
OUTPUT
Hello
World
World
Example 5:
<?php$Hello="Vaidikalaya";$msg='Hello';echo $msg.' '.$$msg;?>
OUTPUT
Hello Vaidikalaya
12) How many ways to define the scope of variables in PHP?
PHP has three different variable scopes
- Local
- Global
- Static
| Global | Local | Static |
|---|---|---|
| A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. | A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function. | A variable is declared with a static keyword and does not lose its value when the function exits and will still hold that value should the function be called again. |
|
|
|
13) how many data types are used in PHP?
PHP supports the following data types:
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
- Resource
14) How many string methods are available in PHP?
PHP supports a number of string methods like:
- strlen(): return the length of the string.
- strrev(): reverses a string.
- str_replace(): replaces some characters with some other characters in a string.
- trim(): Removes whitespace or other characters from both sides of a string.
- ucfirst(): Converts the first character of a string to uppercase.
- lcfirst(): Converts the first character of a string to lowercase.
- ucwords(): Converts the first character of each word in a string to uppercase.
- strtolower(): Converts a string to lowercase letters.
- strtoupper(): Converts a string to uppercase letters.
15) what are constants and how do create them in PHP?
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
Note: Unlike variables, constants are automatically global across the entire script.
PHP constants can be defined by 2 ways
- using define()
- using const
Define()
Syntex:
define(name, value, case-insensitive)
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether a constant is case-insensitive. The default value is false. It means it is case-sensitive by default.
Example 1:
<?phpdefine("MESSAGE","Hello World");echo MESSAGE; //Hello Worldecho message; //Undefined constant "message"?>
Example 2:
Using const<?phpdefine("MESSAGE","Hello World",true);echo MESSAGE; //Hello Worldecho message; //Hello World?>
The const keyword defines constants at compile time. It is a language construct, not a function. The constant defined using the const keyword are case-sensitive.
Syntex:
const constant_name=value;
Example:
<?phpconst MESSAGE="Hello World";echo MESSAGE; //Hello World?>
16. What is the loop in PHP?
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
- for: for loop can be used to traverse a set of codes for the specified number of times
- foreach: loops through a block of code for each element in an array
- while: loops through a block of code as long as the specified condition is true
- do while: loops through a block of code once, and then repeats the loop as long as the specified condition is true
17) What is an array in PHP?
An array is a special variable, which can hold more than one value at a time.We can store numbers, strings and objects in the PHP array. In PHP, the array() function or [] is used to create an array.
There are 3 types of array in PHP.
- Indexed Array:
$arry=array("summer","winter","spring","autumn");
OR
$arry=["summer","winter","spring","autumn"]
- Associative Array:
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
OR
$salary=["Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000"];
- Multidimensional Array:
array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
18) How many array methods are available in PHP?
PHP provides various array functions to access and manipulate the elements of array. The important PHP array functions are given below.
- count(): return counts of all elements in an array.
- sort(): sorts all the elements in an array.
- array_search(): search an array for a value and returns the key.
- in_array(): searches an array for a specific value and returns 1 if the value match.
- list(): The list() function is used to assign values to a list of variables in one operation.
19) What are superglobals in PHP?
Superglobal Variables in PHP are predefined global variablesGlobal variables are variables with global scope,which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
20) What are the magic methods and magic constants in PHP?
Magic Methods:
Magic methods in PHP are special methods that are aimed to perform certain tasks. These methods are started with a double underscore (__) as a prefix. And It is automatically called when certain criteria are met.
The following magical methods are currently available in PHP.
- __construct()
- __destruct()
- __call($name,$parameter)
- __toString()
- __get($name)
- __set($name , $value)
- __debugInfo()
- __sleep()
- __wakeup()
- __serialize()
- __unserialize()
- __isset()
- __unset()
Magic C onstants:
Magic constants are the predefined constants in PHP that get changed on the basis of their use. They start with double underscore (__) and end with a double underscore(__).
There are nine magic constants in PHP. In which eight magic constants start and end with double underscores (__).
- __LINE__
- __FILE__
- __DIR__
- __FUNCTION__
- __CLASS__
- __TRAIT__
- __METHOD__
- __NAMESPACE__
- ClassName::class