PHP Primitive Data Types

PHP has 10 primitive data types including 4 scalar, 4 compound and 2 special types.

Primitive Data is a Building Block for Creating Data Structures

PHP Scalar Data Types

I am scalar and therefore I am one single value. Just like a destitute bachelor, I fly solo, no other values shall be stored.

Boolean
Is it true or fale? 0 or 1? Is the user logged in (true then do something, false do something else).

$logged_user = false;

Integer
Any non-decimal number, negative or positive. The total items in the shopping cart is 12.

$total = sum($cart=>items);

Float
Floating point numbers, doubles or real numbers.
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;

String
A series of characters.
$random_string = "asdfaohfg adsf q2048t strings #$!FW!#%FS"

PHP Compound Data Types

I will take data, and I will grant it structure, as I am Compound, the controller of structure and aggregates.

Array
Method of storing indexed values. More commonly used as an ordered map/associative array with key and values. Partners nicely with JSON.

$array_simple = array("David Jones", "35 years old", "Lives in Nantucket, New Jersey");
$array_typical = array("Name"=>"David Jones", "Age"=>35, "City"=>"Nantucket", "State"=>"New Jersey");
 

Object
We have a class blueprint to create a bus, we can use inheritance to create bus objects painted with different colors.

class Bus{
    function paintBus($color){
        echo 'The color of the bus is ' . $color . '.<br>';
    }
}
$red_bus = new Bus();
$school_bus = new Bus():
$red_bus->paintBus('red');
$school_bus->paintBus('yellow');

The color of the bus is red.
The color of the bus is yellow.

Callback
Yes, I answer to string. Just call me by name.
class CallMe{
  function callMeMethod(){
    echo 'you called me, I am a method as I reside in a class';
  }
}
function callme_function(){
  echo "you called me, I am function";
}
call_user_func('callme_function');
call_user_func(array('CallMe', 'callMeMethod'));

Iterable
Pseudo type introduced in PHP 7.1. Accepts arrays or objects.
function iterate(iterable $iterable = []) {
  foreach ($iterable as $value) {
    echo $value . '<br>';
  }
}
iterate(['value10 ', 'value20 ']);

PHP Special Data Types

External resources and the NULL value.

Resource
Reference to an external resource, for example, connecting to a database.

$conn = mysqli_connect(localhost,"root","admin","sales");

NULL
Representation of a variable with no value. Is case sensitive.

$variable = NULL;