A First Step in PHP Programming
A computer program is a list of instructions that tell the computer what to do.
A program, like any system, can be seen as sequence of
Input → Process → Output
The Hello World program is the common first program used to introduce
a programming language, it has the objectives of
- Verifying that someone is able to properly run a program
- Verifying that a program is able to send output to the user
- Verifying that someone is able to enter the source code (program statements)
of a program.
PHP is an interpreted language, which means that
PHP is a program itself that reads statements
that are written in the PHP language, and executes the statements.
Running PHP Interactively
If you have PHP installed, you can run PHP interactively, which
means you can enter commands and have PHP read and interpret statements as you
type them in. Text entered by the user/programmer is shown below in blue.
Example: running PHP from a Linux command line
PHP
<?
print 'Hello world.';
?>
[Ctrl]-D
Hello World.
Explanation:
The
PHP command starts the PHP interpreter.
PHP will not send the user any output or prompt for input; PHP
is waiting for program instructions.
The first command entered by the user is
<? which signals to php that what comes next is a PHP
program statement. All text that is between the tags <? and ?>
are treated as PHP statements. All text outside of these tags will
be sent to the user exactly as it appears.
print 'Hello world.';
The user enters
[Ctrl]-D (control key + the letter D) to let PHP know there is
no more input. The [Ctrl]-D may show up as ^D on some systems, or may not
show up at all. ^D is often used as the end-of-file marker. Here the ^D
marks the end of the standard input file, the keyboard.
When the user enters
^D PHP executes the print statement, sending the
output
Hello World. to the screen and PHP terminates.
Three ways to send the "Hello World." message to the user:
print 'Hello world.';
print ('Hello world.');
echo 'Hello world.';
Running PHP with a stored program
Most progams will be entered using a text file, where the programs
can be stored, edited as needed, and reused.
Create a text file named helloWorld.php
and enter the following program
<?
print 'Hello world.';
?>
With the program saved, PHP can be run from the command line with the name of the stored program.
Example: running PHP with a program name
php helloWorld.php
Hello World.
Explanation:
The
php helloWorld.php command starts the PHP interpreter
and PHP will immediately begin to process the program
in file
helloWorld.php.
PHP will not display any program information about
itself, nor will it provide a prompt. PHP executes
the program helloWorld.php sending the output
Hello World. and then terminates.
Making a PHP program executable from the command line
When you run PHP from the command line, the operating system
will search your path for the PHP interpreter. In unix/Linux you can
find out where a program is located using the which command.
To find out where PHP has been installed, issue the following
Linux command:
In my system, the which command replies with:
By adding the following line as the first line of helloWorld.php,
and making the file executable, the operating system will always
know to use /usr/bin/php to execute the program:
#!/usr/bin/php
<?ph
p
print 'Hello world.';
?>
Make the program helloWorld.php executable using the following unix/Linux command:
The program helloWorld.php can be run from the command line using only the name of the stored program.
Example: running an executable PHP program
./helloWorld.php
Hello World.
Explanation:
When the operating system is told to execute the helloWorld.php program, it will see by
the first line in the file that it should use the PHP interpreter,
and PHP will immediately begin to process the program.
PHP will not display any program information about
itself, nor will it provide a prompt. PHP executes
the program helloWorld.php and then terminates.
The ./ before helloWorld.php
in the command tells the operating system to look in 'the current directory' for the program
helloWorld.php