Support Forums
PHP Tut 4 - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: PHP Tut 4 (/showthread.php?tid=8064)



PHP Tut 4 - Minus-Zero - 06-29-2010

Credits to W3Schools for the text in quote tags and the first example.
Quote:If variables are the building blocks of a programming language, operators are the glue that let you build something useful with them. You've already seen one example of an operator - the assignment operator -, which lets you assign a value to a variable. Since PHP believes in spoiling you, it also comes with operators for arithmetic, string, comparison and logical operations.

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

Code:
<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>

You will notice in the code it sets its variable but as you look farther down your like Uhh wat?

That is because you see this
Code:
// calculate difference in price
$diffPrice = $currPrice - $origPrice;

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

Now Lets explain some of that!

$diffPrice = $currPrice - $origPrice;

Well this is simple its just using set variables to subtract from each other, then the resulting difference is our new variable.

$diffPricePercent = (($currPrice - $origPrice) * 100)/$origPrice

Ok well here you see your setting a variable again. Notice all that stuff after the " = ". Well sort of like order of operations. What it looks for is the thing it needs to do first, which is whats inside the parenthese. Oh but look their is parenthese in the parenthese. So it does the parenthese inside of the parenthese first. Which is currPrice - OrigPrice, then it will times it by 100. Finally it will divide by the OrigPrice.

The rest of the code is just putting it into a table.

Now lets move on to another example!

If you'd like, you can even perform an arithmetic operation simultaneously with an assignment, by using the two operators together! Is that amazing or what?

Here:
Code:
<?php

$s = 3;
$s = $s + 5;

?>

Does the same thing as:

Code:
<?php

$s = 3;
$s += 5;

?>

PHP also allows you to add strings with the string concatenation operator, represented by a period.

Code:
<?php

$a = 'Please;
$b = 'Vote';
$c = 'For';
$d = 'ExN';
$e = 'Now';

$statement = $a.' '.$b.' '.$c.' '.$d.' '.$e.'<br />';
print $statement;

$command = $b.' '.$c.' '.$d.' '.$e.'!';
print $command;

?>

First thing i did was set the variables.

$statement = $a.' '.$b.' '.$c.' '.$d.' '.$e.'<br />';

Ok Well basically all this did was set a variable using all the other variables..... Not much more i can say

Also you can concatenate and assign simultaneously.
Code:
<?php

$hel = 'hello';  

$hel .= 'o';

echo $str;

?>

First line is just me setting a variable as normal.

Then there is

$str .= 'n';

Well what this does is will take $str variable which was "hell" And just add an o to the end. Turning "hell" Into "hello"

That is all for this tutorial

Please rate and thank me! Smile


RE: PHP Tut 4 - `P R O D I G Y™ - 07-21-2010

Another thorough tutorial by you, you're working for 5 soon.

Keep it up dude