PHP data type conversion and detection

不言
Release: 2023-03-25 14:54:02
Original
1462 people have browsed it

This article mainly introduces the conversion and detection of PHP data types. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Data types Conversion

Although PHP is a weakly typed language, data can be converted just like C language. Just add the type name enclosed in parentheses before the variable.

  1. Convert to Boolean type: such as (boolean)

    ##str

  2. Convert to character type: such as (string)

    ##flo

  3. Convert to integer type: such as (integer)
  4. ##str

    Convert to floating point type: For example, (float)$str
  5. Convert to array: For example, (array)$str
  6. Convert to object: Such as (obiect)$str
  7. Note: The following should be noted during type conversion: When converting to boolean type, null, 0 and unassigned variables or arrays will be Convert to false, others to true; when converting to integer, false of Boolean type is converted to 0, true is converted to 1, the decimal part of floating point type is discarded, and if character type starts with a number, it will be intercepted into non-number bit, otherwise output 0.
In addition, type conversion can also be set through the settype function.

bool settype(mixed var,string type)

Note: The parameter var is the specified variable; the parameter type is the specified data type. The parameter type has 7 optional values, namely boolean, float, .integer, array, null, object and string. If the conversion is successful, the settype() function returns true, otherwise it returns false.

When the string is converted to an integer or floating point type, if the string starts with a number, the number part will be converted to an integer first. type, and then the following string will be removed; if the number contains a decimal point, the number before the decimal point will be retrieved.


2. Data type detection

is bool Check whether the variable is a Boolean type: such as is bool(true), is_bool(false)
  1. is_string Check whether the variable is a string type: such as is_string('string') is_string( 1234)
  2. is float/is double check Check whether the variable is a floating point type: such as is_float(3.1415), is_float(3 1415)
  3. is_integer/is_int Check whether the variable is an integer: such as is_integer(34), is_integer('34' )
  4. is_null Check whether the variable is null: such as is_null(null)
  5. is_array Check whether the variable is an array type: such as i_sarray($arr )
  6. is_object checks whether the variable is an object type: such as is_object($obj)
  7. is_numeric checks whether the variable is a number or consists of numbers Strings: such as is_numeric('5'), is_numeric('aabb')
  8. Related recommendations:


PHP data type conversion ( Character to number, number to character)

The above is the detailed content of PHP data type conversion and detection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template