Vamp the Linux LAMP with PHP E-mail
by David M Williams   
Monday, 24 September 2007
Firstly, the condition can be expressed by comparing variables to other variables or to literal values using operators, some of which are well known and others which are not. The less-than symbol (<) is straightforward enough; a condition like ($a < $b) clearly tests if the value of variable $a is less than that of variable $b. Similarly, greater-than-or-equal-to is easily understood; ($a >= $b) tests if $a has a value which is either greater than, or equal to, that of $b.


What is not so obvious is that values are tested for equality using a special operator ==. This is because the equals sign (=) is already used to assign values to variables, so something else was needed to check if two items are the same. Remember, $a = $b copies what’s in $b over to $a. ($a == $b) tests if $a and $b both have the same value already.


You should also note the special symbols used. A pair of parenthesis surround the condition. Curly braces are used to define just how many program lines should be executed as part of the action. Also, you will see an “else” clause which lets you do something special even if the condition failed. An obvious example might be to display different status messages like so:


if ($loginSuccessful) {
  echo “Logged in.”;
}
else {
  echo “Log in failed. Please try again.”;
}


This else clause is purely optional. There is no need to include if it is not required. In this case, PHP will do what it is instructed if the condition is met but will not do anything special if the condition fails; it will just keep executing instructions at the closing curly brace.


Looping
There are three fundamental constructs in programming: sequence – where programs execute lines one at a time, in order; selection – where a condition is evaluated as above; and repetition – where a block of lines is repeatedly executed over and over. This latter construct is what we call a loop.

PHP lets you loop a series of instructions over and over, in several different ways. The most basic of these is the while loop which just keeps running forever so long as a specified condition remains true. As soon as the condition becomes false the loop will end.


$a = 0;
while ($i <= 10) {
  echo $i;
  $i++;
}


This bit of PHP code keeps running the loop so long as the value of $i is not yet greater than 10. Each time through the loop two commands are executed. The first displays the current value of $i, and the second increments $i by one. A special operator is used; ++ means simply that PHP should add one to the variable. This is a shorthand way of writing $i = $i + 1; which would also be valid and perform the same job.


Here’s where we’ll stop for the moment; check out part two to see more about PHP including getting user-entered data out of HTML forms. Also be sure to check PHP.Net for downloads, tutorials, articles and documentation.

Hopefully you're encouraged to try your hand at making your own PHP page. You'll be impressed at the results and how easy it is to obtain them. Before you know it, you'll be reading and writing PHP like a pro.

Powered By Joomla Tags

Please enable JavaScript in your browser to post your comment!



 
< Next story in category   Previous story in the category >
iTWire user statistics Visitors last 30 days
694,279
Subscribers 15,210
#1 independent technology news advertise here
  •   *  
  • Search
  • AdvSeach
  • Login
  • Events
  • FreeStuff

- Advertisement -

Featured Whitepapers

Follow iTWire on Twitter

About iTWire

iTWire is all about technology news, information, jobs and community for the IT and telecommunications industry professional. Subscribe to our free ICT daily newsletter