How does PHP convert numbers into strings

Two conversion methods: 1. Add the target type “(string)” in parentheses before the data, and the syntax “(string) specifies the data”. 2. Use the cast cast function strval() or settype() with the syntax “strval(specify data)” or “settype(specify data 2,”string”)”.

PHP forces data to a string type

Method 1: Prefix the data with the target type “(string)” in parentheses

<?php
header('content-type:text/html;charset=utf-8');   
$n=123;
var_dump($n);
var_dump((string)$n);
echo "<hr>";
 
$n=true;
var_dump($n);
var_dump((string)$n);
echo "<hr>";
 
$n=null;
var_dump($n);
var_dump((string)$n);
echo "<hr>";
?>

Method 2: Use a cast function

Strval () : The string value used to get the variable.

Settype () : Used to set a variable to the specified type(the setType () function changes the original type of the variable).

<!--?</span--> php
header("Content-type:text/html; charset=utf-8");
$n1 = 146;
var_dump($n1);
$str = strval($n1);
The echo 'variable $n1 has the following type:'.getType ($STR).'
';
echo "
<hr>";
$n2= FALSE;
var_dump($n2);
$str = settype($n2,"string");
The echo 'variable $n2 has the type:'.getType ($n2);
? >

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish