Thursday, 30 August 2018

The php function does not return the string and the line breaks

Two issues with the below php function.

One problem with the below function is the function echoes the $boo and $boo2 variables where the function is (i.e. before 1 and 2 is echoed). Both functions are echoed before 1 and 2. I'm unsure how to return the variables where each function is called without prematurely ending the function with 'return'.
The $boo variables are single line breaks or paragraphs of text.
<?php
function listformat ($list) {
    $listformat = explode("\n", $list);
    echo '<ul>';
    foreach ($listformat as $line) {
        echo "<li>".$line."</li>";
    };
    echo '</ul>';
};
listformat($boo);
echo '1';
listformat($boo2);
echo '2';
?>

Also wondering how I can remove line breaks of the $boo variables within the function when the lines of text have more than 1 line break.
EDIT: sample $boo is:
aaaa aaaaa
bbb bbbb
cccc c cc
Sometimes it can have more or less line breaks in between.

function listformat ($list) {
    $listformat = explode("\n", $list);
    $out = '<ul>';
    foreach ($listformat as $line) {
        $out .= "<li>".$line."</li>";
    };
    $out .= '</ul>';
    return $out;
};

0 comments:

Post a Comment