PHP Data types

This video was created by Dani Krossing

Like most programming languages, PHP will work with many different types of data, known as data types. PHP data types includes the following:

  • Boolean
  • Integer
  • Float
  • String
  • Array
  • Object
  • NULL
  • resource

For now, we will just focus on the first four (Boolean, Integer, Float, String), which are known as Scalar types.

Booleans

Booleans are the simpliest type and expresses a truth value, either true or false.

  $bool = true;

Integers

An integer is any whole number, either positive or negative. To set a variable to a number, provide the number literal with no quotes.

<?php
  // setting variables to numbers
  $var1 = 3;
  $var2 = 4;
?>

Floats

Floating point numbers or floats for short, are any number with a decimal in them. To set a variable to a floating point number, simply provide a number literal that contains a decimal.

<?php 
  // setting a variable to a float
  $float = 3.14;
?>

Strings

A string is a series of characters, surrounded by quotes or special syntax. A string can be created using single quotes or double quotes.

Strings can be used with echo statements to display the strings in the browser.

<?php 
  // echo a string with double quotes
  echo "Hello World <br>";
  
  // echo a string with single quotes
  echo 'Hello World <br>';