- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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.
- Related Articles
- Which one should I use? The datetime or timestamp data type in MySQL?
- PHP $string{0} vs. $string[0];
- Which one should I learn: Python or Scala?
- Which datatype should I use for flag in MySQL?
- Regular cast vs. static_cast vs. dynamic_cast in C++
- Regular cast vs. static_cast vs. dynamic_cast in C++ program
- JavaScript vs. PHP: Which is Easier?
- Why should I use Hubspot?
- How to use the CAST function in a MySQL SELECT statement?
- CAST function in Cassandra
- When Should I use Selenium Grid?
- String reverse vs reverse! function in Ruby
- Which equals operator (== vs ===) should be used in JavaScript?
- When should you use a class vs a struct in C++?
- Should I use , , or for SVG files?
