How To Convert Array To String In PHP
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of how to convert array to string in, array always contain more number of values when we do conversion it will became make it as single line string.
PHP provides more function for that conversion like string buffering method, implode or encode method.
We can use any one for make that conversion result will be same and here we used implode method.
Step By Step Guide On How To Convert Array To String In PHP :-
For converting an array into string we used implode method. The implode() function returns a string from the elements of an array and this function accepts its parameters in either order.
User defined array has number values using implode() in-built method its altered to without space all values are joined together and displayed on browser as a single string.
Result will display on plain webpage because we are not styled.
<?php $arr=array("hi","welcome","greetings"); echo implode("",$arr); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?> tags.
- The default file extension for php files is “.php” and php each statements end with ‘;’ semicolon. Between start and end tag we need to define our program in php.
- We declared one array with three values that array variable name is ‘$arr’ and here for declare one variable we need to use ‘$’ symbol before variable name.
- Those array values are ‘"hi","welcome","greetings"’ and before values ‘array’ mentioned because it denotes as array values.
- In array we can define with any number of values, those are our choice not mandatory to use same as above used values and result needs to have all described values in array as a string.
- In case your defined values in array within double quotes is integer (decimal) values those are also treated as string value in array.
- In php ‘echo’ used for printing on webpage any values, string or text, like anything. Implode() method has two parameters first one is denotes empty string its optional so we can leave.
- Second one is denotes array variable so we used ‘$arr’ after conversion result will display on webpage.
- In-built method called explode() available it will returns string into array value. It is same as implode() but the result is reverse.
In conclusion we are able to know how to convert array to string using php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program.
When executing this program on browser page, we declared array ‘$arr’ with three separated ("hi","welcome","greetings") values joined together even without space between each values displayed on webpage.
For recursive result use explode () method and for same result use above mentioned two other functions at intro explanation.
I hope this tutorial on how to convert array to string in PHP helps you and the steps and method mentioned above are easy to follow and implement.