PHP Data Types

PHP is Loosely Typed

You don't need to declare a type when creating a variable—PHP figures it out automatically:

$x = 5;           // Integer
$y = "Hello";     // String
$z = 3.14;        // Float

String

Text enclosed in quotes:

$text = "PHP is fun";

Integer

Whole numbers:

$num = 100;

Float (Double)

Decimal numbers:

$pi = 3.14159;

Boolean

Either true or false:

$isLoggedIn = true;

Array

Stores multiple values:

$fruits = array("apple", "banana", "cherry");

Object

Instance of a class:

class Car {
  public $brand;
}
$myCar = new Car();
$myCar->brand = "Toyota";

NULL

Represents a variable with no value:

$data = null;

Resource

Special type used for file and database connections. Example:

$handle = fopen("test.txt", "r");

var_dump()

Use var_dump() to display type and value:

$x = "PHP";
var_dump($x);

Type Checking Functions

  • is_string()
  • is_int()
  • is_float()
  • is_bool()
  • is_array()
  • is_object()
if (is_array($fruits)) {
  echo "This is an array.";
}

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic