PHP Data Types


Data Types

1. Data types in PHP are used to store various sorts of data or values. PHP provides eight primitive data types, which are further divided into three categories:

  • Types of Scalars (predefined)
  • Types of Compounds (user-defined)
  • Types that are unique

PHP Types of data: Types of Scalars

  • It simply stores one value. In PHP, there are four scalar data types.
  • boolean\sinteger\sfloat\sstring
  • Compound Types are a subset of PHP data types.
  • It has the ability to store numerous values. In PHP, there are two forms of compound data.
  • array\sobject

Data Types in PHP

Various data types can be stored in variables, and different data types can accomplish other things.

The following data types are supported by PHP:

  • String\sInteger
  • Allow yourself to float (floating point numbers - also called double)
  • Boolean\sArray
  • Object\sNULL\sResource

Strings in PHP

A string, such as "Hello world!" is a collection of characters.

Any text enclosed in quotations can be used as a string. You have the option of using single or double quotes:

Example

<?php $x = "Hello!"; $y = 'knowledge 2life'; echo $x; echo "<br>"; echo $y; ?>

output:

Hello! knowledge 2life

Integer in PHP

A non-decimal number between -2,147,483,648 and 2,147,483,647 is an integer data type.

Integer rules are as follows:

At least one digit is required for an integer.

A decimal point cannot be used in an integer.

A positive or negative integer is a number that can be either positive or negative.

Decimal (base 10) notation, hexadecimal (base 16), octal (base 8) notation, and binary (base 2) notation can all be used to specify integers.

In the example below, $x is an integer. The PHP function var dump() returns the data type and value.

Example

<?php $x = 8595; var_dump($x) ?>

output:

int 8595

Float in PHP

A float (a floating-point number) is a decimal or exponential number with a decimal point.

In the example below, $x is a float. The data type and value are returned by the PHP var dump() function:

Example

<?php $x = 14.314; var_dump($x); ?>

output:

float 14.314

Boolean expressions in PHP

TRUE or FALSE are the two possible states of a Boolean.

Booleans are frequently used in conditional testing, with $x = true and $y = false. In a subsequent chapter of this course, you'll learn more about conditional testing.

Example

<?php $var=TRUE; echo $var. "\n"; var_dump($var); $var1=FALSE; echo $var1; var_dump($var1); ?>

output:

1 boolean true boolean false

Array in PHP

An array is a single variable that holds many values.

$cars is an array in the following example. The data type and value are returned by the PHP var dump() function:

Example

<?php $cars = array("Honda","BMW","Ferrari"); var_dump($cars); ?>

output:

array (size=3) 0 => string 'Honda' (length=5) 1 => string 'BMW' (length=3) 2 => string 'Ferrari' (length=7)

Object in PHP

Object-oriented programming is divided into two parts: classes and objects.

An object is an instance of a class, a template for objects.

Individual objects inherit all of the attributes and actions of the class when they are formed, but the properties will have distinct values for each object.

Let's pretend we have a Car class. A car can have characteristics such as model, color, etc. To hold the values of these characteristics, we may construct variables like $model, $ color, and so on.

When separate objects (Volvo, BMW, Toyota, and so on) are formed, they inherit all of the class's attributes and behaviors, but each object's properties will be distinct.

PHP will automatically call the __construct() function you defined when you create an object from a class.

Example

<?php class Car { public $color; public $model; public function __construct($color, $model) { $this->color = $color; $this->model = $model; } public function message() { return "My car is a " . $this->color . " " . $this->model . "!"; } } $myCar = new Car("black", "Volvo"); echo $myCar -> message(); echo "<br>"; $myCar = new Car("red", "Toyota"); echo $myCar -> message(); ?>

NULL Value in PHP

Null is a unique data type with only one possible value: NULL.

The NULL data variant does not have a value associated with it.

When a variable is created without a value, it is given NULL by default.

The value of a variable can likewise be set to NULL to empty it.

Example

<?php $x = "knowledge 2life!"; $x = null; var_dump($x); ?>

output:

null