PHP Data Types

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types


PHP Data Types: Scalar Types

Scalar types are the simplest data types, representing a single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string


PHP Data Types: Compound Types

Compound types are data types that can hold multiple values or complex data structures. There are 2 compound data types in PHP.

  1. array
  2. object


PHP Data Types: Special Types

Special types that serve unique purposes, beyond storing scalar or compound values. There are 2 special data types in PHP.

  1. resource
  2. NULL


Boolean


 A boolean (or bool) is a data type that represents two possible values: true or false. Booleans are often used in conditional statements and control structures to determine whether a condition is met or not.


Example
<!DOCTYPE html>
<html>
<body>

<?php
$isTrue = true;
$isFalse = false;

if ($isTrue) {
echo gettype($isTrue)."<br>";
echo "This is true!";
} else {
echo gettype($isFalse)."<br>";
echo "This is false!";
}

?>

</body>
</html>

Try it yourself

Note

  • The is_bool() function checks whether a variable is a boolean or not.
  • This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

Integer


Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.


Rules for integer:

  • An integer can be either positive or negative.
  • An integer must not contain decimal point.
  • Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.
Example
<!DOCTYPE html>
<html>
<body>

<?php
$decimal = 42; // Decimal
$hex = 0x1A; // Hexadecimal (26 in decimal)
$octal = 0755; // Octal (493 in decimal)
$binary = 0b1010; // Binary (10 in decimal)
echo $decimal."<br>";
echo $hex."<br>";
echo $octal."<br>";
echo $binary."<br>";

?>

</body>
</html>

Try it yourself

Float


A float (also known as a double or real number) is a data type that represents numbers with decimal points or numbers in scientific notation. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Example
<!DOCTYPE html>
<html>
<body>

<?php
$price = 19.99; // A float representing a price
$discount = 0.15; // A float representing a 15% discount
$totalPrice = $price - ($price * $discount);
echo $totalPrice; // Outputs: 16.99 (the total price after discount)

?>

</body>
</html>

Try it yourself

String


A string is a sequence of characters, used to store and manipulate text. Strings in PHP can include letters, numbers, symbols, or any combination of these.


String values must be enclosed either within single quotes or in double quotes. But both are treated differently.

Example
<!DOCTYPE html>
<html>
<body>

<?php

$name = "world";

$string1 = 'Hello, world!'; // Single-quoted string
$string2 = "Hello, $name!"; // Double-quoted string (variable parsing)
$string3 = 'Hello, $name!'; // Single-quoted string (variable parsing as string)
$string4 = 'Hello, '.$name; // Single-quoted string (variable parsing)

echo $string1."<br>";
echo $string2."<br>";
echo $string3."<br>";
echo $string4."<br>";

?>

</body>
</html>

Try it yourself

Array


An array is a special variable that can hold multiple values at once. Arrays are one of the most powerful data types in PHP because they allow you to store and manipulate collections of values, whether they are numbers, strings, or even other arrays.

Example
<!DOCTYPE html>
<html>
<body>

<?php

$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana


?>

</body>
</html>

Try it yourself

Objects


Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Example
<!DOCTYPE html>
<html>
<body>

<?php

class bike {
function model() {
$model_name = "Royal Enfield";
echo "Bike Model: " .$model_name;
}
}
$obj = new bike();
$obj -> model();


?>

</body>
</html>

Try it yourself

Resources


Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.


This is an advanced topic of PHP, so we will discuss it later in detail with examples.


NULL


NULL is a special data type that represents a variable with no value. It is used to indicate that a variable has been intentionally left empty or that a function or operation has no value to return. NULL is different from other data types like strings, integers, or arrays, and it is a unique value in PHP.

Example
<!DOCTYPE html>
<html>
<body>

<?php

$var = NULL; // Explicitly setting the variable to NULL
echo var_dump($var); // Outputs: NULL

?>

</body>
</html>

Try it yourself

Note

If a variable is created without a value, it is automatically assigned a value of NULL.

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.