Back ticks in PHP

in

Here’s a fun little PHP fact, Did you know, text enclosed within back ticks ( ` ) is executed within the shell command line?

The back tick in PHP is known as an language::operators.execution(), it’s function is identical to shell_exec.

Fetch the user Apache runs under

This ones quite useful when setting directory permissions and you don’t know which user your web-server is running as. The example below uses the default Apache server as provided by Mac OS X 10.9.

// Returns the current user apache executed the script as
print_r(`whoami`);
# Outputs: `_www`

Get the directory of the current running script

// directory of the running script (index.php in the `web` directory)
print_r(`pwd`);
// Outputs: `/Users/Anil/Sites/website/web/`

Better formatting using nl2br

You can use the nl2br function of PHP to output a better formatted version.

// List the directory contents (nl2br will output them onto different lines)
print_r(nl2br(`ls`));
// Outputs: (The contents of the current directory)..
/**
index.php
javascript
css
images
favicon.ico
 */