Below is a resource of tools and explanations from various
aspects of software development.
What you will find below is beneficial to programmers and
developers of all experience levels.
Essentially, it allows you interact with your pages.
PHP is able to pre-form a number of tasks
including: printing data, making numeric calculations (such
as addition or multiplication), making comparisons (which
is bigger, are they equal, etc) and making simple choices.
From this you can create more complex loops and functions
to make your page generate more specialized data.
PHP is a server side scripting language used on the Internet
to create dynamic web pages. It is often coupled with MySQL,
a relational database server that can store the information
and variables that the PHP files may use.
Together, they can create everything from a simple web
site to an interactive web forum.
Everything between the “?”s is read as PHP
code. The <?php statement can also be phrased as simply
<? if desired.
Anything outside of these PHP tags is read
as HTML, so you can easily switch between PHP and HTML
as needed
PHP Functions
A function is something that performs a
specific task. People write functions
if they plan on doing the same task repeatedly. This allows
you to only write the code once, saving time and space.
This gives an example of three functions;
absolute value, square root, and rounding.
You can use the function in the print statement
or assign it to a variable.
The Mail () Function
Many websites offer a way for you to send
them an email from a form on their site.
Providing the form as opposed to simply
listing your email address serves two purposes:
-
This form lets the website owner decide
what information it is important to collect and prompts
the users to fill in the answers to each of their
questions.
-
If you list your email directly on your
site it can be picked up by bots designed to 'farm'
email addresses. What that means for you is SPAM.
Nobody likes to have their inbox flooded with SPAM,
and using a form can help prevent that.
WHILE Loops
In PHP there are several different types
of loops. What a loop does is evaluate
if a statement is true or false.
If it is true, it executes
some code and alters the original statement. It will then
start all over again by re-evaluating it.
It will continue to loop through the code
like this until the statement becomes false.
Here is an example in its simplest
form:
What it does: While a number
is greater than or equal to 10 it prints the number. The
++ adds one to the number, however this
could also be phrased as $num = $num + 1;
Once the number becomes greater than 10, it stops executing
the code within the {brackets}
FOR Loops
A FOR loop is similar to
a WHILE loop in that it continues to process a block of
code until a statement becomes false.
However in FOR Loops, everything is defined
in a single line.
The basic structure for
a FOR loop is:
for ( start; conditional; increment) { code to
execute; }
Using the previous for the numbers 1 through
10, do the same thing using a FOR loop
The FOR loop can also be used in
conjunction with a conditional:
Other PHP Functions
date(), mail() and more
date()
This function returns a local time (date)
formatted according to the parameters within the format
string.
An example:
The script would output: Today
is: 2001-06-19 assuming today is June 19th, 2001.
mail()
An example:
The above script will send an e-mail to
youremail@server.com with the PHP e-mail
example in the subject line of the e-mail message.
The Message goes here as the actual message.
The mail() function will
work only if SENDMAIL is installed on
your server. Contact your host before using this function.
mysql_connect()
An example:
In order to connect to MySQL
database you must know the hostname, username and
password of the database.
The next function closes the last
opened database:
mysql_close()
Create your first table in the database using the following
complete PHP script:
Connecting to MySQL
Interacting with MySQL
makes PHP a more powerful tool. Below
is some of the most common ways PHP interacts with MySQL.
To follow along, create a database table
by executing this command:
This will create a table that has friends'
names, favorite colors, favorite foods, and pets to work
with.
The first thing to do in the PHP file is
connect to the database. Do this using this code:
You will replace server, username, password,
and Database_Name with the information relevant to
your site. If you are unsure what these values are, contact
your hosting provider.
Writing Functions
While having pre-made functions is useful,
for most things you are going to want the flexibility
to write your own custom functions. A function you create
takes this shape: function functionname () { your
code }
Below is an example of two simple functions:
As seen above, the code defined between
the { and } is all executed when calling
the function below.
In the first example (examplefunction)
the function was used to simply print text. In the second
function (sqr) you needed an operand
to complete the math within the function.
Put this inside the parenthesis
() after the function name. Then, add in the
RETURN line, so that the value is outputted
to the place in the code where you originally called the
function.