Get DevWP - WordPress Development Theme

PHP Data Types

PHP supports 10 Primitive Data Types which can be used with Variables to store data. You can store simple strings to numeric values and more complex types of data with the data types mentioned below.

Four Scalar Data Types:

  • Boolean – either TRUE or FALSE
  • Integer – is a non-decimal number between -2,147,483,648 and 2,147,483,647
  • Float – are numbers that can have a decimal point or a number as an exponent
  • String – Regular text in Alphanumeric form like Joel or Joel123

Four Compound Data Types:

  • Array – can store multiple values in a single variable
  • Object – stores data and information on how that data can be processed
  • Callable / Callbacks – Callback functions can be simple functions, or they can be object methods, including static class methods.
  • Iterable – is a pseudo-type introduced in PHP 7.1. It accepts any array or object implementing the Traversable interface. Both Data types are iterable using foreach and can be used with yield from within a generator.

And Two Special Data Types:

  • Resource – holds a reference to an external resource that can be created and used by special functions.
  • NULL – this is a special data type that means void of value or no value at all.

PHP Integer Example

As mentioned earlier, Integers can be either a positive or negative number and don’t have any decimals in them.


<?php
$x = 100; // decimal number
var_dump($x);
echo "<br>";

$y = -100; // a negative number
var_dump($y);
echo "<br>";
?>

PHP String Example

Strings are text or data displayed in an alphanumeric form. Yes numbers can be in a string format but aren’t the same as integers or floats.


<?php
  $var = "PHP Powers almost 80% of the Modern Web.";
  echo $var;
?>

PHP Float Example

Floats are numbers with decimals.


<?php
$z = 50.35;
var_dump($z);
?> 

PHP Boolean Example

Boolean’s are either True or False and often used with conditional code.


<?php
// Assign the value FALSE to a variable
$notTrue = FALSE;
var_dump($notTrue);

echo '<br>';

// Assign the value TRUE to a variable
$isTrue = TRUE;
var_dump($isTrue);
?>

PHP Array Data Type Explained

PHP Arrays are extremely useful when you want to store multiple values in one variable. You can have Indexed Arrays or Associative Arrays. Arrays can even store other arrays in them. Below is a basic example of a PHP Array.


<?php
$os = array("Linux", "Windows", "MacOS");
var_dump($os);
?>

PHP Object Data Type Explained

As mentioned above, PHP Objects are data types that store data and information on how to process that data. Objects can have Properties and Methods which are like Variables and Functions and are created off of a blueprint which is a Class. Checkout the example Below.


<?php
// Class definition
class welcome{
    // public property
    public $txt = "Hello";

    // methods
    function display_txt(){
        return $this->txt;
    }
}

// Create object from class
$msg = new welcome;
var_dump($msg);
?>

PHP Resource

This is used to store a reference to a function or external resource. You can use it to open files or another example would be to do a database call.


PHP NULL Explained

As mentioned above, the NULL data type in PHP is how you represent a variable that’s empty or void of a value.


<?php
$x = "Hi";
$x = null;
var_dump($x);
?>

Those are basic examples of Data Types in PHP. In upcoming tutorials, I’ll go deeper on each topic with multiple examples.



View Our Themes