Using functions is probably a better choice anyway. However, note that it is possible to use aliases if you set the expand_aliases
option:
<?php
$code = 'ls';
$aliases = '
shopt -s expand_aliases
alias ls="ls -l"';
$code = $aliases . "\n" . $code;
exec('bash -c ' . escapeshellarg($code), $result);
echo implode("\n", $result) . "\n";
Output:
$ php aliasexec.php
total 12
-rw-rw-rw- 1 mlk mlk 198 Feb 18 11:18 aliasexec.php
This is what the man page has to say (emphasis mine):
Aliases are not expanded when the shell is not interactive, unless
the expand_aliases shell option is set using shopt […].
The rules concerning the definition and use of aliases are somewhat
confusing. Bash always reads at least one complete line of input
before executing any of the commands on that line. Aliases are
expanded when a command is read, not when it is executed. Therefore,
an alias definition appearing on the same line as another command
does not take effect until the next line of input is read. […] To
be safe, always put alias definitions on a separate line, and do not
use alias in compound commands.
For almost every purpose, aliases are superseded by shell functions.
That is why you must use newlines and not the ;
character to define the alias.