Mark As Completed Discussion

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.

Introduction

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

PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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?

TEXT/X-PHP
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

PHP Data Types Introduction

There are eight fundamental data types in PHP. Here’s an overview of these eight types.

Data TypesDescriptionExample
StringA sequence of characters inside quotes“Hello World”
BooleanA boolean represent two states: true/falsetrue false
NULLShows absence of a value. An unassigned variable has the value null$a; // variable a is null
ArrayAn array stores multiple values in a single variable[1,2,3,4,5,6,7,8,9,10]
IntegerAn integer represents a non-decimal number100 -2 2000 1
DoubleA double represents decimal or numbers in exponential form3.14159 22.6 -3.1
ObjectAn instance of a class that inherits its attributes and methods$person = new Person()
ResourceA resource data type is a reference to an external resource like a file or databaseDatabase call
PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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

PHP Arrays Introduction

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.

PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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 Introduction

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
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Build your intuition. Click the correct answer from the options.

What will be the output of this code?

TEXT/X-PHP
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

PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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.

PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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.

PHP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

One Pager Cheat Sheet

  • PHP is an open-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 example echo "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 of 1 when used to output strings and variables, unlike the language construct echo which does not return a value.
  • The print statement returns 1 and takes one argument which can be printed to the console, but is slower than echo.
  • 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, and Resource.
  • 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, and Multi-dimensional arrays.
  • An indexed array is a collection of values that are accessed by a numeric index starting at zero, which is a concept familiar to many programmers.
  • An associative array is a data structure in which key-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 the explode() function, which splits a string into an array, and gettype() 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 the separator.
  • The same value can have different data types, such as string and integer, so the answer is "Same value, different data types".
  • The equal operator (==) checks for equality in value, while the identical operator (===) also checks for equality in data type.
  • Classes are instantiated when invoked 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 or blueprint for creating objects.
  • 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.