Home  >  Article  >  Backend Development  >  How to force conversion to string in PHP

How to force conversion to string in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-09-01 15:00:177014browse

Methods to force conversion into strings in PHP: 1. Add "(string)" before the variable to be converted, for example "(string)3.14"; 2. Use the strval() function, for example " strval(3.14)".

How to force conversion to string in PHP

[Related learning recommendations: php graphic tutorial]

How to force conversion into a string in PHP:

PHP’s data type conversion is a forced conversion, and the conversion type is (string): converted to character String

Conversion methods: intval() floatval() strval() three types.

<?php   
$str="123.9abc";   
$int=intval($str);     //转换后数值:123   
$float=floatval($str); //转换后数值:123.9   
$str=strval($float);   //转换后字符串:"123.9"    
?>

The PHP data types that allow conversion are:

  • (int), (integer): converted to integer

  • (float), (double), (real): Convert to floating point type

  • (string): Convert to string

  • ( bool), (boolean): Convert to Boolean type

  • (array): Convert to array

  • (object): Convert to object

There are three conversion methods for PHP data types:

Add the target type enclosed in parentheses before the variable to be converted.

Use three specific types of conversion functions, intval(), floatval(), strval() [Memory: the destination type you want to convert val()].

Use the general type conversion function settype(mixed var, string type).

Related learning recommendations: php programming (video)

The above is the detailed content of How to force conversion to 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