php中数字转字符串的函数:1、strval(),语法格式“strval (数字变量)”,可获取并返回变量的字符串值;2、settype(),语法格式“settype(数字变量,'string')”,可将变量设置为“string”类型。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
php中数字转字符串
方法1:使用strval()函数
<?php $int_str= 123.52; var_dump($int_str); $str = strval($int_str); var_dump($str); ?>
输出:
立即学习“PHP免费学习笔记(深入)”;
float 123.52 string '123.52' (length=6)
说明:
strval()函数是用于数值转字符串的转换函数,可以获取并返回变量的字符串值。
strval()函数转换数据类型,但不会影响原变量的类型。
方法2:settype()函数
<?php header('content-type:text/html;charset=utf-8'); $old = 500; echo "原数据类型" . gettype($old), '<br>'; $current = gettype(settype($old, 'string')); echo "现数据类型" . gettype($current), '<hr>'; $old = 500.25; echo "原数据类型" . gettype($old), '<br>'; $current = gettype(settype($old, 'string')); echo "现数据类型" . gettype($current), '<hr>'; ?>
输出:
立即学习“PHP免费学习笔记(深入)”;
说明:settype() 函数用于设置变量的类型。语法如下:
settype ( $var , $type )
将变量 var 的类型设置成 type。
参数 | 描述 |
---|---|
var | 要转换的变量。 |
type |
type 的可能值为:
|
settype()函数会影响原变量的类型。
推荐学习:《PHP视频教程》
以上就是php中数字转字符串的函数有哪些的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号