You can do this with a loop consisting of two iterations. With an if/else containing each function calls as statements within the loop. A flag determines which function to run. After one iteration the flag is toggled, so next time the other function runs:
function a() {
echo 'a';
}
function b() {
echo 'b';
}
function c($flip = false) {
for($i=0; $i<2; $i++) {
if(! $flip) {
a();
} else {
b();
}
$flip = ! $flip; // On next run the other statement will run
}
}
c($flip = false);
c($flip = true);
// Outputs: abba
Or using the ternary operator:
function d($flip = false) {
for($i=0; $i<2; $i++, $flip=!$flip) {
!$flip ? a() : b();
}
}
In a html view:
<?php for($i=0; $i<2; $i++) { if(! $flip) { // Change the block display order if flip is true. ?>
<h2>Block Foo</h2>
<p>...</p>
<?php } else { ?>
<h2>Block Bar</h2>
<p>...</p>
<?php } $flip = !$flip; } ?>
The context being that I wrote a CMS module, and wanted a simple option for the end user to change the order of html, without overriding templates. I hope someone finds this useful.