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.
Try this exercise. Click the correct answer from the options.
Echo is a __ in PHP
Click the option that best answers the question.
- Function
- Variable
- Language Construct
#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): void |
---|
The 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
https://www.php.net/manual/en/function.echo.php
xxxxxxxxxx
<?php
//Prints Welcome to Algodaily!!
echo “Welcome to AlgoDaily!!”;
?>
Build your intuition. Click the correct answer from the options.
The print statement returns _.
Click the option that best answers the question.
- 1
- true
- null
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): int |
---|
The print statement returns 1 and takes only one argument. The parenthesis is optional in the syntax.
Source
https://www.php.net/manual/en/function.print.php
xxxxxxxxxx
<?php
//Prints Welcome to Algodaily!!
print("Welcome to Algodaily!!");
?>
Try this exercise. Click the correct answer from the options.
In PHP, the gettype() function returns the data type of a variable. Given this information, what will be the output of this code?
1<?php
2
3echo gettype("Welcome to AlgoDaily!!");
4
5echo gettype(["Welcome","To","AlgoDaily"]);
6
7echo gettype(1+2);
8
9?>
Click the option that best answers the question.
- Boolean Integer Array
- String Array Integer
- String Object Double
#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 |
xxxxxxxxxx
<?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);
?>
Build your intuition. Click the correct answer from the options.
1,2,3,4,5 is an example of?
Click the option that best answers the question.
- Associative Array
- Multi-dimensional Array
- Indexed Array
#3 - PHP Arrays
There are three types of arrays in PHP a. Indexed array b. Associative array c. Multi-dimensional array
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.
xxxxxxxxxx
<?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
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.
xxxxxxxxxx
<?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
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.
xxxxxxxxxx
<?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]);
?>
Are you sure you're getting this? Is this statement true or false?
PHP foreach loop iterates over arrays and objects only.
Press true if you believe the statement is correct, or false otherwise.
#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
xxxxxxxxxx
<?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.
*/
?>
Build your intuition. Click the correct answer from the options.
What will be the output of this code?
1<?php
2
3$greetings_string = "Hello World";
4
5$greetings_explode = explode(" ",$greetings_string);
6
7echo gettype($greetings_explode);
8
9?>
Click the option that best answers the question.
- string
- boolean
- array
#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): array |
---|
Here the $separator is a specific character that determines the split points in the string.
Source
https://www.php.net/manual/en/function.explode.ph
xxxxxxxxxx
<?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)
?>
Are you sure you're getting this? Click the correct answer from the options.
“123” and 123 are?
Click the option that best answers the question.
- Same values & Same data types
- Same values & Different data types
- Different values & Same data types
#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.
xxxxxxxxxx
<?php
$a = 123;
$b = "123";
//Output: false
echo $a === $b;
//Output: true
echo $a == $b;
?>
Let's test your knowledge. Is this statement true or false?
Class is a template for objects. Objects are instances of a class.
Press true if you believe the statement is correct, or false otherwise.
#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.
xxxxxxxxxx
<?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
An object is an instance of a class. Objects of the same class have similar attributes and methods. Objects are instantiated with new keyword.
xxxxxxxxxx
<?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
PHP
is anopen-source scripting language
widely used for scripting the backend of a website, integrating well with HTML, CSS, and Javascript.- The
echo
language 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
print
statement is a function that returns an integer value of1
when used to output strings and variables, unlike the language constructecho
which does not return a value. - The print statement
returns 1
and takes oneargument
which can beprinted
to theconsole
, but isslower
thanecho
. - 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-dimensional
arrays. - An
indexed array
is a collection of values that are accessed by a numericindex
starting at zero, which is a concept familiar to many programmers. - An associative array is a
data structure
in whichkey-value pairs
are stored and can be accessed using the keys. - A
multi-dimensional array
has arrays within arrays and is just like a matrix with more than one row, with either indexed or associative arrays. - PHP's
foreach
loop is an easy and syntactically simple way to iterate over arrays and objects. PHP foreach loop
is a specialized construct used to efficiently loop over associative arrays and access key-value pairs.$greetings_explode
is being set to the output of theexplode()
function, which splits astring
into an array, andgettype()
can be used to return the data type of a variable, so the output of the code will be "array".- The
explode
function in PHP splits a string into an array using a specific character as theseparator
. - The same value can have different data types, such as
string
andinteger
, so the answer is "Same value, different data types". - The
equal
operator (==) checks for equality in value, while theidentical
operator (===) also checks for equality indata type
. - Classes are
instantiated
wheninvoked
to create an instance of the class, which then has the same properties and methods as defined by the class. - A PHP class is a
template
orblueprint
for creatingobjects
. - An object is created with the
new
keyword and is an instance of a class, with the same attributes and methods as other objects of the same class.