PHP Data Types

In PHP, variables are used to store and manipulate data. Variables can store data of different types, such as integers, floats, strings, and so on. Different data types can be used in different ways and have different properties.

PHP has 8 primitive data types:

  1. Integers – Whole numbers, without a decimal point, like 4195.
  2. Floats – Numbers with a decimal point, like 3.14159.
  3. Strings – A sequence of characters, like “Hello World”.
  4. Booleans – True or false values.
  5. Arrays – An ordered map of values, allowing you to access values using keys.
  6. Objects – An instance of a class, which can store properties and functions.
  7. Resources – A special type that represents a handle to an external resource, such as a database connection.
  8. NULL – A special type representing a null value, meaning the absence of a value.

PHP also supports two pseudo-types:

  1. mixed – Can be any type.
  2. void – Used only in function return types to specify that a function does not return a value.

PHP String

In PHP, a string is a sequence of characters, like “Hello World”. Strings can be surrounded by either single or double quotes, and you can use one type of quotes to define a string that contains the other type of quotes. For example:

$string1 = 'Hello World';
$string2 = "Hello World";
$string3 = 'I said "Hello World"';
$string4 = "I said 'Hello World'";

There are several ways to manipulate strings in PHP:

  1. Concatenation – Use the “.” operator to concatenate two strings, like this:
$string1 = 'Hello';
$string2 = 'World';
$string3 = $string1 . ' ' . $string2; // $string3 is now "Hello World"

2. Escaping characters – Use a backslash () to escape characters that have a special meaning in PHP. For example:

$string1 = 'I\'m a string'; // $string1 is now "I'm a string"
$string2 = "This is a new line\n"; // $string2 is now "This is a new line" followed by a new line

3. String functions – PHP has many built-in functions for manipulating strings, such as strlen(), strtoupper(), and substr(). For example:

PHP Integer

An integer in PHP is a whole number, without a decimal point. Integers can be positive or negative, and can be stored in a variety of sizes, depending on the size of the number and the system architecture.

In PHP, you can use the following functions to check if a value is an integer:

  • is_int() – Returns true if the value is an integer, false otherwise.
  • is_integer() – An alias of is_int().

You can also use the following functions to convert a value to an integer:

  • intval() – Converts a value to an integer.
  • (int) – Typecasts a value to an integer.

For example:

$num1 = 123;
$num2 = '123';
$num3 = '123.45';

var_dump(is_int($num1)); // Outputs: bool(true)
var_dump(is_integer($num1)); // Outputs: bool(true)
var_dump(intval($num2)); // Outputs: int(123)
var_dump((int)$num3); // Outputs: int(123)

PHP Float

A float (short for floating point number) in PHP is a number with a decimal point, like 3.14159. Floats are used to represent numbers that are not necessarily whole numbers.

In PHP, you can use the following functions to check if a value is a float:

  • is_float() – Returns true if the value is a float, false otherwise.
  • is_double() – An alias of is_float().

You can also use the following functions to convert a value to a float:

  • floatval() – Converts a value to a float.
  • (float) – Typecasts a value to a float.

For example:

$num1 = 123.45;
$num2 = '123.45';
$num3 = '123';

var_dump(is_float($num1)); // Outputs: bool(true)
var_dump(is_double($num1)); // Outputs: bool(true)
var_dump(floatval($num2)); // Outputs: float(123.45)
var_dump((float)$num3); // Outputs: float(123)

PHP Boolean

A boolean in PHP represents a true or false value. Booleans are often used to check if a condition is true or false, and to control the flow of a program.

In PHP, you can use the following functions to check if a value is a boolean:

  • is_bool() – Returns true if the value is a boolean, false otherwise.

You can also use the following functions to convert a value to a boolean:

  • boolval() – Converts a value to a boolean.
  • (bool) – Typecasts a value to a boolean.

For example:

$bool1 = true;
$bool2 = 'true';
$bool3 = 'false';

var_dump(is_bool($bool1)); // Outputs: bool(true)
var_dump(boolval($bool2)); // Outputs: bool(true)
var_dump((bool)$bool3); // Outputs: bool(false)

PHP Array

An array in PHP is an ordered map of values, allowing you to access values using keys. Arrays are useful for storing and manipulating data that has a logical order, or that needs to be associated with a specific key.

There are two types of arrays in PHP:

  1. Indexed arrays – Arrays with a numeric index. The index starts at 0, and you can access elements of the array using the index number.
  2. Associative arrays – Arrays with string keys. You can access elements of the array using the string keys.

Here’s an example of an indexed array:

$colors = array('red', 'green', 'blue');

echo $colors[0]; // Outputs: red
echo $colors[1]; // Outputs: green
echo $colors[2]; // Outputs: blue

Here’s an example of an associative array:

$user = array('name' => 'John', 'age' => 30, 'email' => 'john@example.com');

echo $user['name']; // Outputs: John
echo $user['age']; // Outputs: 30
echo $user['email']; // Outputs: john@example.com

PHP Object

An object in PHP is an instance of a class, which is a blueprint for creating objects. Objects have properties, which are variables associated with an object, and methods, which are functions that can be called on an object.

Here’s an example of a class and an object in PHP:

class Car {
  public $color;
  public $make;
  public $model;

  public function __construct($color, $make, $model) {
    $this->color = $color;
    $this->make = $make;
    $this->model = $model;
  }

  public function honk() {
    return 'Beep Beep!';
  }
}

$car = new Car('red', 'Toyota', 'Corolla');
echo $car->color; // Outputs: red
echo $car->make; // Outputs: Toyota
echo $car->model; // Outputs: Corolla
echo $car->honk(); // Outputs: Beep Beep!

In the example above, the Car class has three properties (color, make, and model) and one method (honk). The $car object is an instance of the Car class, and it has its own values for the color, make, and model properties. You can access these properties using the arrow (->) operator, and you can call the honk method using the same operator.

PHP NULL Value

The NULL value in PHP represents the absence of a value or a null pointer. It is a special data type that only has one value: NULL.

You can use the following functions to check if a value is NULL:

  • is_null() – Returns true if the value is NULL, false otherwise.
  • empty() – Returns true if the value is NULL, an empty string, or 0, false otherwise.

You can also use the following functions to set a value to NULL:

  • unset() – Sets a value to NULL.
  • null – A language construct that can be used to assign a value to NULL.

For example:

$var = null;

var_dump(is_null($var)); // Outputs: bool(true)
var_dump(empty($var)); // Outputs: bool(true)

unset($var);
var_dump(is_null($var)); // Outputs: bool(true)

$var = null;
var_dump(is_null($var)); // Outputs: bool(true)