Home > Backend Development > PHP Problem > How to convert value into string in php

How to convert value into string in php

青灯夜游
Release: 2023-03-15 15:24:01
Original
3877 people have browsed it

Conversion method: 1. Use strval(), syntax "strval(value)"; 2. Use settype(), syntax "settype (value, "string")"; 3. Use sprintf() function , the syntax is "sprintf(value)"; 4. Use the number_format() function.

How to convert value into string in php

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

php transfers the value Into a string

#Method 1: Use strval() function

strval(): Used to obtain the string value of a variable.

<?php
header("Content-type:text/html;charset=utf-8");
var_dump(strval(TRUE));
var_dump(strval(FALSE));
var_dump(strval(12.35));
var_dump(strval(null));
?>
Copy after login

How to convert value into string in php

Method 2: Use the settype() function

settype ($var,$type) The function is used to set the variable $var to the specified data type $type.

<?php
header("Content-type:text/html;charset=utf-8");
$num=12.63;
$bool1=TRUE;
$bool2=FALSE;
settype ($num,"string");
settype ($bool1,"string");
settype ($bool2,"string");
var_dump($num);
var_dump($bool1);
var_dump($bool2);
?>
Copy after login

How to convert value into string in php

Method 3: Use sprintf() function

sprintf() function writes the formatted string into a variable middle.

<?php
header("Content-type:text/html;charset=utf-8");
var_dump(sprintf(TRUE));
var_dump(sprintf(FALSE));
var_dump(sprintf(null));
var_dump(sprintf(12));
var_dump(sprintf("%.1f",12.35));
var_dump(sprintf("%.3f",12.35));
?>
Copy after login

How to convert value into string in php

Method 4: Use the number_format() function

number_format() can be used to format numbers by thousands grouping.

<?php
header("Content-type:text/html;charset=utf-8");
var_dump(number_format(TRUE));
var_dump(number_format(FALSE));
var_dump(number_format(null));
var_dump(number_format(12));
var_dump(number_format(12.35,1));
var_dump(number_format(12.35,3));
?>
Copy after login

How to convert value into string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert value into string 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