Domanda

I got this code, and I want 2 returns for it, how is correct way to make it? (PHP)

function two_tables($atts, $content = null) {

        return '<div id="table1"><table class="table table-bordered">'.$content.'</table></div>';

        return '<div id="table2"><table class="table table-bordered">'.$content.'</table></div>';
}

Nessuna soluzione corretta

Altri suggerimenti

In short, no, you can't return twice, the function exits after the first one.

However, this may do what you want:

function two_tables($atts, $content = null) {
    return '<div id="table1"><table class="table table-bordered">'.$content.'</table></div>
    <div id="table2"><table class="table table-bordered">'.$content.'</table></div>';
}

This is the two outputs combined together.

If You want to be able to use them both seperately, you could always return an array of both items.

function two_tables($atts, $content = null) {

    $one = '<div id="table1"><table class="table table-bordered">'.$content.'</table></div>';

    $two = '<div id="table2"><table class="table table-bordered">'.$content.'</table></div>';
    return array($one, $two);
}

This way when you receive the function, you could do this.

$result = two_tables($blah, $blah);
echo $result[0]; //the first return..
echo $result[1]; //the second return

Apart from Alex's answer, if you want to keep those separate, you can return them in an array.

return array(
    '<div id="table1"><table class="table table-bordered">'.$content.'</table></div>', 
    '<div id="table2"><table class="table table-bordered">'.$content.'</table></div>'
);

This function will always return first line of HTML.

For returning both s :

function two_tables($atts, $content = null) {
        return '<div id="table1"><table class="table table-bordered">'.$content.'</table></div><div id="table2"><table class="table table-bordered">'.$content.'</table></div>';
}

In order to return more than one element, you simply need to return an array, like so:

return array(
    '<div id="table1"><table class="table table-bordered">'.$content.'</table></div>', 
    '<div id="table2"><table class="table table-bordered">'.$content.'</table></div>'
);

If you are wanting the elements to be specifically referenced upon their return, as opposed to being in a simple array, you can request them when calling the function, like so;

list($table1, $table2) = two_tables($atts, $content = null);

Then to use them, you would simply call:

echo $table1;

// some other code here

echo $table2;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top