| Overview: We
will write our first PHP code, this section also covers basic PHP syntax,
including variable usage.
Before you can
work with PHP it's a good idea that you install PHP on your computer if
you haven’t installed PHP yet, now is a good time to install PHP.
First we will make a simple HTML page
<html>
<body>
Welcome to PHP tutorial
</body>
</html>
Okay the above HTML page doesn't do anything special
but just outputs Welcome to PHP tutorial on the browser, now lets add some
PHP tags to it.
Note: All PHP code is written between <?php
and ?> tags, now lets write
"Welcome to PHP tutorial" using PHP.
<html>
<body>
<?php
echo "Welcome to PHP tutorial";
?>
</body>
</html>
Okay the above PHP script will output Welcome
to PHP tutorial note that we use the echo construct of PHP, the echo
function simply outputs the content to the browser.
So if we wanted to write "Welcome" we would write echo
"Welcome";
Note Have you noticed the ; at the end of the statment, Each
PHP instruction must end with a semicolon.
Introduction to Variables
Let me introduce you to PHP variables, variables help you store and
retrive data.
- All variables start with a dollar sign
$
- The name of the variable can
consist of letters and numbers, but must begin with a letter. It can
also contain special characters like underscore '_', see some examples
below
$site =
'phpbuddy.com' // valid variable
$4site = 'not yet'; // invalid variable starts with a number
$_4site = 'not yet'; // valid variable starts with an underscore
Some more examples
$testvariable
= 1 + 1; // Assigns a value of 2.
$testvariable = 1 – 1; // Assigns a value of 0.
$testvariable = 2 * 2; // Assigns a value of 4.
$testvariable = 2 / 2; // Assigns a value of 1.
Okay to make things easier I will show you an
example here we will create a variables $a, $b and multiply them and
store the value in $c, just see it's so simple
<?php
$a = 5; //we created a variable a and assigned it a value of 5
$b = 2; //we created a variable b and assigned it a value of 2
$c = $a * $b; //we multiply varaible a and b and store the value
in c
echo $c; //we print the content of variable c which is 10
?>
PHP variables can
hold strings, dynamic data, PHP variables are case sensitive $a is
not the same as $A
<?php
$a = "Welcome to PHP "; //$a holds the string Welcome
to PHP
$A = 4; //$A holds the value 4
echo "Variable a conatains: $a";
echo "Variable A contains: $A";
echo "$a $A"; //outputs both variables $a and $A
?>
Okay now we know what are variable and how to
work with them, In the next part we learn how to add logic to our
programms.
Adding
Logic with PHP
Overview: Introducing the if else
construct, while loops and other control loops that will help add
logic to your code.Okay our task is to make our programs intelligent I
mean let's learn how to add decision making logic in our code. PHP
like all other good programming languages provides logic and looping
using the if and while statements.
Introducing the IF construct
The if construct is one of the basic decision making construct it is
also included in PHP. It allows for conditional execution of code
fragments. PHP features an if structure that is similar to that of the
'C' language.
The syntax is of if else structure is
If
(conditions)
{
// Code if condition is true
}
else
{
// Code if condition is false
}
Let me give you an example, let's assume that you and your friend are
planning to go for a movie you need more than 20 bucks to go to the
movie, if you have less than 20 bucks you can't go, if you would have
written a program the PHP equalent code would be something like
<?php
$money = 15;
if($money > 20)
{
echo "Hey we can go to the movie we have $money bucks";
}
else
{
echo "Oops we can't go to the movie we have only $money
bucks";
}
?>
We have used the Comparison operator > Greater than above to
check if $money > 20
PHP supports many different comparison operators that allow us to add
logic in our code.
The comparison operators are listed below:
$a == $b $a is equal to $b
$a != $b $a is not equal to $b
$a < $b $a is less than $b
$a > $b $a is greater than $b
$a <= $b $a is less than or equal to $b
$a >= $b $a is greater than or equal to $b
Now we know how to add logic using if else and comparison operators to
our code in the next part we will learn the basic's of looping.
Basic
Looping
Overview: Understanding
how to use looping in our code introduction to While Loops
The ability to add logic in a script
is the first fundamental part of any true programming language, but
then the ability to execute the same code multiple times is also very
important.
Imagine you had to print the number from 1 to 20 you could echo it
from 1....20 or we can write a simple while loop that does the job.
Introducing the While Loop
while loops are the simplest type of loop in PHP, It tells PHP to
execute the nested statement(s) repeatedly, as long as the while
expression evaluates to TRUE.
The syntax of a while loop
While(conditions)
{
// This code will execute until the conditions
// provided no longer evaluates to true
}
Let do a simple while loop that prints the numbers 1 to 10
<?php
$cnt = 1;
while($cnt != 11)
{
echo "$cnt
";
$cnt = $cnt + 1;
}
?>
Understanding the above example: In the above example we have
created a variable $cnt and assigned it an value of 1, then we add the
while loop.
We said to the while loop to continue the loop until $cnt != 11 means
that the loop should continue until $cnt reaches 11, then we print the
value of $cnt, after that we increment the value of $cnt by one, the
loop checks the value of $cnt if its not equal to 11 it continues the
loop, when finally $cnt reaches 11 the loop exits.
Let's make a simple multiplication table using While Loop
<?php
$table = 5; //we will use the multiplication table 5
$cnt = 1;
while($cnt != 11)
{
echo "
$table x $cnt = ". $table * $cnt;
$cnt++;
}
?>
See how easy it is to use while loops!
| Always
be sure to incriment the counter and check the logic
in the while loop so that it exit's upon reaching
certain condition or value otherwise you might end up
with a infinte loop (A loop that goes on and on and
never exits). |
|
|