search
HomeBackend DevelopmentPHP ProblemHow to convert value into string in php

How to convert value into string in php

Mar 10, 2022 pm 04:09 PM
phpstringtype conversion

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));
?>

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);
?>

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));
?>

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));
?>

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!

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.