PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
String: A string is a sequence of characters, like "Hello world!". You can declare a string in PHP like so:
$x = "Hello world!"; $y = 'Hello world!';
Integer: An integer is a non-decimal number between -2,147,483,648 and 2,147,483,647. An integer data type is a non-decimal number between -2147483648 and 2147483647. A valid integer must be without decimal points, and it can be either positive or negative.
$x = 5985;
Float (floating point numbers - also called double): A float is a number with a decimal point or a number in exponential form.
$x = 10.365;
Boolean: A Boolean represents two possible states: TRUE
or FALSE
.
$x = true; $y = false;
Array: An array stores multiple values in one single variable.
$cars = array("Volvo","BMW","Toyota");
Object: Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model;
NULL: The special NULL
value represents a variable with no value. NULL
is the only possible value of type NULL
.
$x = NULL;
Resource: The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
Remember that PHP is a loosely typed language and does not require explicit declaration of the data type of the variable. Depending on the context, PHP automatically converts the variable to the correct data type.
PHP Integer Data Type Examples:
Integers represent whole numbers without a decimal point.
<?php $integerVar = 42;
Float and Double Data Types in PHP:
Floats (or doubles) represent numbers with decimal points.
<?php $floatVar = 3.14;
PHP Boolean Data Type Usage:
Booleans represent true or false values.
<?php $isTrue = true; $isFalse = false;
PHP Array Data Type Explanation:
Arrays store multiple values.
<?php $numericArray = [1, 2, 3]; $stringArray = ['apple', 'orange', 'banana'];
Associative Arrays in PHP and Their Data Type:
Associative arrays use named keys.
<?php $assocArray = ['name' => 'John', 'age' => 25];
PHP Object Data Type and Object-Oriented Programming:
Objects are instances of classes in object-oriented programming.
<?php class Person { public $name; public $age; } $personObj = new Person();
PHP Resource Data Type Uses:
Resources are special variables holding references to external resources.
<?php $fileHandle = fopen('example.txt', 'r');
PHP NULL Data Type and Its Significance:
NULL represents the absence of a value.
<?php $nullVar = null;
Type Casting in PHP for Changing Data Types:
Convert a variable from one data type to another.
<?php $stringValue = '42'; $intValue = (int) $stringValue; // Type casting to integer
PHP Data Type Conversion Functions:
Use functions like intval()
, floatval()
, strval()
for conversion.
<?php $stringValue = '3.14'; $floatValue = floatval($stringValue);
Checking Variable Types in PHP with gettype()
and var_dump()
:
Determine the type of a variable.
<?php $exampleVar = 42; echo gettype($exampleVar); // Output: integer var_dump($exampleVar); // Output: int(42)
PHP Strict Typing and Declaring Return Types:
Enforce strict typing and declare return types in functions.
<?php declare(strict_types=1); function addNumbers(int $a, int $b): int { return $a + $b; }