iWebBuddy.com - Website Design and Web Development Forum
Thanks for visiting iWebBuddy.com ,

If it is your first time here, please enjoy reading the tutorials and articles. If you can not find what you're looking for, then you can join the forums by registering so you will be able to log into the forums and ask for support for website development, request graphic designing services, promote your website, request website reviews, get advice from others about your website or be an active member of the community to help iWebBuddy.com grow.

After you register an account, you are required to log in. Once you log into your account, you will have more privileges. You will be able to post on the forums, send private messages and view the Chatbox ( link ) at the bottom of the forums.

Have a nice day.

Numbers and loops

Post new topic   Reply to topic

View previous topic View next topic Go down

Numbers and loops

Post by dsignresponder on Fri Dec 18, 2009 11:46 am

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 :
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... Smile

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! greatwork

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

dsignresponder
Forum Newbie
Forum Newbie

Posts Posts: 9
iCoins iCoins: 30

View user profile

Back to top Go down

View previous topic View next topic Back to top


Permissions of this forum:
You cannot reply to topics in this forum