Asked 3 years ago
30 Jul 2021
Views 930
Jaunita

Jaunita posted

How to cast array elements to strings in PHP?

How to cast array elements to strings in PHP?
Melyna

Melyna
answered Aug 22 '21 17:15

implode() function is can convert the array to string in PHP.


$array=array("array",,"is""string","now");
echo implode(" ",$array); // it print array is string now


implode("first argument is glue","second argument must be array ")
so in above we glued the array value with one space and make a string in returns
Melyna

Melyna
answered Aug 22 '21 17:15

join() function is can convert the array to string in PHP.


$a=array("a",,"b""c","d");
echo join(" ",$array); 


join("first argument is glue","second argument must be array ")

so in above we glued the array value with one space and make a string in returns with join() function in PHP

join() function is alias of the implode() function in PHP
Melyna

Melyna
answered Aug 22 '21 16:59

json_encode() function will encode array to JSON, JSON is one type of string which formatted so it can convert JSON string to array again.

$response=array("stauts"=>"1");
echo json_encode($response);


JSON format is used to information exchange between server to server or server to client
Server Ajax response will be sent back to the browser in the format of JSON
Post Answer