Home  >  Article  >  Backend Development  >  What are the functions to convert numbers to strings in php?

What are the functions to convert numbers to strings in php?

青灯夜游
青灯夜游Original
2021-05-08 10:51:532979browse

Function to convert numbers to strings in php: 1. strval(), syntax format "strval (numeric variable)", which can obtain and return the string value of the variable; 2. settype(), syntax format " settype(numeric variable,'string')" can set the variable to the "string" type.

What are the functions to convert numbers to strings in php?

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

digital conversion in php String

Method 1: Use strval() function

<?php
$int_str= 123.52;
var_dump($int_str);
$str = strval($int_str);
var_dump($str);
?>

Output:

float 123.52
string &#39;123.52&#39; (length=6)

Description:

The strval() function is a conversion function used to convert numerical values ​​to strings. It can obtain and return the string value of a variable.

The strval() function converts the data type, but does not affect the type of the original variable.

Method 2: settype() function

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$old = 500;
echo "原数据类型" . gettype($old), &#39;<br>&#39;;
$current = gettype(settype($old, &#39;string&#39;));
echo "现数据类型" . gettype($current), &#39;<hr>&#39;;

$old = 500.25;
echo "原数据类型" . gettype($old), &#39;<br>&#39;;
$current = gettype(settype($old, &#39;string&#39;));
echo "现数据类型" . gettype($current), &#39;<hr>&#39;;
?>

Output:

What are the functions to convert numbers to strings in php?

Description: settype( ) function is used to set the type of a variable. The syntax is as follows:

settype ( $var , $type )

Set the type of variable var to type.

To be converted Variables. The possible values ​​of settype() function will affect The type of the original variable.
Parameters Description
##var
type
##type

are:

"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 deprecated)
  • "string"
  • "array"
  • "object"
  • "null" (from PHP 4.2.0)

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of What are the functions to convert numbers to 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