Course Content
About Lesson

What are data types in PHP?

Data types define the type of data a variable can hold.

  • String
  • Integer
  • Float (Double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

A string is a sequence of characters, written in quotes. Example:

A string is a sequence of characters, enclosed in single or double quotes.
Example:

PHP
<?php
  $text = "Hello, PHP!";
  echo $text;
?>

An integer is a whole number, positive or negative, without decimals.
Example:

PHP
<?php
  $x = 100;
  var_dump($x); // Output: int(100)
?>

A float (or double) is a number with decimal points.
Example:

PHP
<?php
  $price = 99.99;
  var_dump($price); // Output: float(99.99)
?>

A boolean represents two possible values: true or false.
Example:

PHP
<?php
  $is_logged_in = true;
  var_dump($is_logged_in); // Output: bool(true)
?>

An array holds multiple values in one variable.
Example:

PHP
<?php
  $colors = array("Red", "Green", "Blue");
  var_dump($colors);
?>

NULL is a special data type that represents a variable with no value.
Example:

PHP
<?php
  $value = null;
  var_dump($value); // Output: NULL
?>

A constant is an identifier for a value that cannot be changed after it’s set. Defined using define().
Syntax:

PHP
define("CONSTANT_NAME", value);

Example:

PHP
<?php
  define("SITE_NAME", "MyWebsite");
  echo SITE_NAME; // Output: MyWebsite
?>
FeatureVariableConstant
SyntaxStarts with $Uses define()
ChangeableYes, can change anytimeNo, fixed after defining
ScopeLocal or globalGlobal

You can make constants case-insensitive (deprecated in PHP 8.0+) using a third parameter in define():

PHP
<?php
  define("GREETING", "Hello!", true);
  echo greeting; // Output: Hello!
?>