You should pay attention to this article in the documentation:
Accessing functions via variables
Here is an example implementation:
$rand = rand(1, 2); function test_1 () { return 'test 1'; } function test_2 () { return 'test 2'; } $random_function = "test_{$rand}"; echo $random_function();
Eugene Moskvin, unfortunately, from your question it is not possible for me to understand what task you were faced with initially, and therefore I can only answer the question posed. No more, no less.
Please note that you may have chosen far from the most optimal way to solve the task before you. I suggest you stop and think about everything again before using this solution.
Solution after updating the issue
// Храните данные о результатах матча в массиве. // Так вы сократите число переменных. // Обратите внимание, что у меня очки целочисленные. // Раз над ними будет проводиться операция сравнения, это логично. $match = array( 'home' => array( 'name' => 'Зайцы', 'score' => 3 ), 'guest' => array( 'name' => 'ТрынТрава', 'score' => 0 ) ); // Эта функция возвращает случайный элемент массива. // Замечания по работе штатной функции array_rand почитайте в документации: // http://php.net/manual/ru/function.array-rand.php function array_random($array) { $random_key = mt_rand(0, count($array) - 1); return $array[$random_key]; } // Эта функция возвращает текстовый результат сравнения очков команд function analyze_score_difference($score_a, $score_b) { // Разница по очкам высчитывается по модулю $score_difference = abs($score_a - $score_b); // Разница равна 0? Команды сыграли вничью. if ($score_difference === 0) { return 'equal'; } // Разница равна 1? Команды сыграли с минимальной разницей. if ($score_difference === 1) { return 'minimum-difference'; } // Обычный матч return 'default'; } // Функция возвращает случайное сообщение для результатов матча function get_match_status_message($match) { $messages = array( // Ничья 'equal' => array( // Прямой порядок Хозяин - Гость '%s дома сыграл вничью с %s', // Обратный порядок Гость - Хозяин '%2$s на выезде сыграл вничью с %1$s' ), // Минимальная разница 'minimum-difference' => array( '%s дома с минимальным преимуществом переиграл %s', '%2$s на выезде минимально проиграл %1$s' ), // Обычный матч 'default' => array( '%s дома переиграл %s', '%2$s на выезде проиграл %1$s' ) ); // Анализируем результаты матча $match_result = analyze_score_difference( $match['home']['score'], $match['guest']['score'] ); // Возвращаем случайное сообщение для результатов матча // Документация по sprintf // http://php.net/manual/ru/function.sprintf.php // Обратите внимание на: "Пример #4 Изменение порядка параметров" return sprintf( array_random($messages[$match_result]), $match['home']['name'], $match['guest']['name'] ); } // Вывод результатов матча echo get_match_status_message($match);
Please note that there are no checks for variables or the existence of keys in an array. In my example, only the most basic functionality. Redo it for yourself.
View an example of work
$function_name = "test_{$_POST['malware']}"; echo $function_name();$function_name = "test_{$_POST['malware']}"; echo $function_name();- romeoвыглядит извращение крайней степениExactly. It is worth thinking how to solve your problem in another way. The names of the functions, and for OOP methods and properties of the object should be specified explicitly, and not with the help of calculations (your case), and especially the substitution of variables with data from the outside. - romeo