How to take the whole result of a for, and make it become a single variable

Asked

Viewed 45 times

1

Let’s say I have one for! And that the result of the is is " 1 2 3 4 5 6", only each number is a line!!

How to take all these lines and make it become a single variable?

  • Can transform into an array with var[] = $i or concatenate as a string using $var .= $i

  • Not wanting to ask too much!! Could you show me an example?

  • u want a string? or array?

  • What you find most viable.

  • Is that it? I don’t know what’s feasible in your case, generating an array from the same array doesn’t make much sense, just like it’s in the question.

  • My case was more curious!! To see how empty!! Because I had no notion!! With your reply I had a better idea and I was able to do what I wanted! Thank you

  • You have other options: http://answall.com/q/88228/91

Show 2 more comments

1 answer

2


You can do this through a string, concatenating the values with .= or the numbers all come together.

<?php

$arr = range(1,6);
$str = '';
foreach($arr as $item){
    $str .= ' '.$item;
}

echo $str;

Example - ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.