PHP Data Types

WBOY
發布: 2024-08-29 12:37:56
原創
824 人瀏覽過

PHP or Hypertext PreProcessor is a web-based application development programming language that can incorporate HTML coding in them for constructing a web application. In PHP, there are eight different data types that are used for declaring and calling the variables in the script. They are ‘Boolean’ for true or false values, ‘Integer’ for numeric values, ‘Float/Double’ for decimal numerals, ‘String’ for characters, ‘Arrays’ for fixing element size, ‘object’ for representing instances of the class, ‘NULL’ for a void, and ‘resources’ for referring to elements outside the PHP script.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Top 3 PHP Data Types

PHP variables used to store values may be associated with all kinds of data types ranging from the simplest int to more complicated data types such as arrays. PHP is called a loosely typed Programming language, which means the variable data types are decided based on their attributes during run-time and are not explicitly defined. It analyses the value-given attributes and then determines the data type to be assigned to it. There are 8 primitive data types which PHP supports and which can be further classified into 3 types as below:

Let us go through each one of them in detail with an example each.

PHP Data Types

1. Scalar Types

They can be further divided into primitive types as below:

a. Boolean

These types have their possible output in the form of either 0 or 1, i.e. true or false. They are used for conditional testing cases where the event returns true when the condition is satisfied and false when it does not satisfy. It also considers NULL and empty string as false.

Code:

登入後複製

Output:

PHP Data Types

b. Integer

An integer data type holds non-decimal whole number values between -2,147,483,648 and 2,147,483,647. This maximum and minimum value depend on the system, whether it is 32-bit or 64-bit. By using the constant PHP_INT_MAX, we can find out the max value. It also holds base 10, base 8 and base 6 values.

Code:

登入後複製

Output:

PHP Data Types

c. Float/ Double

A number having a decimal point or an exponent is called a floating-point number/ real number. It can have both positive and negative numbers. There shall be a pre-defined number of decimal places displayed for the number.

Code:

登入後複製

Output:

PHP Data Types

d. String

A string data type is basically a collection of characters, including numbers, alphabets, and letters. They can hold values up to 2GB. They are to be declared using double quotes if a variable has to be displayed amongst the string. Else, a single quote also works.

Code:

登入後複製

Output:

PHP Data Types

2. Compound Types

These are the ones for which new values cannot be assigned. Arrays and objects fall under this category.

a. Arrays

It is a data structure having a collection of a fixed size of elements with similar data types. It is also used to store the known amount of key-value pairs in the form of an ordered map in it. It can be used for various purposes like a list, hash table (map implementation), collection, stack, dictionary, queue, etc.; multi-dimensional arrays are also possible.

A simple example of an array is as follows:

Code:

 "Puppy", "Cat" => "Kitten", "Cow" => "Calf" ); var_dump($animal_babies); ?>
登入後複製

Output:

PHP Data Types

b. Objects

It allows storing data (called its properties) and gives information on how to process (called the methods of the object) the same. An object serves as an instance of a class that is used as templates for other objects. The keyword “new” is used for the creation of an object.

Each object inherits the properties and methods from that of the parent class. It requires an explicit declaration and a “class” in each object.

Code:

stmt; } } // Creation of new object $msg = new statement; var_dump($msg); ?>
登入後複製

Output:

PHP Data Types

3. Special Types

There are 2 special data types in PHP which fall under this category since they are unique. They are:

a. NULL

In PHP, this special NULL is used for representing empty variables, i.e. the variable has no data in it, and NULL is the only possible value to it. If it has been set to unset() or if no value has been set to it, a variable assigned to the constant NULL becomes a NULL data type.

Here we are setting NULL directly to val1. For the val2 variable, we are assigning a string value first and then setting it as NULL. In both cases, the final value of variables is NULL.

Code:

"; $val2 = "Any string"; $val2 = NULL; var_dump($val2); ?>
登入後複製

Output:

PHP Data Types

b. Resources

A resource is not an actual data type, whereas it is a special variable that keeps a reference to a resource external to PHP. They hold special handlers for files and database connections that are open. Special functions usually create and use these resources.

To run this code, we must have the file.txt created in the system with reading permission given to it. It throws an error in case “handle” is not a resource. Also, make sure to connect to any existing database in your system.

Code:

"; // Connecting to MySQL database server with settings set to default $db = mysql_connect("localhost", "root", ""); var_dump($db); ?>
登入後複製

Apart from the above data types, we also have something called pseudo-types: the keywords in PHP document used to indicate the types or values that an argument can have. Some of them are:

  • mixed: They allow a parameter to accept more than one type. Ex: gettype()
  • number: With a number, a parameter can be afloat or an integer.
  • void, callback, array|object are some of the other pseudo-types

Conclusion

Here we have covered almost all of the data types which are available in PHP. All of the above 8 primitive types are implicitly supported by PHP, and there is no need for the user to specify them manually. Arrays and objects can hold multiple values, whereas, for rest, all can hold only a single value (except NULL, which holds no value).

以上是PHP Data Types的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!