PHP Data Types
PHP supports several data types, including:
- Integers: Whole numbers, either positive, negative, or zero.
- Floats: Decimal numbers, also known as floating-point numbers.
- Strings: Sequences of characters, such as words or sentences.
- Booleans: True or false values.
- Arrays: Collections of values of the same type.
- Objects: Instances of classes, which represent complex data structures.
- NULL: A special value that represents the absence of any value.
Example of PHP Integers
<?php
$integer1 = 10;
$integer2 = -5;
$integer3 = 0;
echo gettype($integer1) . "\n";
// Outputs: integer
echo gettype($integer2) . "\n";
// Outputs: integer
echo gettype($integer3) . "\n";
// Outputs: integer
?>
Output:
integer
integer
integer
Explanation: This example demonstrates the use of integers in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP Floats
<?php
$float1 = 10.5;
$float2 = -3.14;
$float3 = 0.0;
echo gettype($float1) . "\n";
// Outputs: double
echo gettype($float2) . "\n";
// Outputs: double
echo gettype($float3) . "\n";
// Outputs: double
?>
Output:
double
double
double
Explanation: This example demonstrates the use of floats in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP Strings
<?php
$string1 = 'Hello, World!';
$string2 = "Hello, World!";
$string3 = 'This is a single-quoted string.';
$string4 = "This is a double-quoted string.";
echo gettype($string1) . "\n";
// Outputs: string
echo gettype($string2) . "\n";
// Outputs: string
echo gettype($string3) . "\n";
// Outputs: string
echo gettype($string4) . "\n";
// Outputs: string
?>
Output:
string
string
string
string
Explanation: This example demonstrates the use of strings in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP Booleans
<?php
$boolean1 = true;
$boolean2 = false;
echo gettype($boolean1) . "\n";
// Outputs: boolean
echo gettype($boolean2) . "\n";
// Outputs: boolean
?>
Output:
boolean
boolean
Explanation: This example demonstrates the use of booleans in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP Arrays
<?php
$array1 = array(1, 2, 3);
$array2 = array('apple', 'banana', 'orange');
$array3 = array('name' => 'John', 'age' => 30);
echo gettype($array1) . "\n";
// Outputs: array
echo gettype($array2) . "\n";
// Outputs: array
echo gettype($array3) . "\n";
// Outputs: array
?>
Output:
array
array
array
Explanation: This example demonstrates the use of arrays in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP Objects
<?php
$object1 = new stdClass();
$object1->name = 'John';
$object1->age = 30;
echo gettype($object1) . "\n";
// Outputs: object
?>
Output:
object
Explanation: This example demonstrates the use of objects in PHP. The gettype()
function is used to determine the data type of each variable.
Example of PHP NULL
<?php
$nullValue = NULL;
echo gettype($nullValue) . "\n";
// Outputs: NULL
?>
Output:
NULL
Explanation: This example demonstrates the use of NULL in PHP. The gettype()
function is used to determine the data type of the variable, which outputs NULL.