Become Master Yoda in PHP (Part-4)

Hello, guys welcome back to becoming Master Yoda in PHP, in the last session we discussed the syntax, different predefined data types, and loops used in PHP. In today’s lesson, we will learn about the array, constant, and superglobals in PHP. So, let’s get started-

Array in PHP-

An array is a type of data structure that holds a series of the homogenous data. The arrays in PHP are declared as- $array_example = array(“helix”, “smart”, “labs”);


$array_example = array(“Helix”, “Smart”, “Labs”);
echo “I like ” . $array_example[0] . ”  ” . $array_example[1] . ”  ” . $array_example[2] . “.”;

Array in PHP

Output of Array

From the above example you can see, the way to declare the arrays-

  • The array() the function is used to create an array.
  • The strings in an array are declared in double-quotes/ single quotes and the integer, float-point numbers don’t need any quotes.
  • The first element of an array can be called by array_example[0], each element in an array can be declared by their index number that is, array_example[0] will carry the first element of the given array. 

Now there are three types of array in PHP:

Single Dimensional array/ Indexed Array-

This is the simplest way to declare an array, like the one declared in the above example. But there is one more way to call the elements in an array, that is, through the loops:

$array_example = array(“Helix”, “Smart”, “Labs”);
echo “I like “;
for($i = 0; $i < count($array_example); $i++) {
               echo $array_example[$i];
             echo ” “;

Single dimensional array

Output of Single Dimensional Array

Multidimensional array-

The multidimensional array is also known as an array of arrays. The multidimensional arrays can be an array with two, three, four, or more levels, that is, an array with multiple dimensions. For example- $multidimensional_array= array( array(1,2),  array(3,4),  array(5,6));

Multidimensional array can be implemented in two ways as well-

  1. By calling their Indexes
$array_example = array( array(1,2),  array(3,4),  array(5,6));
               echo $array_example[0][1];
             echo ” “;

Calling by Index

Output of above code

2. By using the loops

$array_example = array(array(1,2),  array(3,4),  array(5,6));
for($i = 0; $i < 3; $i++) {
               for($j=0; $j<2;$j++){
                             echo $array_example[$i][$j];
                      echo ” “;

Array using loops

Output of array using loops

Associative array-

Associative array will have their index as string so that you can establish a strong association between key and values.

  1. Calling through the index
$hsl = array(“Helix”=>”001″,”Smart”=>”005”, “Labs”=>”007”);
echo “Hello ” . $hsl[‘Helix’] . ” * “.$hsl[‘Smart’].” * “.$hsl[‘Labs’];

Associative array

Output of Associative array

2. This can be done through the foreach loop:

$hsl = array(“Helix”=>”001”, “Smart”=>”005”, “Labs”=>”007”);
foreach($hsl as $value => $new_value) {
  echo “Key=” . $value . “, Value=” . $new_value;
  echo “\n”;

Associative array

Output of associative array through loop

That’s all about the array, now let’s talk about Constants.

Constant in PHP:

A constant’s value cannot be changed during the whole code, whereas a valid constant name starts with a letter or underscore and there is no need for $ sign before a constant.

To create a constant, use the define() function. The syntax will be as: define(name, value, case-insensitive) where

  • name: The name of the constant
  • value: The value of the constant
  • case-insensitive: States whether the constant name should be case-insensitive or not, where the default is false.


define(“Hello”, “Welcome to Helix Smart Labs”, true);
echo Hello;

Constant in PHP

Output of Constant in PHP

SuperGlobals in PHP:

There are predefined variables in PHP are called “superglobals”, 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.

Some examples are:

  • $GLOBALS – $GLOBALS is a variable that is used to access global variables from anywhere in the PHP script.
  • $_SERVER – $_SERVER is a variable that holds information about headers, paths, and script locations.
  • $_REQUEST – PHP $_REQUEST is an associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.
  • $_GET – PHP $_GET is an associative array of variables passed to the current script via the URL parameters.
  • $_POST – PHP $_POST is an associative array of variables passed to the current script via the HTTP POST method.
  • $_COOKIE – PHP $_COOKIE is an associative array of variables passed to the current script via HTTP Cookies.
  • $_SESSION – PHP $_SESSION is an associative array containing session variables available to the current script.

That’s all for this tutorial see you next time. Happy Learning!!

Leave a Comment

Your email address will not be published. Required fields are marked *