How to use strval() function in PHP?

醉折花枝作酒筹
Release: 2023-03-10 11:22:02
forward
2136 people have browsed it

This article will introduce to you how to use the strval() function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use strval() function in PHP?

The strval() function is a built-in function in PHP that is used to convert any scalar value (string, integer or double) to a string. We cannot use strval() on arrays or objects, this function only returns the type name of the value to be converted if it is applied.

Syntax:

strval( $variable )
Copy after login

Parameters: This function accepts a single parameter $variable. This parameter represents the value we want to convert to a string

Return value: This function returns a string. The string is generated by typecasting the value of the variable passed to it as a parameter.

The following program illustrates the strval() function.

Program 1:

<?php
  
$var_name = 32.360;
  
// prints the value of above variable 
// as a string
echo strval ( $var_name );
  
?>
Copy after login

The output is as follows:

32.36
Copy after login

Program 2:

<?php
  
class lsbin
{
     public function __toString()
     {   
         // returns the class name 
         return __CLASS__ ;
     }
}
  
// prints the name of above class as
// a string
echo strval ( new lsbin);
  
?>
Copy after login

The output is as follows:

lsbin
Copy after login

Program 3:

<?php
// Program to illustrate the strval() function
// when an array is passed as parameter
  
// Input array
$arr = array (1, 2, 3, 4, 5);
  
// This will print only the type of value
// being converted i.e. &#39;Array&#39;
echo strval ( $arr );
  
?>
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to use strval() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!