Getting Started with PHP – Part 5 (PHP DataTypes)

Guest Posts Paolo Nikko Nuñal

In our last tutorial we discussed about PHP variables, in this tutorial we are going to learn data types and how you can manipulate them in your code.

There are seven data-types in PHP namely:


  • String

  • Integer

  • Floating point numbers

  • Boolean

  • Array

  • Object

  • NULL


Strings

PHP strings are a sequence of characters and are placed inside single or double quotes.

<?php
   $txt = "Hello World";
   $txt2 = 'The quick brown fox jumps over the lazy dog";

   echo $txt."<br>";
   echo $txt2;
?>


Integers

In PHP, an integer is a whole number or in other words a number that doesn't have any decimals.

<?php
   $num1 = 23;
   $num2 = 24;
?>


Floating Point Numbers

In PHP, a floating point is a number with decimals.

<?php
   $num1 = 2.34;
   $num2 = 8E-5;
?>


Booleans

In PHP, a boolean can either be TRUE or FALSE

<?php
   $stmt = true;
   $stmt2 = false;
?>


Arrays

Arrays can store multiple values in  a single variable. You can check out our other pages that deals more about ARRAYS and functions.

<?php
   $array = array ("mango","carrot","onions","tomatoes");
   echo var_dump($array);
?>


Objects

PHP variables can hold objects. But to declare an object you need to first imply what type of object you are creating.

<?php
  $person = new Person();
?>


Null Value

In PHP you can specify variables as null. Null is the only possible value of the data type null. Values can be emptied and set with a null.

<?php
  $x= 23;
  $x = null;
?>