Home  >  Article  >  Backend Development  >  How to convert numbers into strings in php

How to convert numbers into strings in php

王林
王林Original
2020-11-03 11:35:452956browse

php method to convert numbers into strings: You can use the strval() function to convert numbers into strings, such as [$str="123.9abc";strval($float);]. It can also be achieved through forced type conversion or the universal type conversion function settype().

How to convert numbers into strings in php

There are three conversion methods for PHP data types:

1. Forced conversion, enclosed in parentheses before the variable to be converted The target type

2. Use three specific type conversion functions, intval(), floatval(), strval()

3. Use the general type conversion function settype(mixed var,string type)

(Video tutorial recommendation: php video tutorial)

Example:

The first conversion method

<?php   
$num1=3.14;   
$num2=(int)$num1;   
var_dump($num1); //输出float(3.14)   
var_dump($num2); //输出int(3)   
?>

th Two conversion methods:

<?php   
$str="123.9abc";   
$int=intval($str);     //转换后数值:123   
$float=floatval($str); //转换后数值:123.9   
$str=strval($float);   //转换后字符串:"123.9"    
?>

The third conversion method:

<?php   
$num4=12.8;   
$flg=settype($num4,"int");   
var_dump($flg);  //输出bool(true)   
var_dump($num4); //输出int(12)   
?>

Related recommendations: php training

The above is the detailed content of How to convert numbers into strings in php. 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