PHP data type conversion_PHP tutorial

WBOY
Release: 2016-07-13 10:42:06
Original
1102 people have browsed it

PHP does not require (or support) explicit type definitions in variable definitions; the variable type is determined based on the context in which the variable is used. In other words, if a string value is assigned to the variablevar,varbecomes a string. If you assign an integer value tovar, 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 tointeger
  • (bool), (boolean) - Convert toboolean
  • (float), (double), (real) - Convert toFloat (float)
  • (string) - Convert tostring (string)
  • (binary) - convert to binarystring (string)(PHP 6)
  • (array) - Convert toarray (array)
  • (object) - Convert toobject (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

Convertstring(string)literals and variables to binarystring(string):

 = (binary) = b"binary string"?>
Copy after login

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

settypeSet the type of variable

bool settype( mixed$var, string$type)

Set the type of variablevartotype.

Possible values for

typeare:

  • "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)

ReturnsTRUEon success, orFALSEon failure.

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

www.bkjia.com true http: //www.bkjia.com/PHPjc/675205.html TechArticle PHP does not require (or does not support) explicit type definitions in variable definitions; the variable type is based on the use of the variable determined by the context. That is, if you assign a string value to...
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!