Vamp the Linux LAMP with PHP E-mail
by David M Williams   
Monday, 24 September 2007
Type up the following in a text editor and save it to disc with a .php file extension, perhaps as testing.php. To view it from the web, you’ll need to make sure you save it into an area covered by your web server.

<html>
<body>
<p>10 + 20 =
<?php
  $a = 10;
  $b = 20;
  echo $a + $b;
?>
<p>10 + “20” =
<?php
  $c = “20”;
  echo $a + $c;
?>
</body>
</html>


There’s two different ways you can take this for a whirl. The first is, as said, to copy the file into your web server directory then call it from a web browser as you would any other page except be sure to specify the full filename including .php suffix.

Alternatively, you can invoke the PHP pre-processor directly from the command line by executing php testing.php – in this case the HTML output will still be generated but will display to the screen.

Either way, you’ll see the output tells you twice what the sum of 10 and 20 is. Compare the output with the program code, reading from top to bottom. You can see we switched into PHP mode twice, with HTML tags used to control the paragraphing and messages.

You’ll also note the differences between the two additions; the first time, two variables called $a and $b are assigned numeric values. Their sum is displayed. The second time around, $a is added to $c which actually contains the text string “20”, rather than the numeric value 20. When the addition is performed PHP sensibly recognises the need for “20” to be interpreted as a numeric value and still delivers the correct result. PHP offers other mathematical operators as well as addition, namely subtraction (-), multiplication (*) and division (/).

There are some more advanced operators too which perform functions like bitwise operations and modulus division.

You might actually want PHP to join two text strings together. In that case, don’t use the + symbol because PHP interprets that as adding numbers. Instead, use a period (.) to join two strings together, one right after the other.

There’s two more import things to note in the listing above. Semicolons (;) are used to separate commands in PHP and the echo command, which as you might guess, sends the result of a calculation or some other operation to the output being built up.


Conditional statements
Sometimes you might want to display a message or perform an action only if a certain condition is true. You don’t want this action to happen if the condition isn’t met. The simplest way to achieve this is with the if statement.

if (condition) {
  Perform some action
  which can be as many lines as needed
}
else {
  Optionally perform some other action
  if the condition is not met
}


This looks easy enough, but actually there’s a number of important things to be aware of.




 
< 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