How to force string to other types in php

青灯夜游
Release: 2023-03-15 10:06:01
Original
1991 people have browsed it

Forcing method: 1. Add the target type enclosed in parentheses before the string variable, such as "(int)$str", "(bool)$str", "(float)$str" "; 2. Use the conversion functions intval(), floatval(), boolval(), and settype().

How to force string to other types in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php will string Force conversion to other types

#Method 1: Add the target type enclosed in parentheses before the string variable

  • (int), (integer): Convert to integer type;

  • (bool), (boolean): Convert to Boolean type;

  • (float), (double), (real): Convert to floating point type;

  • (array): Convert to array type;

  • (object): Convert to object type.

Example:

<?php
header("Content-type:text/html;charset=utf-8"); 
$str = &#39;123.456abc&#39;;
$int = (int)$str;
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
$float = (float)$str;
echo &#39;变量 $float 的类型为:&#39;.gettype($float).&#39;<br>&#39;;
$bool = (bool)$str;
echo &#39;变量 $bool 的类型为:&#39;.gettype($bool);
?>
Copy after login

How to force string to other types in php

2. Use the cast function

  • intval(): used to get the integer value of the variable;

  • floatval(): used to get the floating point value of the variable;

  • boolval(): used to get the Boolean value of a variable;

  • settype(): used to set a variable to a specified type (the settype() function will change the original type of the variable).

<?php
header("Content-type:text/html;charset=utf-8"); 
$str = &#39;123.456abc&#39;;
$int = intval($str);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
$float = floatval($str);
echo &#39;变量 $float 的类型为:&#39;.gettype($float).&#39;<br>&#39;;
$bool = boolval($str);
echo &#39;变量 $bool 的类型为:&#39;.gettype($bool).&#39;<br>&#39;;

$arr = settype($str,"array");
echo &#39;变量 $str 的类型为:&#39;.gettype($str);
?>
Copy after login

How to force string to other types in php

Description: The value of the second parameter (set type) of the settype() function can be:

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

  • "string"

  • "array"

  • "object"

  • "null" (Starting from PHP 4.2.0)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to force string to other types in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!