Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Variable Types

A Variable in PHP is just like with any other coding. It is used to store information to use in code without the need of repeating it over and over again or as something dynamic or user-input interceptive.

Topics to be covered

We will cover the following topics in this lesson

  • PHP Variable Example

  • Characteristics of Variables in PHP

  • Types of Variables

    • Strings
    • Integers
    • Booleans
    • Double
    • Null
    • Arrays
    • Resources
    • Objects
  • Summary

PHP Variable Example

For example:

<?php

$subject = "Economics";

$marks = 81;

echo "You got $marks in $subject";

?>

In the above example both the $subject and $marks are variables with information assigned to them each.

Characteristics of Variables in PHP

A PHP variable may have distinct variable characteristics which are as follows:

  • All Variables are written with the prefix of the Dollar sign $
  • Expression (or information) are assigned to a Variable after an equal sign or = operator. The Variable will always be written on left side and expression on the right (i.e. $variable = expression;)
  • A Variable is always written in same way whether it is used to save string expression or numerical values (i.e. $variable = abc; $variable = 123;)
  • PHP Variables are pretty much Perl-like

Types of Variables

PHP Variables can store the information (or data) in about 8 ways which can be classified as types of Variables or data types that can be assigned to PHP Variable.

Strings

Strings are basically alphabets or alpha-number combo. It may include symbols and special characters may be passed but they will always be written inside quotes. Such as

<?php

$someString1 = testing;

$someString1 = 'testing';

$someString2 = "testing & 123";

echo $someString1,$someString2;

?>

HERE,

  • $someString1 = 'testing'; assigns a string value to our variable
  • $someString4 = "testing & 123";

The following examples are not valid because they skip the quotes when assigning the values.

<?php

$someString2 = testing & 123;

echo $someString1,$someString2;

?>

Integers

Integers are whole numbers, such as numerical value without any decimal point. (i.e. $num = 1984)

<?php
    
$number = 1984
    
echo $number;

?>

HERE,

  • $number = 1984 assigns the integer value 1984 to the variable $number. Notice for numbers we don't have to use either single or double quotes when assigning values.

Booleans

Booleans are values assigned to a variable in the state of either true or false. Where true represents logical 1 and false means 0 (nothingness) Such as:

<?php

$city1 = "New York";

$city2 = Paris;

$city3 = London;

if ($city1 == TRUE) {
	echo "Value for Variable city1 is $city1<br>";
}

if ($city2 == FALSE) {
	echo "Variable name city2 doesn't exist";
}

if ($city3 == FALSE) {
	echo "Variable name city doesn't exist";
}

if ($city4 == FALSE) {
	echo "Variable name city4 doesn't exist";
}

?>

The output of above code will be

Value for Variable city1 is New York

Variable name city4 doesn't exist

As you can see city1 variable does exist and it does have a value assigned to it but city4 doesn't exist therefor city1 is TRUE Boolean and city4 is a false Boolean.

Double

Doubles are type of integers that are written as floating-point numbers. For instance 0.1 or 3.141592

<?php
$number1 = 0.1;
$number2 = 3.141592;
    
echo $number1, ' | ', $number2;
?>

The above code outputs the following

0.1 | 3.141592

 

Null

As the name suggest Null means nothing or non-existence, for Boolean it will be interpreted as FALSE therefor 0 (zero) which is also denoting to "nothingness". So if a variable's expression is assigned to null then Boolean will take it as FALSE and echoing the variable won't return any output in the console or browser.

Arrays

This is one of the wide range of values that are indexed of serialized in organized way for displaying organized/serialized or indexed values. For example

<?php

$browser = array('Chrome','Firefox','Opera');

echo implode("<p>",$browser);

echo "<p>3rd browser in the array is <b><font color=blue>".$browser[2]."</font>";

?>

The above code produces the following results

PHP Arrays

As you can see that although Opera is stored in 3rd position but array reads it as [2] that is because array always counts from 0 and array[0] is 1st storage position and array[1] will be 2nd and so on.

The above code can also alternatively be written as follows

<?php

$browser = [
    'Chrome',
    'Firefox',
    'Opera'
];

echo implode("<p>",$browser);

echo "<p>3rd browser in the array is <b><font color=blue>".$browser[2]."</font>";

?>

HERE,

  • $browser = [...] square brackets are used to define arrays without using the array keyword.

Resources

Resources as the name suggest are liable of holding out of PHP resources (for example making database connection inside a PHP code, etc.). Below is an example of how Resources Variable are used in PHP code

<?php

$connection_link = mysql_connect("dbserver", "root", "S3cUr3-Password");

As can be observed in the code snippet above the $connection_link variable is used as Resources data type as it attempts to make connection to some database server, once connection is established then we can directly execute SQL queries from within the PHP script to the server which was connected using the $connection_link variable.

Objects

Objects are basically instances in PHP code where the programmer defines a Class by himself. It can include all other kind of values as well as function that are only specific to the Class. See below example

<?php

class Browser {
    function Browser() {
        $this->Bname = "Google Chrome";
    }
}

$Chrome = new Browser();
$CNAME = $Chrome->Bname;

class BrowserVersion {
    function BrowserVersion() {
    	$this->Bver = " 64.0.3";
    }
}

$ChromeVer = new BrowserVersion();
$CVER = $ChromeVer->Bver;

echo $CNAME,$CVER;

?>

In this example code two classes were created by programmer by the name of Browser and BrowserVersion and in the end echoed it altogether to give an impression of one string i.e. Google Chrome browser and its corresponding version.

The above code produces the following results

Google Chrome 64.0.3

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Syntax Overview

PHP - Constants

Leave A Reply