PHP Variables

In this simple tutorial we will explain to you what variables are, the type of data you can store inside them and alot of extra information you need to understand to start using variables.

As for every beginner in any language in particular PHP, the first question that comes to your mind is;

what is a variable?

A variable is a name of memory location that holds data.In a simple term variables are used to store data, like string of text, numbers among other data formats.

In PHP, a variable starts with the $ sign, followed by the name of the variable.Don't worry you are about to see that below.

In PHP there different data types which we use to construct our variables.

  • Integers
  • Doubles
  • NULL
  • Strings
  • Arrays
  • Booleans
Data Types Explained

Integers - These are whole numbers or without a decimal point eg 12344567890.

In the example below,we are going to create a variable called $Birth_year and later calculate the age of the user.


          //All the years and age are whole numbers
          $Birth_year = 1996;
          $current_year = 2018;

          $age = $current_year - $Birth_year;
          echo "$age years old.";

          //Possible output
          22 years old.

Doubles - These are floating-point or numbers with a decimal point eg 10.5.

In this example below, we have created variables namely $Php, $Javascript, $Python and assigned 90.6,85.2 & 79.4 (floating point numbers) as their values respectively.


          //oston's marks
          $Php = 90.6;
          $Javascript = 85.2;
          $Python = 79.4;

NULL - This is a special type that only has one value.

To give a variable the NULL value, simply write it as shown below −


          $salary = NULL;

Strings - These are sequences of characters or text eg. 'Oston code cypher is the best place to learn.'

In the example below, lets create a variable called $social_media and assign youtube as it's value. Remember you can store any thing so long as it is supposed to be a string


        //Note - strings are wrapped inside double 
          $social_media = "youtube";

         // or single quotes
           $social_media = 'youtube';

Arrays - These are indexed collections of values.

In the example below we are going to create two variables namely; $subjects, $marks and store data inside them respectively.


          //A variable storing an array with subjects
         $subjects = array('math','biology','chemistry');

         //A variable storing an array with marks
         $marks = array(50,79,90);


Booleans - The possible values here are either true or false.

In the example below, we are checking if the user is logged in or not.Remember u can use these booleans values to test for anything.


          $logged_in = true; //it can also be false

            echo "You are logged in";
            echo "Please log in";


Alright guys as you have seen in the tutorial, the main way to store information in the middle of a PHP program is by using variables.

Search it,Get it