mmdwc mmdwc - 1 year ago 70
PHP Question

Randomly switch between two PHP functions

I have 2 PHP functions on a webpage. I would like to run one of those 2 functions randomly. I know how to do it with

s or HTML using PHP, but not how to run a PHP function randomly. I want to use PHP and not Javascript.

Here are my 2 functions:

<?php get_template_part('module_sidebar_1'); ?>
<?php get_template_part('module_sidebar_2'); ?>

Answer Source

If you also have mixed (numeric + non-numeric) template names, Create an array with all template names like,

$templates = ['module_sidebar_1','module_sidebar_2','module_sidebar_non_numeric'];

and call random template file by

<?php get_template_part($templates[array_rand($templates)])?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download