Numbers and loops
iWebBuddy.com - Website Design and Web Development Forum :: Web Design and Development :: Scripting Languages :: Web Server
Page 1 of 1 • Share •
Numbers and loops
Greetings!
I'll try to show you guys some very basic operations with numbers in PHP.
You can use PHP to solve any math function. I will show a simple example, I believe it will be enough to get the point and start your own. Find out more about math functions at : W3SCHOOL or PHPNET
1. Numbers
- open your text editor (notepad) and type :
- save your file as 'numbers.php' in your C:/Xampp/htdocs/lesson_2 folder
- point your browser to
NOTE : you can combine the variables as you wish (my suggestion in similar situations is to create a new variable and defining a math function for it [like $d = $a + $b] rather than calculating and outputting at the same time... ). Technically it's the same, but it's better to read the code...
2. Loop
Loops are very useful when you have to run the same php function several times (i.e. echo all/several rows from a MySql database...).
- now type in your text editor :
- save your file as 'loop.php' in C:/Xampp/lesson_2/loop.php and point your browser to
There are some other loop functions as well (i.e. foreach, do while... etc), you can find all at Control Structures
If you're gonna work with PHP/MySql in the future, be prepared to read/write all kinds of these loops! You'll find out yourself that they are extremely useful!
That's it from me this time!
Hope to see you around. Take care!!!!
I'll try to show you guys some very basic operations with numbers in PHP.
You can use PHP to solve any math function. I will show a simple example, I believe it will be enough to get the point and start your own. Find out more about math functions at : W3SCHOOL or PHPNET
1. Numbers
- open your text editor (notepad) and type :
- Code:
<?php
$a = "5";
$b = 6;
$c = 17;
$d = $a + $b;
$e = $b + $c;
echo 'a = '.$a.'<br />';
echo 'b = '.$b.'<br />';
echo 'c = '.$c.'<br />';
echo 'a + b = '.$d.'<br />';
echo 'b + c = '.$e.'<br />';
echo 'a + c = '.($a + $c).'<br /><br />';
?>
- save your file as 'numbers.php' in your C:/Xampp/htdocs/lesson_2 folder
- point your browser to
- Code:
localhost/lesson_2/numbers.php
NOTE : you can combine the variables as you wish (my suggestion in similar situations is to create a new variable and defining a math function for it [like $d = $a + $b] rather than calculating and outputting at the same time... ). Technically it's the same, but it's better to read the code...
2. Loop
Loops are very useful when you have to run the same php function several times (i.e. echo all/several rows from a MySql database...).
- now type in your text editor :
- Code:
<?
echo 'FOR LOOP<br /><br />';
$j = 5;
$k = 0;
for ($i = 0; $i < $j; $i++) {
echo $i.'<br />';
}
echo '<br />WHILE LOOP<br /><br />';
while ($k <= $j) {
echo $k.'<br />';
$k++;
}
?>
- save your file as 'loop.php' in C:/Xampp/lesson_2/loop.php and point your browser to
- Code:
localhost/lesson_2/loop.php
There are some other loop functions as well (i.e. foreach, do while... etc), you can find all at Control Structures
If you're gonna work with PHP/MySql in the future, be prepared to read/write all kinds of these loops! You'll find out yourself that they are extremely useful!

That's it from me this time!
Hope to see you around. Take care!!!!


dsignresponder- Forum Newbie

Posts: 9
iCoins: 30
Permissions of this forum:
You cannot reply to topics in this forum












