Reza Saadati
Reza Saadati

Reputation: 5419

How to use return instead of multiple echos in a function?

I have a function that contains 3 echos. What I want is just a single return. How can I rebuild my function, so that I can replace those echos with a return?

Especially the foreach part seems to me to be an issue.

function display_customer_table() {
    echo '
        <div class="table-responsive">
            <form method="post">
                <table class="table table-hover table-striped yc_table">
                    <tr>
                        <th>#</th>
                        <th>Anrede</th>
                        <th>Name</th>
                        <th>E-Mail</th>
                        <th>Website</th>
                        <th>Datum</th>
                        <th>Kontaktiert von</th>
                        <th>Angerufen?</th>
                    </tr>';
            foreach ($get_uncontacted_members as $get_uncontacted_member) {
            $timestamp = strtotime($get_uncontacted_member->date);
                echo '
                    <tr>
                        <td>' . $num++ . '</td>
                        <td>' . $get_uncontacted_member->anrede . '</td>
                        <td>' . $get_uncontacted_member->receiver . '</td>
                        <td><a target="_blank" href="mailto:'. $get_uncontacted_member->email .'">' . $get_uncontacted_member->email . '</a></td>
                        <td><a target="_blank" href="http://'. $get_uncontacted_member->website .'">' . $get_uncontacted_member->website . '</a></td>
                        <td>' . date("d.m.Y, H:i", $timestamp) . '</td>
                        <td>' . $get_uncontacted_member->mitarbeiter . '</td>
                        <td><i title="Zielperson wurde telefonisch kontaktiert" id="' . $get_uncontacted_member->id . '" class="fa fa-phone-square called"></i></td>
                    </tr>';
                }
echo '          </table>
            </form>
        </div>';
}

Upvotes: 0

Views: 45

Answers (2)

sockfd2
sockfd2

Reputation: 545

Try this:

function display_customer_table() {
    $output = '
        <div class="table-responsive">
            <form method="post">
                <table class="table table-hover table-striped yc_table">
                    <tr>
                        <th>#</th>
                        <th>Anrede</th>
                        <th>Name</th>
                        <th>E-Mail</th>
                        <th>Website</th>
                        <th>Datum</th>
                        <th>Kontaktiert von</th>
                        <th>Angerufen?</th>
                    </tr>';
            foreach ($get_uncontacted_members as $get_uncontacted_member) {
            $timestamp = strtotime($get_uncontacted_member->date);
                $output .= '
                    <tr>
                        <td>' . $num++ . '</td>
                        <td>' . $get_uncontacted_member->anrede . '</td>
                        <td>' . $get_uncontacted_member->receiver . '</td>
                        <td><a target="_blank" href="mailto:'. $get_uncontacted_member->email .'">' . $get_uncontacted_member->email . '</a></td>
                        <td><a target="_blank" href="http://'. $get_uncontacted_member->website .'">' . $get_uncontacted_member->website . '</a></td>
                        <td>' . date("d.m.Y, H:i", $timestamp) . '</td>
                        <td>' . $get_uncontacted_member->mitarbeiter . '</td>
                        <td><i title="Zielperson wurde telefonisch kontaktiert" id="' . $get_uncontacted_member->id . '" class="fa fa-phone-square called"></i></td>
                    </tr>';
                }
$output .= '          </table>
            </form>
        </div>';
return $output;
}

Also note that your code is not very neat, that is, because you keep concatenating strings with variables. Have a look at the function sprintf.

Upvotes: 2

Richard
Richard

Reputation: 2815

Set first an empty variable:

$return = "";

Instead of echo, you just say

$return .=

Then you can (at the end of the function) say:

return $return;

Example:

echo '          </table>
        </form>
    </div>';

Go to:

$return .= '          </table>
        </form>
    </div>';

Upvotes: 2

Related Questions