search
HomeBackend DevelopmentPHP ProblemHow to convert numbers to string type in php

How to convert numbers to string type in php

Dec 08, 2021 am 10:49 AM
phpstringnumberData type conversion

php method to convert numbers to strings: 1. Add the target type "(string)" enclosed in parentheses before the numeric variable to be converted, the syntax is "(string)$num"; 2 , use strval() function, syntax "strval($num)".

How to convert numbers to string type in php

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

php converts numbers For string type

#Method 1: Add the target type enclosed in parentheses "(string)"# before the numeric variable to be converted.

## You can coerce a specified variable to a specified type by preceding the variable with the target type enclosed in parentheses, whereas:

  • ( string): It is forced to convert to string type;

  • <?php
    header("Content-type:text/html;charset=utf-8");
    $num = 12354;
    echo &#39;原变量类型为:&#39; . gettype($num) . &#39;<br>&#39;;
    
    $str = (string)$num;
    echo &#39;转换后的变量类型为:&#39; . gettype($str) . &#39;<br><br>&#39;;
    
    $num = 123.54;
    echo &#39;原变量类型为:&#39; . gettype($num) . &#39;<br>&#39;;
    
    $str = (string)$num;
    echo &#39;转换后的变量类型为:&#39; . gettype($str) . &#39;<br><br>&#39;;
    ?>

How to convert numbers to string type in php

##Method 2: Use strval() function

strval() function is used to get the string value of a variable.

<?php
header("Content-type:text/html;charset=utf-8");
$num = 3.1415;
echo &#39;原变量类型为:&#39; . gettype($num) . &#39;<br>&#39;;

$str = strval($num);
echo &#39;转换后的变量类型为:&#39; . gettype($str) . &#39;<br><br>&#39;;

$num = 31415;
echo &#39;原变量类型为:&#39; . gettype($num) . &#39;<br>&#39;;

$str = strval($num);
echo &#39;转换后的变量类型为:&#39; . gettype($str) . &#39;<br><br>&#39;;
?>

How to convert numbers to string type in php Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to convert numbers to string type 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 Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft