PHP

When to use call_user_func() and 'funcname'() in PHP?

There are 2 ways to call a function whose name is set at runtime in PHP. One is call_user_func() and another is 'funcname'().

<?php

$funcname = 'array_sum';

// call_user_func()
call_user_func($funcname, [1, 2, 3]);  // => 6

// 'funcname'()
$funcname([1, 2, 3]);  // => 6

At a glance these 2 methods are completely same but there are some important differences between them. Let's check them one by one.