Our First Program
We will now create your our PHP program. This exercise will
give a quick review of the relevant Unix commands involved in running
PHP on RCI or Eden, but our future examples will assume that you
know these commands and focus on PHP only. If you have not yet done so,
please
ssh
to RCI or Eden, login, and type the following commands at
the prompt.
Create your public_html directory:
mkdir public_html
If your public_html directory already exists that is OK.
Please continue on to the next step.
Change the permissions of public_html so that it is viewable from the web:
chmod 755 public_html
Enter your public_html directory:
cd public_html
Create your php sub-directory:
mkdir php
If your php directory already exists that is OK.
Please continue on to the next step.
Change the permissions of the php directory so that it is viewable
from the web:
chmod 755 php
Enter your php directory:
cd php
Create a file called "hello.php3" inside this directory.
touch hello.php3
Change its permissions so that it can be seen
from the web.
chmod 644 hello.php3
Edit the file using the emacs editor.
emacs hello.php3
The editor should open an empty file named hello.php3,
in which you will write your program. Copy the following
into this file:
<html>
<h3>My first PHP Program</h3>
<? echo "Hello world!"; ?>
</html>
It is better that you copy the above in by hand (instead of
copying and pasting) so that you get familiar with the syntax
of PHP. Save the file in emacs by holding down the control
key with your left hand and then typing x and then s
with your right hand. You can then exit out of emacs in a similar
way by typing control x and then control c.
You can use the
Emacs
Command Summary or an introductory
Emacs
tutorial for basic information on Emacs.
Keep in mind that Emacs is much more than just a text editor. It is
an environment that can be easily extended to suit your needs. It is
also an ideal environment for developing PHP and has its dedicated
PHP Mode.
Here are some tips to help you start taking advantage of Emacs'
power.
If you want more Emacs information, see the
GNU
Emacs Manual.
Once your file is saved you should try to run it on the
webserver. In order to do this you must start up a new browser
window and go to the appropriate URL.
The following links point to a PHP script which given your username,
will direct you to your PHP examples.
If you are an RCI user
click here.
If you are an Eden user
click here.
When you get to the version of the above URL that is appropriate
for you, a listing similar to the following will be displayed on
your browser.
Click on hello.php3 (not above, but on your browser).
You should see something like the following.
If you don't see something like the above then there has been a syntax
error. A syntax error is what happens when you do not write proper
statements in PHP, and as a result the PHP interpreter, should tell
you which statement it was not able to
parse.
Depending on how PHP has been configured on the computer that you are
working on, you might see error messages or a blank screen. RCI/Eden's
PHP configuration does not have error messages, so that if you have a
syntax error, then you should see a blank screen in your browser. If
this is the case, run PHP from the command line as described
here, so that you can see where
the parse error occured. You can also have a carefull look at the
original code to the program as described above. When your syntax is
perfect your program should run as described above.
Congratulations, you've written your first PHP script and have
taken part in a
grand tradition!
Echo
Now that you have a feel for how to run PHP on RCI/Eden, and have
seen the output of your PHP program, let's analyze the code.
The following is the combination of HTML and PHP that we used
to write this program.
<html>
<h3>My first PHP Program</h3>
<? echo "Hello world!"; ?>
</html>
If we remove the HTML we will have only the following PHP.
<? echo "Hello world!"; ?>
The above example revolves around the PHP
language construct echo.
The echo language construct must be followed by a
space. After the space you can give echo any sentence
you choose, provided that the sentence is contained within
quotes (" "). Like any PHP statement, it must also end
with a semicolon (;).
The echo language construct also raises an important term
in PHP, as well as general programming terminology: strings,
words and sentences.
Strings
In the purest sense, a string is series of characters.
In the context of this example we will add the requirement that
strings be contained within quotes (" ").
Strings can be
divided into two types, words and sentences.
- A word is a series of characters within quotes
(" ") that does not contain any spaces or commas.
- A sentence is a series of characters within quotes
(" ") that does contain at least one space or comma.
Given the above three definitions, we can deduce that in the
"Hello World!" example, what was referred to as a
sentence, can also be formally refered to as a set of
words, or a string.
The following are examples of three different words:
"1234567890"
"-=\_+|X~qwertyuioo"
"String"
and the following are examples of three different sentences:
"1234567890,-=\_+|X~qwertyuioo"
"pasdf ghjkl;;'zxcv bnm,./[]{}"
"This collection of words is a string!"
All six of the above examples are strings. For more
information on strings see the PHP manual's entry on
strings.
Now that we have our terminology defined we can say that
whatever you choose to display with echo must be a
string.
PHP also has a a language construct called print which is
very similar to echo. Within this tutorial we will
use echo. More information on
echo and
print can be found online.
We can use echo to display information
on our website. Most of the PHP programs we will examine in
this class will use PHP to process data and come up with an
answer. We will then use echo to display that answer.
Thus, even though it serves a simple function, it is essential
to using PHP.
Variables
Variables (as taught in Algebra) can take on unknown values and
allow us (and our PHP programs) to generalize, which in turn can
allow for flexibility. We use variables in spoken languages like
English often. For example, a common word such as age can
be considered a variable because age can take different values for
different people or for the same person at different times.
Similarly,
country can be considered a variable because a person's country
can be assigned a value. Programming languages like PHP also allow us
to give a name to something which we can specify in the future.
Variables are denoted in PHP by preeding
them with a dollar sign ($). So if I wanted to create a
variable called a, I would type the following:
$a;
When programming you should give your variables meaningful names.
For example, $age will make more sense to you then $a, if you want
a variable to represent someone's age. Variables should be single
words (as specified above) .
For example, if we were to type:
$erins age
To represent a person named Erin's age we would have a syntax error,
because the word "age" would have no meaning in PHP and $erins would
be the variable that we created. You should avoid having a space or
a comma in your variable. It is conventional in PHP to use an
underscore to take the place of space in order to keep a string a
word. So, we would represent the variable for Erin's age in PHP like
this:
$erins_age
Another convention which comes from Java for representing spaces
is to remove all spaces, but use a capital letter to denote what
you would intend to be a separate word. For example:
$erinsAge
This method is referred to by some programmers as "studly caps".
In this tutorial we will be using the underscore replacement of
space instead.
Operators
Operators do things to variables. If I wanted to say
"my age is 26", the word "is" could be seen as an operator.
The string "my age" could be seen as a variable, and "26"
could be seen as the value of the variable. In PHP this
would look like this:
$my_age = 26;
Note the use of the assignment operator (=) which
sets $my_age to the value of 26.
PHP has other operators and you should already be familiar
with most of the following:
| Example | Name | Result |
|---|
| $a + $b | Addition | Sum of $a and $b. | | $a - $b | Subtraction | Difference of $a and $b. | | $a * $b | Multiplication | Product of $a and $b. | | $a / $b | Division | Quotient of $a and $b. | | $a % $b | Modulus | Remainder of $a divided by $b. |
Throughout the tutorial we will learn about new operators,
but in our next example we will learn about special variables.
Previous Page
Next Page

edseries@nbcs.rutgers.edu
|