3

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);

5
  • Does this really work? Even if it does, I would use a second variable which would save the imploded array. After that I would assign this string back to the first variable. So the second one is only a helper-variable. Commented Jul 30, 2010 at 11:22
  • @faileN why wouldn't it? Commented Jul 30, 2010 at 11:24
  • Don't know, thought implode would work per reference or something, so that this would go terribly wrong. But if it works okay, But I'd never do it this way :) . Thanks Commented Jul 30, 2010 at 11:25
  • @faileN: PHP's documentation is quite good, and implode()'s page doesn't mention anything about references; also, it actually works like that. See uk.php.net/manual/en/function.implode.php Commented Aug 3, 2010 at 14:31
  • I know where to find the docs, believe me I'm using it since 8 years. But in fact I was to lazy to check this particular issue. So Znarkus already got the fast answer for me. Thanx Commented Aug 3, 2010 at 19:46

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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.

Comments

2

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

1 Comment

@user406659: "never" is a long time. I've seen (and sometimes written) a lot of code which originaly assumed "that'll never happen".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.