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 typesopen in new window 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

Booleansopen in new window are the simpliest type and expresses a truth value, either true or false.

  $bool = true;

Integers

An integeropen in new window 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 numbersopen in new window 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 stringopen in new window is a series of characters, surrounded by quotes or special syntax. A string can be created using single quotesopen in new window or double quotesopen in new window.

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>';