I have the code below:
$xxx = shell_exec('sudo chown -R user:user "/temp123" 2>&1');
It works great, I mean, the user/group is set correctly.
But the problem is that when I echo
the variable $xxx
it looks empty but it's not.
For example, the code:
echo '|' . $xxx . '|';
Returns ||
. Also the code:
echo strlen($xxx);
Returns 0
. However if I try this code:
if ($temp1122 !== '') {
echo 'is not empty';
}
It always returns is not empty
. I tried echoing $xxx[0]
but PHP returns error saying there is no char at that index. So what is going on? I think some control char is being returned by shell_exec
but it should change strlen
or even [0]
right?
shell_exec()
returns null
if the command produces no output. chown
usually doesn't print anything when it works, so that's normal.
When you echo
it, PHP cast null
into an empty string ''
, so you just see ||
.
But keep in mind: null !== ''
, they're not the same. If you're not sure, just do a var_dump($xxx)
and you'll see it.
See the official docs: shell_exec return values