Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Tut 4
#1
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
Reply
#2
Another thorough tutorial by you, you're working for 5 soon.

Keep it up dude
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 759 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,237 10-10-2011, 01:00 PM
Last Post: Greyersting
  [TUT] PHP Shell Scripting Gaijin 8 8,007 08-05-2011, 09:34 PM
Last Post: BreShiE
  [TUT] PHP Password Protected Page PurpleHaze 23 5,724 05-20-2011, 02:56 AM
Last Post: stephen5565
  [TUT] Pagination using PHP Gaijin 7 4,188 11-01-2010, 09:01 AM
Last Post: Arеs

Forum Jump:


Users browsing this thread: 2 Guest(s)