# PHP Data Types

PHP supports many different data types, but they are generally divided in two categories:

scalar and composite.

A scalar value contains only one value at a time. PHP supports four scalar types:

```boolean A value that can only either be true or false
int A signed numeric integer value
float A signed floating-point value
string A collection of binary data```

Numeric Values
PHP recognizes two types of numbers, integers and floating-point values. The int data type is used to represent signed integers (meaning that both positive and negative numbers can be expressed with it). Numbers can be declared using several different notations:

```Decimal
10; -11; 1452
Standard decimal notation. Note that no thousand separator is needed—or, indeed, allowed.```
```Octal
0666, 0100
Octal notation—identified by its leading zero and used mainly to express UNIX-style access
permissions.```
```Hexadecimal
0x123; 0XFF; -0x100
Base-16 notation; note that the hexadecimal digits and the leading 0x prefix are both
case-insensitive.```

Floating-point numbers (also called floats and, sometimes, doubles) are numbers that have a fractional component; like integers, they are also signed. PHP supports two different notations for expressing them:

```Decimal
0.12; 1234.43; -.123
```Exponential
2E7, 1.2e2
Exponential notation—a set of significant digits (also called the mantissa), followed by the
case-insensitive letter E and by an exponent. The resulting number is expressed multiplied by
ten  to the power of the exponent—for example, 1e2 equals 100.```

There are a few important gotchas that you need to be aware of when dealing with numbers. First of all, the precision and range of both types varies depending on the platform on which your scripts run.

For example, 64-bit platforms may, depending on how PHP was compiled, be capable of representing a wider range of integer numbers than 32-bit platforms. What’s worse, PHP doesn’t track overflows, so that the result of a seemingly innocuous operation like an addition can have catastrophic consequences on the reliability of your application.

Most importantly, you need to be aware that the float data type is not always capable of representing numbers in the way you expect it to. Consider, for example this very simple statement:

• `echo (int) ((0.1 + 0.7) * 10);`

You would expect that the expression ((0.1 + 0.7) * 10) would evaluate to 8 (and, in fact, if you print it out without the integer conversion, it does). However, the statement above outputs 7 instead. This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact).

The lesson that you need to take home from all this is simple: know the limitations of your numeric data types, and plan around them. Whenever the precision of your calculation is a relevant factor to the proper functioning of your application, you should consider using a the arbitrary precision functions provided by the BCMath extension (you can search for it in your copy of the PHP manual) instead of PHP’s built-in data types.

Strings
In the minds of many programmers, strings are equivalent to text. While in some languages this is, indeed, the case, in many others (including PHP), this would be a very limiting—and, in some cases, incorrect—description of this data type. Strings are, in fact, ordered collections of binary data—this could be text, but it could also be the contents of an image file, a spreadsheet, or even a music recording.

PHP provides a vast array of functionality for dealing with strings.

Booleans
A Boolean datum can only contain two values: true or false. Generally speaking, Booleans are used as the basis for logical operations.

When converting data to and from the Boolean type, several special rules apply:

```• A number (either integer or floating-point) converted into a Boolean becomes false if the
original value is zero, and true otherwise.```
```• A string is converted to false only if it is empty or if it contains the single character 0.
If it contains any other data—even multiple zeros—it is converted to true.```
`• When converted to a number or a string, a Boolean becomes 1 if it is true, and 0 otherwise.`

Compound Data Types
In addition to the scalar data type that we have just examined, PHP supports two compound data types—so called because they are essentially containers of other data:

```• Arrays are containers of ordered data elements; an array can be used to store and retrieve any
other data type, including numbers, Boolean values, strings, objects and even other arrays.```
`• Objects are containers of both data and code. They form the basis of Object-oriented Programming.`

Other Data Types
In addition to the data types that we have seen so far, PHP defines a few additional types that are used in special situations:

```• NULL indicates that a variable has no value. A variable is considered to be NULL if it has been
assigned the special value NULL, or if it has not yet been assigned a value at all—although in
the latter case PHP may output a warning if you attempt to use the variable in an expression.```
```• The resource data type is used to indicate external resources that are not used natively by PHP,
but that have meaning in the context of a special operation— such as, for example, handling files
or manipulating images.```

Converting Between Data Types
As we mentioned, PHP takes care of converting between data types transparently when a datum is used in an expression. However, it is still possible to force the conversion of a value to a specific type using type conversion operators. These are simply the names of the data type you want to convert to enclosed in brackets and placed before an expression. For example:

```<?php
\$x = 10.88;
echo (int) \$x; // Outputs 10
?>
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.88885576,"math_prob":0.92795134,"size":5890,"snap":"2022-27-2022-33","text_gpt3_token_len":1265,"char_repetition_ratio":0.10737343,"word_repetition_ratio":0.0081883315,"special_character_ratio":0.21748726,"punctuation_ratio":0.11873351,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96416366,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-26T10:54:21Z\",\"WARC-Record-ID\":\"<urn:uuid:7a3a8ad6-b337-4073-a7c4-e47bfc4efb01>\",\"Content-Length\":\"31348\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:921d2c26-cc5c-4596-95f6-ef2bc406efe8>\",\"WARC-Concurrent-To\":\"<urn:uuid:61c181a2-3b8b-4633-a593-7175554e4af1>\",\"WARC-IP-Address\":\"204.93.183.55\",\"WARC-Target-URI\":\"https://www.phpmoot.com/data-types/\",\"WARC-Payload-Digest\":\"sha1:UOBODACB366DDNKW3BVPCVJ4LUAXZDSW\",\"WARC-Block-Digest\":\"sha1:DOFWIGICW766UB4FV7Q6CRMSWQVI7BCY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103205617.12_warc_CC-MAIN-20220626101442-20220626131442-00021.warc.gz\"}"}