Databases Reference
In-Depth Information
Example 14-5. A PHP script to say hello
<?php
echo "Hello, world\n";
?>
Type this in an editor and save it to a file called
hello.cl.php
.
You can run PHP scripts from the shell prompt or command window by running the
PHP executable and passing the name of the script to it:
$
php hello.cl.php
Hello, world
If the operating system can't find the
php
executable, you'll need to specify the full path
to the file. On a Linux or Mac OS X system, this may be available as the file
/usr/bin/
php
. If you've installed XAMPP, you can use the program
/opt/lampp/bin/php
on Li-
nux,
/Applications/xampp/xamppfiles/bin/php
on Mac OS X, and
C:\Program Files
\xampp\php\php.exe
on Windows.
You can also have the operating system call the PHP program automatically when you
run a PHP script from the command line. To do this on a Linux or Mac OS X system,
you need to add this line to the top of each script to specify the PHP program to use:
#!
path_to_the_php_executable
For example, you could specify the
/usr/bin/php
program in the
hello.cl.pl
script as
follows:
#!/usr/bin/php
<?php
echo "Hello, world\n";
?>
You must add the “executable” permission to the file so that the operating system can
execute this script:
$
chmod u=rwx,g=,o= hello.cl.php
We explained permission settings in “Restricting access to files and directories” in
Chapter 2.
You can now run the script by just typing in its name:
$
./hello.cl.php
Hello, world
The initial
./
tells the operating system that the file is in the current working directory.
If the script is in a directory that's also listed in your system PATH, you can omit these
characters. See “Error Message About MySQL Executable Programs Not Being Found
or Recognized” in Chapter 2 for details on setting the system PATH.
For a Windows system, you should associate PHP files with the PHP program. Open
a command-prompt window and type in these two lines: