Jump to content
MakeWebGames

MArket Value


Hash-Op

Recommended Posts

In this topic, I shall just be covering something simple which should help you boost your knowledge.

A good way to get familiar with operators is to use them to perform arithmetic operations on variables, as in the following example:

 

<html>
<head>
</head>
<body>

<?php

// set quantity
$quantity = 1000;

// set original and current unit price
$origPrice = 100;
$currPrice = 25;

// calculate difference in price
$diffPrice = $currPrice - $origPrice;

// calculate percentage change in price
$diffPricePercent = (($currPrice - $origPrice) * 100)/$origPrice

?>

<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>Quantity</td>
<td>Cost price</td>
<td>Current price</td>
<td>Absolute change in price</td>
<td>Percent change in price</td>
</tr>
<tr>
<td><?php echo $quantity ?></td>
<td><?php echo $origPrice ?></td>
<td><?php echo $currPrice ?></td>
<td><?php echo $diffPrice ?></td>
<td><?php echo $diffPricePercent ?>%</td>
</tr>
</table>

</body>
</html> 

 

Looks complex? Don't be afraid - it's actually pretty simple. The meat of the script is at the top, where I've set up variables for the unit cost and the quantity. Next, I've performed a bunch of calculations using PHP's various mathematical operators, and stored the results of those calculations in different variables. The rest of the script is related to the display of the resulting calculations in a neat table.

If you'd like, you can even perform an arithmetic operation simultaneously with an assignment, by using the two operators together. The two code snippets below are equivalent:

 

<?php

// this...
$a = 5;
$a = $a + 10;

// ... is the same as this
$a = 5;
$a += 10;

?>

 

If you don't believe me, try echoing them both.

Enjoy

[Thanks Vaswani]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...