PHP equivalent of Java’s toString() method
This article demonstrates the PHP equivalent of Java’s toString() method.
1. Using Typecasting or strval() function
You can use type casting to get the string representation of the scalar types (int, float, string, or bool). To cast a variable to a string, you can enclose the string type within parentheses before the variable.
The following code demonstrates the type conversion from scalar types (and null) to strings. It is worth noting that the boolean values true and false are converted to the "1" and "" (the empty string), respectively. The null value, when cast to a string, results in an empty string. The program uses var_dump() to display the type and value of the converted variable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php /* integer */ var_dump((string) 10); // string(2) "10" var_dump((binary) 10); // string(2) "10" /* boolean */ var_dump((string) true); // string(1) "1" var_dump((string) false); // string(0) "" /* floats */ var_dump((string) 0.0); // string(1) "0" var_dump((string) 3.14); // string(4) "3.14" /* null */ var_dump((string) NULL); // string(0) "" ?> |
Currently, (binary) is an alias for the (string) cast. However, this should be used with caution, as its behavior may change in the future. It is also possible to enclose the variable in double quotes, rather than casting it to a string. This technique is called variable interpolation in PHP, resulting in compact, readable, and cleaner code. For example,
|
1 2 3 4 5 6 7 8 9 10 |
<?php $var = 10; $string = "$var"; var_dump($string); /* Output: string(2) "10" */ ?> |
Alternatively, you can use the strval() function to convert a value to a string, which works in a similar way as the (string) cast.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php /* integer */ var_dump(strval(10)); // string(2) "10" /* boolean */ var_dump(strval(true)); // string(1) "1" var_dump(strval(false)); // string(0) "" /* floats */ var_dump(strval(0.0)); // string(1) "0" var_dump(strval(3.14)); // string(4) "3.14" /* null */ var_dump(strval(NULL)); // string(0) "" ?> |
2. Using __toString() Magic Method
Passing an array to the strval() function will result in the PHP warning ‘Array to string conversion’. Objects, on the other hand, produce the fatal error message ‘Object of class classname could not be converted to string’. To convert objects to strings, you can have the class override the magic method __toString() and return a value capturing all fields of the object. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php class MyClass { public $val; public function __construct($val) { $this->val = $val; } public function __toString() { return $this->val; } } $object = new MyClass(3.14); var_dump((string)$object); // string(4) "3.14" var_dump(strval($object)); // string(4) "3.14" var_dump("$object"); // string(4) "3.14" ?> |
3. Using print_r() function
The print_r() function prints human-readable information about a variable. It can return a string instead of outputting it when the second parameter is set to true. The print_r() function will also work with objects and arrays, and their string representation contains values presented in a format showing keys and elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php class MyClass { public $val; public function __construct($val) { $this->val = $val; } public function __toString() { return $this->val; } } ?> <?php /* integer */ var_dump(print_r(10, true)); // string(2) "10" /* boolean */ var_dump(print_r(true, true)); // string(1) "1" var_dump(print_r(false, true)); // string(0) "" /* floats */ var_dump(print_r(0.0, true)); // string(1) "0" var_dump(print_r(3.14, true)); // string(4) "3.14" /* null */ var_dump(print_r(NULL, true)); // string(0) "" /* array */ $array = ['Germany' => 'Berlin', 'UK' => 'London', 'France' => 'Paris']; $string = print_r($array, true); var_dump($string); /* string(75) "Array ( [Germany] => Berlin [UK] => London [France] => Paris ) " */ /* object */ $object = new MyClass(3.14); $string = print_r($object, true); var_dump($string); /* string(37) "MyClass Object ( [val] => 3.14 ) " */ ?> |
4. Using var_export() function
Finally, you can use the var_export() function to output the parsable string representation of a scalar variable, an array, or an object. To return the string representation instead, you can set the second parameter of var_export() to true.
Note that unlike typecasting, strval(), and print_r() function, the var_export() function converts the boolean values true and false to their string equivalent "true" and "false" respectively. Similary, the null values gets converted to string "NULL".
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php class MyClass { public $val; public function __construct($val) { $this->val = $val; } public function __toString() { return $this->val; } } ?> <?php /* integer */ var_dump(var_export(10, true)); // string(2) "10" /* boolean */ var_dump(var_export(true, true)); // string(4) "true" var_dump(var_export(false, true)); // string(5) "false" /* floats */ var_dump(var_export(0.0, true)); // string(3) "0.0" var_dump(var_export(3.14, true)); // string(4) "3.14" /* null */ var_dump(var_export(NULL, true)); // string(4) "NULL" /* array */ $array = ['Germany' => 'Berlin', 'UK' => 'London', 'France' => 'Paris']; $string = var_export($array, true); var_dump($string); /* string(77) "array ( 'Germany' => 'Berlin', 'UK' => 'London', 'France' => 'Paris', )" */ /* object */ $object = new MyClass(3.14); $string = var_export($object, true); var_dump($string); /* string(48) "MyClass::__set_state(array( 'val' => 3.14, ))" */ ?> |
Also See:
That’s all about PHP the equivalent of Java’s toString() method.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)