Which is the recommended approach?
Using include:
// subroutine.php
echo 'hello '.$a;
// usage.php
$a = 'foo';
include 'subroutine.php';
Using function:
// subroutine.php
function subroutine ($a)
{
echo 'hello '.$a
}
// usage.php
include 'subroutine.php';
$a = 'foo';
subroutine($a);
Since both technically work and since there is no “subroutines” in PHP unlike ASP for example. What is the best way to emulate subroutines?
A function is better suited for this purpose. Includes are more widely used for templating, or for when the controller calls a view.
However, your function should not echo the concatenated string. It should return it.
This makes your code testable.