Getting Started with PHP Data Types


Understanding PHP Data Types

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:

1. Integer

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>";
?>

Output:

The value of the integer is: 25

2. String

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>";
?>

Output:

The value of the string is: Hello, AIT!

3. Float

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>";
?>

Output:

The value of the float is: 19.99

4. Boolean

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>";
?>

Output:

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.