Php простые типы данных

PHP Data Types

Summary: in this tutorial, you will learn about PHP data types including scalar types, compound types, and special types.

Introduction to PHP data types

A type specifies the amount of memory that allocates to a value associated with it. A type also determines the operations that you can perform on it.

PHP has ten primitive types including four scala types, four compound types, and two special types:

Scalar types

Compound types

Special types

Scalar types

A variable is a scalar when it holds a single value of the type integer, float, string, or boolean.

Integer

Integers are whole numbers defined in the set . The size of the integer depends on the platform where PHP runs.

The constant PHP_INT_SIZE specifies the size of the integer on a specific platform. PHP uses the int keyword to denote the integer type.

The following example illustrates some integers:

 $count = 0; $max = 1000; $page_size = 10;Code language: HTML, XML (xml)

Float

Floats are floating-point numbers, which are also known as floats, doubles, or real numbers.

PHP uses the IEEE 754 double format to represent floats. Like other programming languages, floats have limited precision.

PHP uses the float keyword to represent the floating-point numbers. The following example illustrates the floating-point numbers in PHP:

 $price = 10.25; $tax = 0.08;Code language: HTML, XML (xml)

Boolean

Boolean represents a truth value that can be either true or false . PHP uses the bool keyword to represent the Boolean type.

The bool type has two values true and false . Since keywords are case-insensitive, you can use true , True , TRUE , false , False , and False to indicate boolean values.

The following example shows how to assign Boolean values to variables:

 $is_admin = true; $is_user_logged_in = false;Code language: HTML, XML (xml)

When you use the values of other types in the boolean context, such as if-else and switch-case statements, PHP converts them to the boolean values.

PHP treats the following values as false :

  • The false keyword.
  • The integer 0 and -0 (zero).
  • The floats 0.0 and -0.0 (zero).
  • The empty string ( «» , » ) and the string “0”.
  • The empty array ( array() or [] ).
  • The null .
  • The SimpleXML objects created from attributeless empty elements.

The values that are not one of these falsy values above are true .

String

A string is a sequence of characters surrounded by single quotes (‘) or double quotes (“). For example:

 $str = 'PHP scalar type'; $message = "PHP data types";Code language: HTML, XML (xml)

Compound types

Compound data includes the values that contain more than one value. PHP has two compound types including array and object.

Array

An array is an ordered map that associates keys with values. For example, you can define a list of items in a shopping cart like this:

 $carts = [ 'laptop', 'mouse', 'keyboard' ];Code language: HTML, XML (xml)

The $carts array contains three string values. It maps the index 0 , 1 , and 2 to the values ‘laptop’ , ‘mouse’ , and ‘keyboard’ . The $carts is called an indexed array because it uses numeric indexes as keys.

To access a value in an array, you use the square brackets:

 echo $carts[0]; // 'laptop' echo $carts[1]; // 'mouse' echo $carts[2]; // 'keyboard'Code language: HTML, XML (xml)

Besides numeric indexes, you can use strings as keys for the array elements. These arrays are known as associative arrays. For example:

 $prices = [ 'laptop' => 1000, 'mouse' => 50, 'keyboard' => 120 ];Code language: HTML, XML (xml)

To access an element in an associative array, you specify the key in the square brackets. For example:

 echo $prices['laptop']; // 1000 echo $prices['mouse']; // 50 echo $prices['keyboard']; // 120Code language: HTML, XML (xml)

Object

An object is an instance of a class. It’s a central concept in object-oriented programming.

An object has properties. For example, a person object may have the first name, last name, and age properties.

An object also has behaviors, which are known as methods. For example, a person object can have a method called getFullName() that returns the full name.

To learn more about objects, check out the object tutorial.

Special types

PHP has two special types: null and resource

Null

The null type has one value called null that represents a variable with no value.

Resource

The resource type holds a reference to an external resource, e.g. a filehandle or a database connection.

Summary

  • PHP has four scalar types, four compound types, and two special types.
  • Scale types: integer, float, string, and boolean.
  • Compound types: array and object.
  • Special types: null and resource.

Источник

Types

The meaning of a value is determined by its type. PHP’s types are categorized as scalar types and composite types. The scalar types are Boolean, integer, floating point, string, and null. The composite types are array, and object. The resource is an opaque type whose internal structure is not specified and depends on the implementation.

The scalar types are value types. That is, a variable of scalar type behaves as though it contains its own value.

The composite types can contain other variables, besides the variable itself, e.g. array contains its elements and object contains its properties.

The objects and resources are handle types. The type contains information — in a handle — that leads to the value. The differences between value and handle types become apparent when it comes to understanding the semantics of assignment, and passing arguments to, and returning values from, functions.

Variables are not declared to have a particular type. Instead, a variable’s type is determined at runtime by the value it contains. The same variable can contain values of different types at different times.

Useful library functions for interrogating and using type information include gettype , is_type , settype , and var_dump .

Scalar Types

General

The integer and floating-point types are collectively known as arithmetic types. The library function is_numeric indicates if a given value is a number or a numeric string.

The library function is_scalar indicates if a given value has a scalar type. However, that function does not consider NULL to be scalar. To test for NULL , use is_null .

Some objects may support arithmetic and other scalar operations and/or be convertible to scalar types (this is currently available only to internal classes). Such object types together with scalar types are called scalar-compatible types. Note that the same object type may be scalar-compatible for one operation but not for another.

The Boolean Type

The Boolean type is bool , for which the name boolean is a synonym. This type is capable of storing two distinct values, which correspond to the Boolean values true and false , respectively. The internal representation of this type and its values is unspecified.

The library function is_bool indicates if a given value has type bool .

The Integer Type

There is one integer type, int , for which the name integer is a synonym. This type is binary, signed, and uses twos-complement representation for negative values. The range of values that can be stored is implementation-defined; however, the range [-2147483648, 2147483647], must be supported. This range must be finite.

Certain operations on integer values produce a mathematical result that cannot be represented as an integer. Examples include the following:

  • Incrementing the largest value or decrementing the smallest value.
  • Applying the unary minus to the smallest value.
  • Multiplying, adding, or subtracting two values.

In such cases, the computation is done as though the types of the values were float with the result having that type.

The constants PHP_INT_SIZE , PHP_INT_MIN and PHP_INT_MAX define certain characteristics about type int .

The library function is_int indicates if a given value has type int .

The Floating-Point Type

There is one floating-point type, float , for which the names double and real are synonyms. The float type must support at least the range and precision of IEEE 754 64-bit double-precision representation.

The library function is_float indicates if a given value has type float . The library function is_finite indicates if a given floating-point value is finite. The library function is_infinite indicates if a given floating-point value is infinite. The library function is_nan indicates if a given floating-point value is a NaN .

The String Type

A string is a set of contiguous bytes that represents a sequence of zero or more characters.

Conceptually, a string can be considered as an array of bytes—the elements—whose keys are the int values starting at zero. The type of each element is string . However, a string is not considered a collection, so it cannot be iterated over.

A string whose length is zero is an empty string.

As to how the bytes in a string translate into characters is unspecified.

Although a user of a string might choose to ascribe special semantics to bytes having the value \0 , from PHP’s perspective, such null bytes have no special meaning. PHP does not assume strings contain any specific data or assign special values to any bytes or sequences. However, many library functions assume the strings they receive as arguments are UTF-8 encoded, often without explicitly mentioning that fact.

A numeric string is a string whose content exactly matches the pattern defined by the str-numeric production below. A leading-numeric string is a string whose initial characters follow the requirements of a numeric string, and whose trailing characters are non-numeric. A non-numeric string is a string that is not a numeric string.

str-numeric:: str-whitespaceopt signopt str-number str-whitespace:: str-whitespaceopt str-whitespace-char str-whitespace-char:: new-line Space character (0x20) Horizontal-tab character (0x09) Vertical-tab character (0x0B) Form-feed character (0x0C) str-number:: digit-sequence floating-literal 

Note that digit-sequence is interpreted as having base-10 (so «0377» is treated as 377 decimal with a redundant leading zero, rather than as octal 377).

Only one mutation operation may be performed on a string, offset assignment, which involves the simple assignment operator =.

The library function is_string indicates if a given value has type string.

The Null Type

The null type has only one possible value, NULL . The representation of this type and its value is unspecified.

The library function is_null indicates if a given value is NULL .

Composite Types

The Array Type

An array is a data structure that contains a collection of zero or more elements whose values are accessed through keys that are of type int or string . See more details in arrays chapter.

The library function is_array indicates if a given value is an array.

Objects

An object is an instance of a class. Each distinct class-declaration defines a new class type, and each class type is an object type. The representation of object types is unspecified.

The library function is_object indicates if a given value is an object, and the library function get_class indicates the name of an object’s class.

Resources

A resource is a descriptor to some sort of external entity. Examples include files, databases, and network sockets.

A resource is an abstract entity whose representation is unspecified. Resources are only created or consumed by the implementation; they are never created or consumed by PHP code.

Each distinct resource has a unique identity of some unspecified form.

The library function is_resource indicates if a given value is a resource, and the library function get_resource_type indicates the type of a resource.

Источник

Читайте также:  Определить конец файла python
Оцените статью