Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views7 pages

Data Types

Uploaded by

masharmaa9129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Data Types

Uploaded by

masharmaa9129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PHP DATA TYPES

In programming a data type refers to the type of value


a variable has and what type of mathematical,
relational or logical operations can be applied on it
without causing an error.

PHP Integers : integers are used to represent whole


numbers, including both positive and negative values,
without any fractional or decimal part.

<?php
$age = 25;
echo $age; // Output: 25
?>
Floats
In PHP, foats are versatile data types that can
represent numbers with fractional or decimal parts,
including both positive and negative values

<?php
$price = 99.99;
echo $price; // Output: 99.99
?>

String
Strings store sequences of characters, including letters
and numbers. Use double quotes for fexibility and
single quotes for literal values.

 Double-Quoted

$double_quoted_string = "Hello, World!";


 Single-Quoted

$single_quoted_string = 'PHP allows single-


quoted strings.';

Note: Double-quoted strings allow variable parsing;


single-quoted strings don’t.
Double quoted
<?php
$name = "Alice";
echo "Hello, $name!"; // PHP sees "$name" and
replaces it with "Alice"
?>
Single quoted
<?php
$name = "Alice";
echo 'Hello, $name!'; // PHP sees "$name" as just text,
not a variable
?>
Boolean
Booleans in PHP are fundamental for conditional
testing, holding either TRUE (equivalent to 1) or FALSE
(equivalent to 0). Successful events typically return
TRUE, while unsuccessful events return FALSE.
Additionally:
<?php
$isActive = true;
if ($isActive) {
echo "Active";
}
?>

Array
Arrays, a compound data type in PHP, efciently store
multiple values of the same data type.
They are like containers for holding multiple pieces of
information. Here's an example with integers
Syntax:- $integer_array = [1, 2, 3, 4, 5]

<?php
$colors = ["red", "green", "blue"];
echo $colors[1]; // Output: green
?>

Objects
An object is an instance of a class and is used to access
methods and properties. Objects are explicitly declared
and created from the new keyword.
<?php
class Car {
public $brand = "Toyota";
}
$car = new Car();
echo $car->brand; // Output: Toyota
?>
Resources
Resources in PHP are used to store references, often to
external functions or resources, such as database
connections. They are not an exact data type but serve
as handles for specifc operations like fle handling or
database connections.

<?php
$handle = fopen("fle.txt", "r");
var_dump($handle);
?>

NULL
NULL in PHP is a special variable type that can only
hold one value: NULL. It is case-sensitive, typically
written in capital letters. When a variable is created
without a value, or explicitly set to NULL, it
automatically takes on this special value.

<?php
$x = NULL;
var_dump($x); // Output: NULL
?>
COMMON EXAMPLES FOR ALL DATA TYPES

<?php
$integer_variable = 42;
$foat_variable = 3.14;
$string_variable = "Hello, PHP!";
$boolean_variable = true;
$array_variable = [1, 2, 3];
$object_variable = new stdClass();
$null_variable = null;

echo "Integer: $integer_variable <br>";


echo "Float: $foat_variable<br>";
echo "String: $string_variable<br>";
echo "Boolean: " . ($boolean_variable ? "true" :
"false") . "<br>";
echo "Array: " . print_r($array_variable, true) . "<br>";
echo "Object: " . print_r($object_variable, true) .
"<br>";
echo "NULL: " . var_export($null_variable, true) .
"<br>";
?>

You might also like