![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP string cast vs strval function, which one should I use?
A value can be converted into a string with the help of (string) cast or the strval() function.
The strval() function is a function call whereas (string) cast is an internal type casting method.
Unless there is some specific dataset or use case, both of these can be used interchangeably.
This is because PHP uses automatic type conversion, due to which a variable's type is determined based on the context in which it is used.
The strval($var) function returns the string value of $var whereas the (string)$var explicitly converts the "type" of $var during the process of evaluation.
The $var can be any scalar type or an object that implements the __toString method.
The strval() can't be used on arrays or on objects that do not implement this __toString method.
In general, (string)cast is relatively quicker.