Business IT - Technology for your business

No. 1 Story

Telstra adds one million mobile services, but Sensis plummets

Telstra has revealed the addition of almost one million new mobile services in the six months to December 2011, but Sensis revenues plummeted 24 percent in 12 months.

read more

Vamp the Linux LAMP with PHP

Business IT - Open Source

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.




- sponsored feature -

The Death of Traditional BI: What’s Next?

How to Make Business Discovery Work for Your Business IP PABX BUYING GUIDE

Business Discovery takes its cues from consumer apps. Like Google, it encourages us- ers to hunt for and explore data without worrying about or even noticing the underly- ing technology. Their entire experience is working within an intuitive interface to get real-time, self-service results with only minimal training. ...more