Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | stat( ) function
Next article icon

PHP strval() Function

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The PHP strval() function converts a given variable to a string. It takes a scalar variable (like integers, floats, and booleans) and converts it to its string representation. For arrays, objects, and resources, it does not work and may produce unexpected results.

Syntax

strval( $variable ) 

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

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

The below programs illustrate the strval() function.

Example: In this example we use of the strval() function, which converts the float value 32.360 to a string and prints it. The function ensures the variable is treated as a string.

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

Output
32.36

Example : In this example we defines a class geeksforgeeks with a __toString() method that returns the class name. Using strval() on an instance of the class prints its name as a string.

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

Output
geeksforgeeks

Example : In this example we demonstrates the strval() function with an array as the parameter. Since strval() does not convert arrays to strings, it prints “Array,” indicating the type, rather than converting the array’s contents.

php
<?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. 'Array'     echo strval($arr);  ?> 

Output
Array


Next Article
PHP | stat( ) function

S

sid4321 , Shivani2609
Improve
Article Tags :
  • PHP
  • Web Technologies
  • PHP-string

Similar Reads

  • PHP | gmp_strval() Function
    The gmp_strval() is an inbuilt function in PHP which returns the string value of a GMP number. (GNU Multiple Precision: For large numbers). Syntax: string gmp_strval ( GMP $num, int $base ) Parameters: The function accepts two parameters $num and $base as shown above and described below. $num - The
    3 min read
  • PHP strlen() Function
    The strlen() is a built-in function in PHP which returns the length of a given string. It calculates the length of the string including all the whitespaces and special characters. Syntax:strlen($string);Parameters: This parameter represents the string whose length is needed to be returned. Return Va
    1 min read
  • PHP strrev() Function
    Reversing a string is one of the most basic string operations and is used very frequently by developers and programmers. PHP comes with a built-in function to reverse strings. The strrev() function is a built-in function in PHP and is used to reverse a string. This function does not make any change
    2 min read
  • PHP | stat( ) function
    The stat() function in PHP is an inbuilt function which is used to return information of a file. The stat(0) function returns statistics of a file which is an array with the following elements : [0] or [dev] - Device number [1] or [ino] - Inode number [2] or [mode] - Inode protection mode [3] or [nl
    4 min read
  • PHP strstr() Function
    The strstr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-sensitive. Syntax : strstr( $
    2 min read
  • PHP rand() Function
    In this article, we will see how to get the random number using the rand() function in PHP, along with knowing its implementation through the example. The rand() is an inbuilt function in PHP used to generate a random number ie., it can generate a random integer value in the range [min, max]. Syntax
    2 min read
  • PHP strnatcmp() function
    The strnatcmp() is a built-in function on PHP. This function compares two strings using a "natural order" algorithm and return a positive integer, negative or zero. This function is case-sensitive. Syntax: strnatcmp( $string1, $string2 ) Parameters: The functions accepts two mandatory string paramet
    2 min read
  • PHP prev() Function
    The prev() function is an inbuilt function in PHP. It is used to return the immediate previous element from an array of the element which is currently pointed by the internal pointer. We have already discussed current() function in PHP. The current() function is used to return the value of the eleme
    2 min read
  • PHP String Functions
    Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s
    6 min read
  • PHP stristr() Function
    The stristr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-insensitive. Syntax : strist
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences