Site Admin
Site Admin Founder of MeaningArticles

PHP Convert Variable to String

Hello Dev.

PHP provides lots of pre-built function to deal with string conversion, we are going to use __toString() method to convert variable to string. Let us check out the example

In the example, we have used the __toString() function to convert variable to string. This function doesn’t take any argument, and it is mostly used with Objects.


class convertToString{
    public $str;

    public function __construct($str) {
        $this->str = $str;

    public function __toString() {
      return (string) $this->str;

$myObj = new convertToString('Days Gone');
echo $myObj;


// Result: Days Gone

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.