php

Execute shell command in PHP

The example below shows how to execute a shell command from within php script.

The command pwd executes the current working directory.

<?php
$output = shell_exec('pwd');
echo "<pre>$output</pre>";
?>

Drupal notes

  • If placed within a drupal node it will always display the base location of the drupal installation.
  • If you require to call a program which resides above the drupal directory the following may be used.

    <?
    include("../../legacy/program.php");
    ?>

Ternary Operator

$variable = condition ? if true : if false

An example is shown below:

  $style = isset($_GET['style']) ? $_GET['style'] : 'default';

This checks whether variable $_GET['style'] is set. If it is variable $style is given this value. Alternatively, variable $style is given the value 'default'.