search
HomeBackend DevelopmentPHP ProblemWhat are the PHP data type conversion functions?

What are the PHP data type conversion functions?

Jun 30, 2021 pm 07:16 PM
phpData type conversion

php data type conversion function: 1. intval(), converted to integer type; 2. floatval(), converted to floating point type; 3. boolval(), converted to Boolean type; 4. strval( ), converted into a string type; 5. settype(), can be converted into a user-specified data type.

What are the PHP data type conversion functions?

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

php data type conversion Function

Specific conversion functions intval(), floatval(), boolval(), strval(), settype(), the functions of these functions are as follows:

  • 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 the variable;

  • strval(): used to get the string value of the variable;

  • settype(): used to set the type of variables.

Example 1:

<?php
    $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;;
    $string = strval($str);
    echo &#39;变量 $string 的类型为:&#39;.gettype($string);
?>

The running results are as follows:

变量 $int 的类型为:integer
变量 $float 的类型为:double
变量 $string 的类型为:string

intval(), floatval(), boolval() When the strval() function converts types, it will not change the type of the converted variable itself. Instead, it will assign the converted new type of data to the new variable, and the type and value of the original variable will remain unchanged.

Example 2:

<?php
    $str = (string)&#39;123abc&#39;;
    $bool = (bool)true;
    settype($str, &#39;integer&#39;);
    settype($bool, &#39;string&#39;);
    echo &#39;$str 的类型为:&#39;.gettype($str).&#39;<br> $bool 的类型为:&#39;.gettype($bool);
?>

The running results are as follows:

$str 的类型为:integer
$bool 的类型为:string

settype() function will change the type of the variable itself, the syntax format is:

settype(mixed &$var, string $type)

Recommended learning: "PHP Video Tutorial"

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment