0

I want to output my code. When I use return then it breaks up after 1time and

(when I use echo my content will be displayed on the top of the page. And I think that I should not use here echo because I want to call functions in the loop later)

Here are pictures that will show how it looks like:

My code with return:

function allmyshortcodesloopfunction() {
    $alltheshortcodes = '';
    $alltheshortcodes = 'thistextshouldbehere4times';

    for($i=0; $i < 4; $i++) {
        echo '<p></p>';
        return $alltheshortcodes;
    }
}

How this looks: enter image description here

how it looks with echo: enter image description here

Maybe you can post a Thread which I could not find to solve this problem. Some Tags for People in the future with this problem. That they can find this: loop, while, if, return, statement, content, breaks up, just one time, only 1 time

0

2 Answers 2

1

Return exits the function, you can't have it in the loop. Store it separately and then reuse the function.

function allmyshortcodesloopfunction() {
    $output = '';
    $alltheshortcodes = 'thistextshouldbehere4times';

    for ($i=0; $i < 4; $i++) {
        $output .= "<p>$alltheshortcodes</p>";
    }

    return $output;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. i have made a bit different : function allmyshortcodesloopfunction() { $alltheshortcodes = ''; $alltheshortcodes = 'thistextshouldbehere4times'; for($i=0; $i < 4; $i++) { $output .= '<p>hi</p>'; } return $output; }
0
function allmyshortcodesloopfunction() {
    $alltheshortcodes = 'thistextshouldbehere4times';
    for ($i = 0; $i < 4; $i++) {
        echo "<p>$alltheshortcodes</p>";
    }
}
allmyshortcodesloopfunction();

1 Comment

Code-only answers are low value on stackoverflow because they do very little to educate/empower thousands of future researchers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.