Hands-on with PHP — Data Types

< Home < PHP Syntax

Classification of Data

All computer programs have to deal with data. Each piece of data is classified into some data type that best reflects the meaning that it represents and defines the way that it is to be used. A digit of 8, for example, should naturally be classified as a number type if it represents an age of a person, and is required to be used for statistics purpose. On the other hand, that digit may be classified as a character if it is to represent a street number. Ultimately, the answer to what data type a data should be lies in the user requirements.

In PHP, data can be classified into one of the following data types:

PHP has a var_dump() function that outputs the type and value of data and a newline. This has come in handy in introducing the various data types.

String

A string, in its simple form,is sequence of characters enclosed insides a pair of double or single quotes, e.g. "Hello World!".

Check out the example in the String code section using the PhpFiddle widget.

<?php
echo "<pre>";
var_dump("Hello World!"); // string(12) "Hello World!"
echo "\"This is a string inside a pair of double quotes.\"\n";
echo '\'This is a string inside a pair of single quotes.\'';
echo "</pre>";
?>

Return to Classification of Data

Boolean

A Boolean represents the truth value of a comparison operation. A truth value is either TRUE or FALSE, and both are not case sensitive. On display, a Boolean TRUE value is converted to the string value 1, whereas a Boolean FALSE is converted to an empty string.

Check out the example in the Boolean code section using the PhpFiddle widget.

<?php
echo "<pre>";
echo "The values of truth:\n";
echo "The value of TRUE is ", TRUE; // The value of TRUE is 1
echo "\n";
echo "The value of FALSE is ", FALSE;  // The value of FALSE is
/*
The value of truth:
The value of TRUE is 1
The value of FALSE is 
*/
echo "\n\n";
echo "Truth values are not case sensitive:\n";
echo "TRUE is ", var_dump(TRUE); // bool(true)
echo "true is ", var_dump(true); // bool(true)
echo "FALSE is ", var_dump(FALSE); // bool(false)
echo "false is ", var_dump(false); // bool(false)
/*
Truth values are not case sensitive:
TRUE is bool(true)
true is bool(true)
FALSE is bool(false)
false is bool(false)
*/
echo "\n";
echo "The outcome of comparison:\n";
echo "If 1 equal 1? ", var_dump(1==1); // If 1 equal 1? bool(true)
echo "If 'a' equal 'A'? ", var_dump('a'=='A'); // If 'a' equal 'A'? bool(false)
/*
The outcome of comparison:
If 1 equal 1? bool(true)
If 'a' equal 'A'? bool(false)
*/
echo "</pre>";
?>

Return to Classification of Data

Integer

An integer is a whole number of the set Z &#8712; {…, -2, -1, 0, 1, 2, …}.

Check out the example in the Integer code section using the PhpFiddle widget.

<?php
echo "<pre>";
var_dump(123); // int(123)
var_dump(1 + 2 + 3); // int(6)
echo "</pre>";
?>

Return to Classification of Data

Float

Also known as double or floating point number, a float is a real number with a decimal point or in exponential form.

Check out the example in the Float code section using the PhpFiddle widget.

<?php
echo "<pre>";
var_dump(1.23); // float(1.23)
var_dump(1.23e2); // float(123)
var_dump(3E-10); // float(3.0E-10)
echo "</pre>";
?>

Return to Classification of Data

Object

An object is an instance of a class. A class in software engineering is a software model that mimics the general characteristics and behaviours of a real world entity that you seek to represent and manipulate in a software application. You may liken a software class to a building blueprint and the actual buildings built based on that blueprint as the objects of that software class.

Check out the example in the Object code section using the PhpFiddle widget.

<?php
echo "<pre>";
// declare a class
class Student {
  public $name;
  function Student($new_name) {
  $this->name = $new_name;
  }
}
       
// create an object
$new_student = new Student("Peter Leow");

// print structure information of an object
var_dump($new_student);
/*
object(Student)#1 (1) {
  ["name"]=>
  string(10) "Peter Leow"
}
*/
echo "</pre>";
?>

Return to Classification of Data

Array

An array is a collection of values or objects. For example, a year can be considered an array type that holds the twelve months from January to December. Likewise, an academic class can be considered an array type that consists of all the students in that class.

Check out the example in the Array code section using the PhpFiddle widget.

<?php
echo "<pre>";
var_dump(array("Apple","Orange","Pear","Banana"));
/*
array(4) {
  [0]=>
  string(5) "Apple"
  [1]=>
  string(6) "Orange"
  [2]=>
  string(4) "Pear"
  [3]=>
  string(6) "Banana"
}
*/
echo "</pre>";
?>

Return to Classification of Data

Null

NULL is a special data type that has only one value &mdash; the case-insensitive NULL. It represents the absence of data.

Check out the example in the Null code section using the PhpFiddle widget.

<?php
echo "<pre>";
var_dump($undefined); // NULL
echo "</pre>";
?>

Return to Classification of Data

Resource

A resource is not a data type by itself, but a handle that stores a reference to an external resource of a particular type, which may be a database connection, a database query result, an external file, an XML document, a JSON object, etc.

Check out the example in the Resource code section using the PhpFiddle widget.

<?php
echo "<pre>";
// Open a file resource
$file = fopen("https://peterleowblog.com/wp-content/uploads/2017/10/textfile.txt", "r");
var_dump($file); // resource(5) of type (stream)
echo "</pre>";
?>

Return to Classification of Data

Quiz

How do you determine the correct data types for those data involved in a software application? Sometimes the obvious is not always so obvious! Some of the considerations include the context of the data and the types of usage required or not required of the data in accordance with user requirements. Check these questions out and ask yourself why you choose a particular answer.

What data type should currencies be classified?
Float
Correct!
String
Wrong!
Integer
Wrong!
Boolean
Wrong!
What data type should ZIP codes be classified?
String
Correct!
Float
Wrong!
Integer
Wrong!
Boolean
Wrong!
What data type should the size of a population be classified?
Integer
Correct!
Float
Wrong!
String
Wrong!
Boolean
Wrong!
What data type should the state of a light switch be classified?
Boolean
Correct!
Float
Wrong!
String
Wrong!
Integer
Wrong!

< Home < PHP Syntax

Tagged with:

Speak Your Mind