PHP supports several data types, which are used to define variables and the type of data they hold. Here’s an overview of some commonly used data types:
An integer
is a whole number (positive or negative) without decimals. Here’s an example:
<?php
$number = 25;
echo "The value of the integer is: " . $number . "<br>";
?>
The value of the integer is: 25
A string
is a sequence of characters, enclosed within single or double quotes. Here’s an example:
<?php
$text = "Hello, AIT!";
echo "The value of the string is: " . $text . "<br>";
?>
The value of the string is: Hello, AIT!
A float
is a number with a decimal point. Here’s an example:
<?php
$price = 19.99;
echo "The value of the float is: " . $price . "<br>";
?>
The value of the float is: 19.99
A boolean
represents a truth value, either true
or false
. Here’s an example:
<?php
$is_logged_in = true;
echo "The boolean value is: " . ($is_logged_in ? 'true' : 'false') . "<br>";
?>
The boolean value is: true
Understanding these data types is crucial for effective programming in PHP. Each type has its unique characteristics and use cases.