PHP Interview Questions Cheat Sheet
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Introduction
Introduction
PHP is an open-source scripting language created back in 1994. PHP stands for Hypertext Preprocessor. It is widely used for scripting the backend of a website. PHP is executed on the server-side and is one of the oldest server-side scripting languages for building dynamic web pages.
PHP integrates well with HTML, CSS, and Javascript, and that’s why it is embedded with these front-end technologies. Although much has been changed in the web development domain and Javascript has dominated. However, PHP is still famous for building blog sites in Wordpress and Joomla or Ecommerce sites in Magento.
Here’s a cheat sheet for some fundamental concepts in PHP. These fundamental questions are usually asked in an interview.
Echo and Print Task1
Echo is a __________ in PHP
Echo Introduction
#1 - Echo and Print
Echo
PHP echo is used to output strings and variables to the console. It is a language construct, not a function. Echo prints strings, escaping characters, multi-line strings, and variables. Here’s the syntax of echo.
echo(string ...$expressions): voidThe echo statement opens with or without parenthesis, depending on the number of arguments. It doesn’t return a value and is faster than the print statement.
Source
```php
<?php
//Prints Welcome to Algodaily!!
echo “Welcome to AlgoDaily!!”;
?>
```
Print Task1
The print statement returns _____.
Print Introduction
Print is also a language construct and is used to print expressions to the console. It is slower than echo and has a return type. Here’s the syntax
print(string $expression): intThe print statement returns 1 and takes only one argument. The parenthesis is optional in the syntax.
Source
```php
<?php
//Prints Welcome to Algodaily!!
print("Welcome to Algodaily!!");
?>
```
PHP Data Types Task1
In PHP, the gettype() function returns the data type of a variable. Given this information, what will be the output of this code?
<?php
echo gettype("Welcome to AlgoDaily!!");
echo gettype(["Welcome","To","AlgoDaily"]);
echo gettype(1+2);
?>
PHP Data Types Introduction
#2 - PHP Data Types
There are eight fundamental data types in PHP. Here’s an overview of these eight types.
Data Types Description Example String A sequence of characters inside quotes “Hello World” Boolean A boolean represent two states: true/false true false NULL Shows absence of a value. An unassigned variable has the value null $a; // variable a is null Array An array stores multiple values in a single variable [1,2,3,4,5,6,7,8,9,10] Integer An integer represents a non-decimal number 100 -2 2000 1 Double A double represents decimal or numbers in exponential form 3.14159 22.6 -3.1 Object An instance of a class that inherits its attributes and methods $person = new Person() Resource A resource data type is a reference to an external resource like a file or database Database call```php
<?php
//Output: string
echo gettype("Hello World");
//Output: boolean
echo(gettype(true));
//Output: array
echo gettype(["Welcome","To","AlgoDaily"]);
//Output: integer
echo gettype(100);
//Output: double
echo gettype(3.14149);
//Output: NULL
echo gettype(null);
?>
```
PHP Arrays Task1
[1,2,3,4,5] is an example of?
PHP Arrays Introduction
Indexed Array Introduction
Indexed Array
The indexed array is an array with values only. The values are accessed through a numeric index. An index starts from zero. Programmers coming from other programming languages are familiar with indexed arrays.
```php
<?php
//An example of indexed array
$indexed_array = [1,2,3,4,5,6,7,8,9,10];
//Output : 1
echo $indexed_array[0];
//Output : 2
echo $indexed_array[1];
//Output : 3
echo $indexed_array[2];
//Output : 10
echo $indexed_array[-1];
?>
```
Associative Array Introduction
Associative Array
An associative array is an incredible data structure that looks like dictionaries in Python and Javascript or maps in Java. It stores key-value pairs rather than just values. Their keys can access the values.
```php
<?php
//An associative array with employee names as keys and their ages as values
$employee_age = array(
"Rachel" => 22,
"Edward" => 23,
"Simon" => 24,
"Anna" => 25,
"Mike" => 26
);
//Output : 22
echo $employee_age["Rachel"];
//Output : 24
echo $employee_age["Simon"];
?>
```
Multi-dimensional Array Introduction
Multi-dimensional Array
A multi-dimensional array has arrays within arrays. It is just like a matrix with more than one row. A multi-dimensional array could have indexed or associative arrays.
```php
<?php
//A multi-dimensional array.
$multidimensional_arr = [[0,1],[2,3],[4,5]];
//Output: [0,1]
print_r($multidimensional_arr[0]);
//Output: [2,3]
print_r($multidimensional_arr[1]);
//Output: [4,5]
print_r($multidimensional_arr[2]);
?>
```
PHP foreach loop task1
PHP foreach loop iterates over arrays and objects only.
PHP foreach loop Introduction
#4 - PHP foreach loop
PHP foreach loop is an exclusive construct in PHP that iterates over arrays and objects only. It is highly efficient for looping over associative arrays. That’s because it reserves two pointers for accessing the key-value pairs as it loops over the associative array. Here’s the syntax.
foreach(\$associative_arr as \$key=>\$value)The $key and $value get the associative array’s key and values as the loop iterates.
Source
https://www.php.net/manual/en/control-structures.foreach.php
```php
<?php
//An array with employee names as keys and their ages as values
$employee_age = array(
"Rachel" => 22,
"Edward" => 23,
"Simon" => 24,
"Anna" => 25,
"Mike" => 26
);
//Name is the key and age is the value
foreach($employee_age as $name=>$age)
{
echo $name." is ".$age." years old.".PHP_EOL;
}
/*
Rachel is 22 years old.
Edward is 23 years old.
Simon is 24 years old.
Anna is 25 years old.
Mike is 26 years old.
*/
?>
```
PHP Explode Task1
What will be the output of this code?
<?php
$greetings_string = "Hello World";
$greetings_explode = explode(" ",$greetings_string);
echo gettype($greetings_explode);
?>
PHP Explode Introduction
#5 - PHP Explode Function
PHP explode function splits a string into an array. The split is based on a specific character. Here’s the syntax of this function.
explode(string $separator, string $string, int $limit = PHP_INT_MAX): arrayHere the $separator is a specific character that determines the split points in the string.
Source
```php
<?php
$fruits = "Apple,Mango,Strawberry,Orange,Pineapple";
$fruits_array = explode(",",$fruits);
/*
Output
Array
(
[0] => Apple
[1] => Mango
[2] => Strawberry
[3] => Orange
[4] => Pineapple
)
*/
print_r($fruits_array)
?>
```
Equal and Identical Operator Task1
“123” and 123 are?
Equal and Identical Operator Introduction
#6 - Equal and Identical Operators
Equal operator == returns true if the variables being compared have the same values. The identical operator === returns true if the variable has the same values and same data types.
```php
<?php
$a = 123;
$b = "123";
//Output: false
echo $a === $b;
//Output: true
echo $a == $b;
?>
```
PHP Class and Objects Task1
Class is a template for objects. Objects are instances of a class.
Class Introduction
#7 - PHP Class and Objects
Class
A class is a template or a blueprint for objects. It is analogous to a map of a house, while a house is an object based on the outlines of the map. PHP uses the class keyword to create a class.
```php
<?php
class Car{
public $color;
public $wheels;
public $model;
function __construct($color,$wheels,$model){
$this ->color = $color;
$this ->wheels = $wheels;
$this ->model = $model;
}
function get_color(){
return $this->color;
}
}
?>
```
Object Introduction
Object
An object is an instance of a class. Objects of the same class have similar attributes and methods. Objects are instantiated with new keyword.
```php
<?php
class Car{
public $color;
public $doors;
public $model;
function __construct($color,$doors,$model){
$this ->color = $color;
$this ->doors = $doors;
$this ->model = $model;
}
function get_color(){
return $this->color;
}
}
$red_car = new Car("Red",4,"2021");
//Output: Red
echo $red_car->get_color();
?>
```
One Pager Cheat Sheet
One Pager Cheat Sheet
-
PHPis anopen-source scripting languagewidely used for scripting the backend of a website, integrating well with HTML, CSS, and Javascript. - The
echolanguage construct in PHP can be used to output string(s) without surrounding parentheses, and can take multiple parameters, as demonstrated by the exampleecho "Hello" . " World!";. PHP echo is a language construct used to output strings and variables to the console.- The
printstatement is a function that returns an integer value of1when used to output strings and variables, unlike the language constructechowhich does not return a value. - The print statement
returns 1and takes oneargumentwhich can beprintedto theconsole, but isslowerthanecho. - The
gettype()function in PHP is used to determine the data type of the input expression, with the example expressions being a String, an Array and an Integer respectively. - The 8 fundamental data types in PHP are
String,Boolean,NULL,Array,Integer,Double,Object, andResource. - An Indexed Array in PHP is a type of array that stores multiple values in a single variable, where each element has an integer index or
key. - PHP supports
Indexed,Associative, andMulti-dimensionalarrays. - An
indexed arrayis a collection of values that are accessed by a numericindexstarting at zero, which is a concept familiar to many programmers. - An associative array is a
data structurein whichkey-value pairsare stored and can be accessed using the keys. - A
multi-dimensional arrayhas arrays within arrays and is just like a matrix with more than one row, with either indexed or associative arrays. - PHP's
foreachloop is an easy and syntactically simple way to iterate over arrays and objects. -
PHP foreach loopis a specialized construct used to efficiently loop over associative arrays and access key-value pairs. -
$greetings_explodeis being set to the output of theexplode()function, which splits astringinto an array, andgettype()can be used to return the data type of a variable, so the output of the code will be "array". - The
explodefunction in PHP splits a string into an array using a specific character as theseparator. - The same value can have different data types, such as
stringandinteger, so the answer is "Same value, different data types". - The
equaloperator (==) checks for equality in value, while theidenticaloperator (===) also checks for equality indata type. - Classes are
instantiatedwheninvokedto create an instance of the class, which then has the same properties and methods as defined by the class. - A PHP class is a
templateorblueprintfor creatingobjects. - An object is created with the
newkeyword and is an instance of a class, with the same attributes and methods as other objects of the same class.



