How to call functions from an array (randomly or sequentially) in PHP

Call all functions sequentially-

<?php


$all_functions = ["func_one","func_two","func_three"];
for($j=0;$j<count($all_functions);$j++)
{
  $all_functions[$j]();
}

function func_one()
{
  echo "first function <br>";
}

function func_two()
{
  echo "second function <br>";
}

function func_three()
{
  echo "third function <br>";
}

?>

Call only one random function

<?php


$all_functions = ["func_one","func_two","func_three"];

$all_functions[array_rand($all_functions)]();

function func_one()
{
  echo "first function";
}

function func_two()
{
  echo "second function";
}

function func_three()
{
  echo "third function";
}

?>

Leave a Comment