

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Which one should I use? The datetime or timestamp data type in MySQL?
- Which datatype should I use for flag in MySQL?
- Why should I use Hubspot?
- Python Vs Ruby, which one to choose?
- Which equals operator (== vs ===) should be used in JavaScript?
- PHP $string{0} vs. $string[0];
- Regular cast vs. static_cast vs. dynamic_cast in C++
- When Should I use Selenium Grid?
- Which equals operator (== vs ===) should be used in JavaScript comparisons
- Regular cast vs. static_cast vs. dynamic_cast in C++ program
- Which datatype is should I use to set a column for 5-star rating?
- When should I use MySQL compressed protocol?
- Which one is more accurate in between time.clock() vs. time.time()?
- When should you use a class vs a struct in C++?
- How many Python classes should I put in one file?