Home > Backend Development > PHP Tutorial > PHP data type conversion, PHP data type_PHP tutorial

PHP data type conversion, PHP data type_PHP tutorial

WBOY
Release: 2016-07-12 09:00:27
Original
841 people have browsed it

php data type conversion, php data type

PHP does not require (or does not support) explicit type definition in variable definition; the variable type is determined based on the context in which the variable is used. decided. In other words, if a string value is assigned to the variable var, var becomes a string. If you assign an integer value to var, it becomes an integer.

An example of PHP's automatic type conversion is the plus sign " ". If any operand is a floating point number, all operands are treated as floating point numbers, and the result is also a floating point number. Otherwise the operands are interpreted as integers and the result is also an integer. Note that this does not change the types of the operands themselves; only how the operands are evaluated and the type of the expression itself is changed.

Type casting

The allowed casts are:

  • (int), (integer) - converted to integer
  • (bool), (boolean) - Convert to boolean
  • (float), (double), (real) - Convert to Float (float)
  • (string) - Convert to string (string)
  • (binary) - Convert to binary string (PHP 6)
  • (array) - Convert to array (array)
  • (object) - Convert to object (object)
  • (unset) - Convert to NULL (PHP 5)

(binary) conversion will prefix the result with 'b', new in PHP 5.2.1.

Note that spaces and tabs are allowed within brackets

Convert string literals and variables to binary string:

<?php
$binary = (binary)$string;
$binary = b"binary string";
?>
Copy after login

If you want to change the type of a variable, see settype();

settype — Set the type of variable

bool settype ( mixed $var , string $type )

Set the type of variable var to type.

Possible values ​​for

type are:

  • "boolean" (or "bool" since PHP 4.2.0)
  • "integer" (or "int" since PHP 4.2.0)
  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
  • "string"
  • “array”
  • "object"
  • "null" (since PHP 4.2.0)

Returns TRUE on success, or FALSE on failure.

intval(), floatval(), strval(), these three functions can also be converted

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1093468.htmlTechArticlephp data type conversion, php data type PHP does not require (or does not support) explicit type definition in variable definition ;The variable type is determined based on the context in which the variable is used. That is...
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