How to convert object or array to string in PHP?

The conversion of object or array to string is possible with the built-in PHP functions or typecasting. But here a question arises why do we need to convert object or array to string in PHP. We do really need it because the transfer of data is possible when the data is in the form of text or string and not in the form of objects or arrays. So We must convert the array or object to a string before retrieving the data with other PHP files.

For example, when we make AJAX requests in JS to retrieve data with other PHP files to update parts of the page without reloading. We need to convert the objects or arrays to string in JS before sending and then back to arrays or objects in PHP on receiving.

We can either serialize the object or convert an array to string. We'll learn how to convert an array or object to string with json_encode() and serialize() functions, respectively.

1. Convert array to string

json_encode() function

json_encode() function accepts the array as an argument and converts it into JSON format. Since, It is not possible print the whole array in PHP. That's why we have to convert this array into string with json_encode() function.

Example 1:

Suppose we have an indexed array in PHP and we want to print the whole array. But it is not possible to print the whole array without using the json_encode() function. As we have discussed that it converts the array into the string which is printable or transferable.

 $arr = ["HTML","CSS","JavaScript", "JQuery","PHP"];
 
 echo $arr; // It gives an error of array to string conversion
 
 echo json_encode($arr); // ["HTML","CSS","JavaScript", "JQuery","PHP"]

See the above-given example. It gives an error when we try to echo the array. But we can print the whole array by converting it into the string (JSON format) with json_encode method.

Example 2:

Now we can take an example of the associative array. The same is the case with the associative arrays. We must convert it into a string to print.

 $arr = ["name"=>,"web4college","email"=>"[email protected]", "password"=>"12@$4"];
 
 echo $arr; // It gives an error of array to string conversion
 
 echo json_encode($arr); // {"name":"web4college","email":"[email protected]","password":"12@$4"}

2. Convert object to string

In this case, we have to serialize the object. And serialize() function converts that object into string.

serialize() function

This function accepts an object as an argument and converts it into a string. We can directly convert an object into a string or make a function in a class to serialize any newly created object.

Example 3:

See the following example. We are explicitly converting objects into a string.

class User{

 protected $name = '';
 protected $email = '';
 protected $password = '';
 
 public function __construct($name, $email, $password)
 {
	 $this->name = $name;
	 $this->email = $email;
	 $this->password = $password;
 }
  
}

$std = new User("web4college","[email protected]","12@$4");

echo (serialize($std)); // O:1:"User":3:{s:7:"*name";s:11:"web4college";s:8:"*email";s:17:"[email protected]";s:11:"*password";s:5:"12@$4";}

In the above-given PHP code, serialize() function converts the $std object into string.

Example 4:

And if you want to convert every newly created object of User class into string automatically. Just make a function in the class to convert any object into a string. See the following example to understand.

class User{

 protected $name = '';
 protected $email = '';
 protected $password = '';
 
 public function __construct($name, $email, $password)
 {
	 $this->name = $name;
	 $this->email = $email;
	 $this->password = $password;
 }
 public function __String()
 {
	return serialize($this);
 }
  
}

$std = new User("web4college","[email protected]","12@$4");

echo ($std); // O:1:"User":3:{s:7:"*name";s:11:"web4college";s:8:"*email";s:17:"[email protected]";s:11:"*password";s:5:"12@$4";}

See the above-given PHP code, we have a function __toString that automatically returns the newly created object as the serialized object.

Summary

You must use the PHP json_encode() function to convert array to string which is transferable. And PHP serialize() function converts the object into a string which is also transferable.


PHP

How to split string into array in php

PHP implode() function to concatenate the associative array values with multiple delimiters


Was this article helpful?