Php data types table

PHP Data Types

This is one boring topic we have in all programming languages – a list of data types. It is so boring that no one cares to read it once completed. Sometimes surprisingly developers discover that they have not used a particular data type.

Data types are generally used to represent the type of data associated with PHP variables, function’s return type/parameters and etc. We get accustomed to them over the period of use.

To the better of all PHP is a loosely typed language, there is no need of specifying data type while declaring variables.

PHP Primitive Data Types

There are eight primitive data types in PHP and it is classified into 3 types.

1. Scalar Data Types

Scalar data types will contain only a single value for variable references.

  1. Integer – PHP integer data type will be used for representing non-fractional numeric values. The range of integers varies with respect to the platform. That is, 2^32 – 2^31 for 32-bit, and, 2^64 – 2^63 for 64-bit.
  2. Float/Double – These data types will represent fractions. For example, 8.99, 0.1 and etc. The double data type supports fractions with more decimal digits.
  3. String – A sequence of bytes enclosed by a pair of single or double quotes is called a PHP string. We can also use heredoc or nowdoc. But double quotes support variable interpolation.
  4. Boolean – It contains case insensitive TRUE or FALSE values. Zero represents FALSE and any non-zero value represents boolean TRUE.
Читайте также:  Java get class null object

2. Compound Data Types

Compound data type, as per its name, will have a group of data in the same type.

  1. Array – PHP Arrays are used to contain a group of values with the same data type. This group of values can be specified with default numeric indices or some associative indices.
  2. Object – This is another compound data type in PHP. Using PHP object data type, we can have a collection of properties set with it.

3. Special Data Types

  • Resource – PHP resource datatype is used to refer to external resource data. For example, file resources, database resources and etc.
  • NULL – This type of data contains PHP constant NULL which is case insensitive. We can use the is_null() function to check any value if it is NULL.

Pseudo-Types in PHP

Apart from the above major classifications, PHP includes pseudo data types. For that, PHP provides keywords that we can see with the syntax of PHP functions. These are used for representing the function’s arguments type or return type.

  • void – We know very well that it is representing empty parameters or return type.
  • mixed – It indicates a PHP function supporting more data types for its arguments and data type.
  • number – This keyword indicates that a function will accept any type of numeric parameter either integer or float and the same for its return type.
  • callback – This keyword represents that the parameter is having the name of the callback function. But, we should use user-defined function names, and not pre-defined PHP constructs like echo(), print()…
Читайте также:  Javascript что такое span
  • gettype() – data type getter returns given variables data types.
  • var_dump() – This function used to get the data type of an expression.
  • is_type>() – There are several variable functions like is_array(), is_null() and etc., to check a variable with respect to specific data type.
  • settype() – This is the data type setter, used in PHP type conversion.

Leave a Reply Cancel reply

Источник

PHP Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers — also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String

A string is a sequence of characters, like «Hello world!».

A string can be any text inside quotes. You can use single or double quotes:

Example

PHP Integer

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

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation

In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

Example

PHP Float

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

In the following example $x is a float. The PHP var_dump() function returns the data type and value:

Example

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE.

Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

PHP Array

An array stores multiple values in one single variable.

In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

Example

You will learn a lot more about arrays in later chapters of this tutorial.

PHP Object

Classes and objects are the two main aspects of object-oriented programming.

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

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Let’s assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Example

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 «
«;
$myCar = new Car(«red», «Toyota»);
echo $myCar -> message();
?>

PHP NULL Value

Null is a special data type which can have only one value: NULL.

A variable of data type NULL is a variable that has no value assigned to it.

Tip: If a variable is created without a value, it is automatically assigned a value of NULL.

Variables can also be emptied by setting the value to NULL:

Example

PHP 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.

A common example of using the resource data type is a database call.

We will not talk about the resource type here, since it is an advanced topic.

Источник

What are the Different Data Types in PHP and How to Use Them?

Different Data Types

The values assigned to a PHP variable will be Different Data Types including numeric types and simple string to more complex data types as objects and arrays.
PHP data types define the type of data a variable can store, PHP supports a total of eight different types of data types Integer, Float, Array, String, Booleans, Object, resource, and NULL.
these PHP allows eight different types of data types are used to construct variable. let’s discuss all data types in detail, the first five data types are called simple data types and the last three data types are called the compound data types, or all eight different data types that can be categorized into three types

Note: strings are classified as alphanumeric characters.
Integers are classified as Whole Numbers.
Floating points are classified as Numbers with decimal Points.
Boolean is classified as True or false values.
Integer included whole numbers like as e.g. -2, -5, 0, 9, 32.

PHP Different Data Types: Classification Data Types Table

Scalar Types Compound Types Special Types
The Scalar data types hold only a single value. There are 4 types of scalar data types in PHP.
1. boolean
2. integer
3. float
4. string
The Compound Data Types hold only a single value. There are 2 types of Compound data Types in PHP.
1. array
2. object
There are 2 types of Special data Types in PHP.
1. resource
2. NULL

Источник

PHP Data Types

PHP 7 Data types: The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.

PHP supports a total of eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.

php data types

The PHP 7 Data types are Given Below with their definition and examples.

PHP Integers

Integers are whole numbers, without a decimal point (…, -2, -1, 0, 1, 2, …). Integers can be specified in decimal (base 10), hexadecimal (base 16 – prefixed with 0x ) or octal (base 8 – prefixed with 0 ) notation, optionally preceded by a sign ( — or + ).

PHP 8 Integers - Data Types

"; $b = -123; // a negative number var_dump($b); echo "
"; $c = 0x1A; // hexadecimal number var_dump($c); echo "
"; $d = 0123; // octal number var_dump($d); ?>

Note: Since PHP 5.4+ you can also specify integers in binary (base 2) notation. To use binary notation precede the number with 0b (e.g. $var = 0b11111111; ).

PHP Strings

PHP 8 String

Strings are sequences of characters, where every character is the same as a byte. A string can hold letters, numbers, and special characters and it can be as large as up to 2GB (2147483647 bytes maximum). The simplest way to specify a string is to enclose it in single quotes (e.g. ‘Hello world!’), however, you can also use double quotes (“Hello world!”).

"; $b = "Hello world!"; echo $b; echo "
"; $c = 'Stay here, I\'ll be back.'; echo $c; ?>

You will learn more about strings in PHP Strings tutorial.

PHP Floating Point Numbers or Doubles

PHP 8 Floats

Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers as demonstrated in the example below.

"; $b = 10.2e3; var_dump($b); echo "
"; $c = 4E-10; var_dump($c); ?>

PHP Booleans

PHP 8 Boolean

Booleans are like a switch it has only two possible values either 1 (true) or 0 (false).

PHP Arrays

php 8 array data type

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example, a set of country or city names. An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.

"; $color_codes = array( "Red" => "#ff0000", "Green" => "#00ff00", "Blue" => "#0000ff" ); var_dump($color_codes); ?>

You will learn more about arrays in PHP Array tutorial.

PHP Objects

An object is a data type that not only allows storing data but also information on, how to process that data. An object is a specific instance of a class that serves as templates for objects. Objects are created based on this template via the new keyword.

Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

Here’s a simple example of a class definition followed by object creation.

str; > > // Create object from class $message = new greeting; var_dump($message); ?>

Tip: The data elements stored within an object are referred to as its properties and the information, or code which describing how to process the data is called the methods of the object.

PHP NULL

The special NULL value is used to represent empty variables in PHP. A variable of type NULL is a variable without any data. NULL is the only possible value of type null.

"; $b = "Hello World!"; $b = NULL; var_dump($b); ?>

When a variable is created without a value in PHP like $var; it is automatically assigned a value of null. Many novice PHP developers mistakenly considered both $var1 = NULL; and $var2 = «»; are the same, but this is not true. Both variables are different — the $var1 has a null value while $var2 indicates no value assigned to it.

PHP Resources

A resource is a special variable, holding a reference to an external resource.

Resource variables typically hold special handlers to opened files and database connections.

Источник

Оцените статью