0

I'm new to PHP and I tried to print array values vertically but it shows values horizontally. This is the code that I tried. Please help me, guys.

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){
        
}
        $output.= $roomsCount[$i];
}

echo $output;

Current output is 120122.

But I want it as

120
122

Really waiting for your help.

  • 2
    You can use \n to break line. `$output.= $roomsCount[$i] . " \n"; ` – Nícolas Sims Botelho Oct 24 '20 at 20:08
  • 1
    @NícolasSimsBotelho has you on the right tack, though it might be better to use `$output.= $roomsCount[$i] . PHP_EOL;`, or, if you're looking to output this in a browser, you might want `$output.= $roomsCount[$i] . '
    ';` What you use might depend on context, but the main point is that you need to tell your program to output each value on a new line.
    – Wesley Smith Oct 24 '20 at 20:17
  • 1
    I agree with you @WesleySmith, thank you. – Nícolas Sims Botelho Oct 24 '20 at 21:27
  • Thank you for the help. I want to display it on the browser. This works. $output.= $roomsCount[$i] . '
    ';
    – Nipun Tharuka Oct 29 '20 at 11:45

4 Answers4

1

You need to add break line to your code. there is some ways which i know and you have to use them on where needed for example using <br> is ok for html but might not gonna work if you are not using php in html codes.

For using in html try this edited code :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){
        
}
        $output.= $roomsCount[$i]."<br>";
        // edited over here // ."<br>" added to your code
}

echo $output;

For using in JavaScript or jQuery string returns or console.log or other things you can use this code too :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){

}
        $output.= $roomsCount[$i]."\n";
        // edited over here // ."\n" added to your code
}

echo $output;

Also if you are trying to save it on database or using on any API you might need to use this code PHP_EOL like this :

$roomCount = count($_POST["room_nos"]);
    for($i=0;$i<$roomCount;$i++) {
        foreach($roomsCount as $value){

}
        $output.= $roomsCount[$i].PHP_EOL;
        // edited over here // .PHP_EOL added to your code
}

echo $output;

I not sure its all things which you can use but its all codes which i knew i hope its resolve your problem.

1

You can use \n to break line. $output.= $roomsCount[$i] . " \n"; Or as @WesleySmith said: it might be better to use $output.= $roomsCount[$i] . PHP_EOL;, or, if you're looking to output this in a browser, you might want $output.= $roomsCount[$i] . '<br>';

0

insert a line break :

    $output.= sprintf ("%d<br>",$roomsCount[$i]); 
Maatoug Omar
  • 173
  • 1
  • 7
-1

you can also append <pre></pre> html tag to print horizontally in php.

 $output .=  '<pre>' . $output .'</pre>';
Al-Amin
  • 596
  • 3
  • 16