Tutorial :PHP : imploding array and storing the result back to same variable



Question:

Are there any pit falls in the below code . Is it safe to use this way. I will not be using the array again

$records_msg = implode(" ",$records_msg);


Solution:1

php is dynamically-typed. there is nothing wrong with choosing brevity at the expense of clarity. you may want the data types of your variables to stay consistent throughout a function/method/class/routine. but nothing in the language prevents you from doing otherwise.


Solution:2

Not really, but using a different variable name for the array may improve readability, since it's not a message yet.


Solution:3

That might be confusing to anyone reading your code. First $records_msg is an Array, then further down the code it is a String.

I would probably rename the Array to $records_messages and the String to $records_message.


Solution:4

Another thing. If you have array in array, you'll lose it. Example:

<?php  $input = array(1,2,3,array(4,5));  echo implode(',', $input);  ?>  

returns:

PHP Notice:  Array to string conversion in C:\Temp\1.php on line 3  1,2,3,Array  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »