Какие типы переменных существуют 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.

Источник

Какие типы переменных существуют php

PHP is a dynamically typed language, which means that by default there is no need to specify the type of a variable, as this will be determined at runtime. However, it is possible to statically type some aspect of the language via the use of type declarations.

Types restrict the kind of operations that can be performed on them. However, if an expression/variable is used in an operation which its type does not support, PHP will attempt to type juggle the value into a type that supports the operation. This process depends on the context in which the value is used. For more information, see the section on Type Juggling.

The type comparison tables may also be useful, as various examples of comparison between values of different types are present.

Note: It is possible to force an expression to be evaluated to a certain type by using a type cast. A variable can also be type cast in-place by using the settype() function on it.

To check the value and type of an expression, use the var_dump() function. To retrieve the type of an expression, use the get_debug_type() function. However, to check if an expression is of a certain type use the is_ type functions instead.

$a_bool = true ; // a bool
$a_str = «foo» ; // a string
$a_str2 = ‘foo’ ; // a string
$an_int = 12 ; // an int

echo get_debug_type ( $a_bool ), «\n» ;
echo get_debug_type ( $a_str ), «\n» ;

// If this is an integer, increment it by four
if ( is_int ( $an_int )) $an_int += 4 ;
>
var_dump ( $an_int );

// If $a_bool is a string, print it out
if ( is_string ( $a_bool )) echo «String: $a_bool » ;
>
?>

Output of the above example in PHP 8:

Note: Prior to PHP 8.0.0, where the get_debug_type() is not available, the gettype() function can be used instead. However, it doesn’t use the canonical type names.

User Contributed Notes

Источник

Читайте также:  Learn python programming third edition pdf
Оцените статью