Home  >  Article  >  Backend Development  >  How to convert data type to string type in php

How to convert data type to string type in php

青灯夜游
青灯夜游Original
2022-03-16 19:44:257963browse

Conversion method: 1. Use strval() function, syntax "strval(data value)"; 2. Use settype() function, syntax "settype(data value, "string")"; 3. Use sprintf() function, syntax "sprintf(formatting mode, data value)".

How to convert data type to string type in php

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

php will change the data type Convert to string type

#Method 1: Use strval() function

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

<?php
$num=3.21;
var_dump($num);
$str=strval($num);
var_dump($str);
?>

How to convert data type to string type 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 = 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 data type to string type in php

The value of $type can be:

  • "boolean" (or "bool" from PHP since 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 disabled)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

Method 3: Use the sprintf() function

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

<?php
$num=12;
var_dump($num);
$str1=sprintf("%.1f",$num);
var_dump($str1);
$str2=sprintf("%.2f",$num);
var_dump($str2);
$str3=sprintf("%.3f",$num);
var_dump($str3);
$str4=sprintf("%.4f",$num);
var_dump($str4);
?>

How to convert data type to string type in php

Description: sprintf() function

sprintf(format,arg1,arg2,arg++)
Parameters Description
format Required. Specifies a string and how to format variables within it.

Possible format values:

  • %% - returns a percent sign %
  • %b - a binary number
  • %c - the ASCII value corresponding Characters
  • %d - Decimal number with sign (negative, 0, positive)
  • %e - Scientific notation using lowercase (e.g. 1.2e 2)
  • %E - Use uppercase scientific notation (e.g. 1.2E 2)
  • %u - Decimal number without sign (greater than or equal to 0)
  • %f - Floating point number (local setting)
  • %F - floating point number (non-local setting)
  • %g - shorter %e and %f
  • %G - shorter %E and %f
  • %o - octal number
  • %s - string
  • %x - hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (capital letters)

Additional format value. Must be placed between % and a letter (such as %.2f):

  • (Add or - in front of the number to define the sign of the number. By default, only negative numbers are marked, Positive numbers are not marked)
  • ' (Specifies what to use as padding, the default is spaces. It must be used with the width specifier. For example: %'x20s (use "x" as padding))
  • - (Left-adjusted variable value)
  • [0-9] (Specifies the minimum width of the variable value)
  • .[0-9] (Specifies the number of decimal places or the maximum string Length)

Note: If multiple above format values ​​are used, they must be used in the order above and cannot be disrupted.

arg1 Required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2 Optional. Specifies the parameter to be inserted into the format string at the second % sign.
arg Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.

Recommended study: "PHP Video Tutorial"

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