<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webcodez - The database of web programming tutorials &#187; loops</title>
	<atom:link href="http://www.webcodez.net/tag/loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Tue, 18 May 2010 16:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generate Random Passwords</title>
		<link>http://www.webcodez.net/php-mysql/generate-random-passwords/</link>
		<comments>http://www.webcodez.net/php-mysql/generate-random-passwords/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 15:04:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[net script]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=184</guid>
		<description><![CDATA[For generating passwords multiple methods can be used. In this tutorial we&#8217;ll be discussing 2 ways to generate random passwords. The result of both methods is exactly the same. Although method 1 is based on generating random indexes and picking another (random) index&#8217;s value each time when adding a new random character to the random [...]]]></description>
			<content:encoded><![CDATA[<p>For generating passwords multiple methods can be used. In this tutorial we&#8217;ll be discussing 2 ways to generate random passwords. The result of both methods is exactly the same. Although method 1 is based on generating random indexes and picking another (random) index&#8217;s value each time when adding a new random character to the random password. While method 2 is based on picking the same index but shuffling the values and indexes so that that index contains a different value each time.</p>
<p><strong>Method 1</strong><br />
We&#8217;ll start with the first method which uses these PHP functions:</p>
<p>    * str_split &#8211; converts a string into an array<br />
    * shuffle &#8211; shuffles an array (index and values)</p>
<p>What we need to create first, is a variable $charset which contains all characters and maybe symbols that we want the system to generate a random password out of. So the characters the password generated can/may contain.</p>
<p>Here I take for example all alphabetical characters and all numbers.</p>
<pre name="code" class="php:nogutter">
&lt;?php

$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678901234567890012345678901234567890";

?>
</pre>
<p>The numbers 0-9 I put in it 4 times because there are about 2 times as much alphabetical characters and they&#8217;re put inside it twice ( non-capital and capital) and I want to have about the same amount (chance) of characters as numbers inside the password.</p>
<p>What we want to do is create a function that generates a random password using random characters or numbers from our charset. We also need to be able to give the amount of chars to pick randomly out of the charset to create a password from ( the length of the random password ). So the argruments for the function should be: charset, length. Where charset is our $charset and length is the length of the password ( amount of random chars picked from the charset and put together ).</p>
<pre name="code" class="php:nogutter">
&lt;?php

function random_passw($charset, $length) {

   ... generate $length random chars out of $charset

}

$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678901234567890012345678901234567890";

$length = 8; //we set the length for our password to 8 chars for example

?>
</pre>
<p>So now we&#8217;ve come to the part of the function which needs to shuffle all characters of the charset. Therefore we first need to put it into an array as the shuffling is based on shuffling the indexes and values. We use the str_split function to put the charset in an array.</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

   $charset_arr = str_split($charset); //charset_array

}
</pre>
<p>So now the charset is put in an array like this:</p>
<blockquote><p>$charset_arr[0] = &#8220;A&#8221;;<br />
$charset_arr[1] = &#8220;B&#8221;;<br />
$charset_arr[2] = &#8220;C&#8221;;<br />
&#8230; etc &#8230;</p></blockquote>
<p>In this case the index 0 has the value &#8216;A&#8217;. We&#8217;ll make a loop which adds that index value to the password ( we&#8217;ll make a variable $passw ) the amount of times set in $length:</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

    $charset_arr = str_split($charset); //charset_array

    $passw = ""; //empty password to start with

  for($a=1;$a<=$length;$a++) { //as long as $a is smaller than or equal to $length
  //and increase $a each time the loop is executed

      $passw .= $charset_arr[0]; //add the value of the charset's first character (of index 0, which in this case is equal to the character 'A' )

   }

 }
</pre>
<p>So now it adds '$length' times the character 'A' to the variable $passw. So if we give the value 8 to the variable $length, it adds 'A' to the password 8 times, like:</p>
<blockquote><p>$passw = "AAAAAAAA";</p></blockquote>
<p>But we want it to add a different character to the set (password) each time it adds a character. To do this we use the function shuffle. This function will shuffle all indexes and values randomly - shuffle all chars in the charset in a random order. So say we first got our charset like:</p>
<pre name="code" class="php:nogutter">
$charset_arr = "ABC";
</pre>
<p>Which has the array:</p>
<pre name="code" class="php:nogutter">
$charset_arr[0] = "A";
$charset_arr[1] = "B";
$charset_arr[2] = "C";
</pre>
<p>It will shuffle all values for the indexes, so it could be shuffled like this then (for example, but it's random so it could be shuffled anyhow) :</p>
<pre name="code" class="php:nogutter">
$charset_arr[0] = "C";
$charset_arr[1] = "A";
$charset_arr[2] = "B";
</pre>
<p>So then the index 0 suddenly got the value "C" instead of "A". This way we can make it pick the same index but containing another value for our index '0' each time and this way pick random characters out of the charset.</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

     $charset_arr = str_split($charset); //charset_array

    $passw = ""; //empty password to start with

  for($a=1;$a<=$length;$a++) { //as long as $a is smaller than or equal to $length
  //and increase $a each time the loop is executed

      $shuffled = shuffle($charset_arr); //shuffles the indexes and values - in other words: shuffles the chars in the charset in a random order
      $passw .= $shuffled[0]; //take the value of the index '0' of the SHUFFLED charset - the first character of the shuffled charset which is different each time it's shuffled ( which is each time the loop is executed / shuffle function executed )

      }

  }
</pre>
<p>Last thing we need to do is make it return the password generated, which can be done using the function return.</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

     $charset_arr = str_split($charset); //charset_array

    $passw = ""; //empty password to start with

  for($a=1;$a<=$length;$a++) { //as long as $a is smaller than or equal to $length
  //and increase $a each time the loop is executed

      $shuffled = shuffle($charset_arr); //shuffles the indexes and values - in other words: shuffles the chars in the charset in a random order
      $passw .= $shuffled[0]; //take the value of the index '0' of the SHUFFLED charset - the first character of the shuffled charset which is different each time it's shuffled ( which is each time the loop is executed / shuffle function executed )

     }

     return($passw);

  }
</pre>
<p>And we're done!</p>
<p>Example of use:</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

     $charset_arr = str_split($charset); //charset_array

    $passw = ""; //empty password to start with

  for($a=1;$a<=$length;$a++) { //as long as $a is smaller than or equal to $length
  //and increase $a each time the loop is executed

      $shuffled = shuffle($charset_arr); //shuffles the indexes and values - in other words: shuffles the chars in the charset in a random order
      $passw .= $shuffled[0]; //take the value of the index '0' of the SHUFFLED charset - the first character of the shuffled charset which is different each time it's shuffled ( which is each time the loop is executed / shuffle function executed )
  }

  }

$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678901234567890012345678901234567890";
$length = 8;

$password = random_passw($charset, $length);

echo "Your password is: ".$password;

?>
</pre>
<p><strong>Method 2</strong></p>
<p>Functions used:</p>
<p>    * mt_rand($first, $last) - generates random number between $first and $last<br />
    * length($str) - calculates the length of the string $str</p>
<p>What we basicly did in the first method was adding the same index's value of the charset. But we made it contain another value each time by suffling the values of the indexes. Now we'll keep our charset in the same 'order' ( so each index keeps the same value ). But this time we'll change the index we request the value of each time we do so. So this way it requests another index's value ( and so another character ) each time we add a new character to the password. In other words: we add random characters to the password. We'll again use our charset and length variables:</p>
<pre name="code" class="php:nogutter">
&lt;?php

$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678901234567890012345678901234567890";

$length = 8; //we set the length for our password to 8 chars for example

?>
</pre>
<p>And now create a function which generates the random characters. We'll therefore again use a for loop to make it add a new random character to the password string as many times as set in $length. So the password will be the length of $length.</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

   $passw = "";

   for($a=1;$a<=$length;$a++) {

       //add a random character to the $passw

   }

}
</pre>
<p>We again use the same loop and set a variable $passw in the beginning to empty. We'll be adding the random characters to it inside the for loop. The charset is now already having indexes and values for each character, like this:</p>
<pre name="code" class="php:nogutter">
$charset[0] = "A";
$charset[1] = "B";
$charset[2] = "C";
... etc ...
</pre>
<p>Just because we got the characters in this order inside the variable:</p>
<blockquote><p>$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678901234567890012345678901234567890";
</p></blockquote>
<p>You see the first character 'A' has been given the index 0. Now what we basicly need to do for adding a random character, is generating a random index for the array ( and so a random value/character corresponding to that index ). Which is generating a random number from 0 ( the first index ) to the last index. The last index can be generated by counting the amount of characters minus 1. Because the first character hasn't been given the index 1 but 0, and for the second character it's 1, etc.. So for the last character's index it's the amount of characters minus one. The amount of characters can be calculated using the length function:</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

   $passw = "";

   for($a=1;$a<=$length;$a++) {

       $first_index = 0;
       $last_index = length($charset)-1;

   }

}
</pre>
<p>Now we need to generate a random number between the first and last index ( 0 - $last_index ). We can use the function mt_rand to do this.</p>
<pre name="code" class="php:nogutter">
function random_passw($charset, $length) {

    $passw = "";

    for($a=1;$a<=$length;$a++) {

        $first_index = 0;
       $last_index = length($charset)-1;

       $index = mt_rand($first_index, $last_index); //generates random index

    }

 }
</pre>
<p>Now we got a random index we generated in the variable $index. And because it's a number between the first and last index of the charset, we are sure it has a value. Which is one of the characters of the charset. Which we can call like $charset[index] ( as it's $array[index], and the charset can be seen a special kind of array where each index has one value ( the next character in the string ) ). And as the random index we generated is in the variable $index, it becomes $charset[$index] for us.So we'll add that value to the password:</p>
<pre name="code" class="php:nogutter">
 function random_passw($charset, $length) {

     $passw = "";

     for($a=1;$a<=$length;$a++) {

         $first_index = 0;
         $last_index = length($charset)-1;

       $index = mt_rand($first_index, $last_index); //generates random index

       $passw .= $charset[$index];

     }

  }
 </pre>
<p>Allright, so now it adds a value of a random index ( and so a random character of the charset ) to the $passw variable. And it does this $length many times. In other words: it creates a random pasword with the length of $length.</p>
<p>Now the last thing to do is actually returning the password:</p>
<pre name="code" class="php:nogutter">
  function random_passw($charset, $length) {

      $passw = "";

      for($a=1;$a<=$length;$a++) {

          $first_index = 0;
         $last_index = length($charset)-1;

        $index = mt_rand($first_index, $last_index); //generates random index

       $passw .= $charset[$index];

      }

   return($passw);

   }
  </pre>
<p>Which is, easy as it gets, the function return. And the random password generator function is done!<br />
It has the same use as the previous one explained in method 1, so enjoy!</p>
<p>Cheers,<br />
Admin.</p>
<p>Btw, realised the first method wasn't that much faster afterall lol </p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/generate-random-passwords/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Foreach Loop</title>
		<link>http://www.webcodez.net/php-mysql/foreach-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/foreach-loop/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 09:57:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[foreach loop]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/wordpress/?p=103</guid>
		<description><![CDATA[Note: This chapter requires you to have a basic knowledge of arrays. If you don’t know what arrays are or how they work, please first have a look at the arrays tutorial.
The foreach loop is used to get all keys and values from all indexes (&#8217;sub-variables&#8217;) of an array and handle them with your given [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note:</strong> This chapter requires you to have a basic knowledge of arrays. If you don’t know what arrays are or how they work, please first have a look at the <strong>arrays tutorial</strong>.</p>
<p>The foreach loop is used to get all keys and values from all indexes (&#8217;sub-variables&#8217;) of an array and handle them with your given (&#8216;act&#8217;) code all one for one. A foreach loop looks like this (structure):</p>
<pre name="code" class="php:nogutter">foreach ( $array as $key => $value ) {

    ... act code ...

}</pre>
<p>The $array variable can be any array variable you want to handle and you can choose any variable name for $key and $value. In these variables ($key and $value in this example) the name ($key) and value ($value) of each index (‘sub-variable’) of the array will be stored and the act code will be ran. Example:</p>
<pre name="code" class="php:nogutter">$product_prices = array("tv" => 599.95,
              "computer" => 899.95,
              "notebook" => 999.999
              );

foreach($product_prices as $prod => $price) {

    echo "Product: ".$prod." \n";
    echo "Price: ".$price." &lt;p>";

}</pre>
<p>Here we got an array with 3 indexes which indicate products (the name of the products are the<br />
&#8216;keys&#8217; of the array) with their prices (the &#8216;values&#8217; of the array). So I changed the variables $key and $value to the names $prod (product) and $price, which are in this case giving a better indication of what actually is stored into them. The output of this script is a list of the products and prices:</p>
<blockquote><p>Product: tv<br />
Price: 599.95</p>
<p>Product: computer<br />
Price: 899.95</p>
<p>Product: notebook<br />
Price: 999.999</p></blockquote>
<p>Cheers,<br />
Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/foreach-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>For Loop</title>
		<link>http://www.webcodez.net/php-mysql/for-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/for-loop/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 09:54:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/wordpress/?p=100</guid>
		<description><![CDATA[Then we’ve got the for loop. This loop is in some point of view kind of smilar to the while loop as it as well repeats a certain code several times. Though, the for loop requires a variable to be set, an increment value (or decrement) and a condition, as argruments of the loop. A [...]]]></description>
			<content:encoded><![CDATA[<p>Then we’ve got the for loop. This loop is in some point of view kind of smilar to the while loop as it as well repeats a certain code several times. Though, the for loop requires a variable to be set, an increment value (or decrement) and a condition, as argruments of the loop. A basic example of a for loop looks like this (structure) :</p>
<pre name="code" class="php:nogutter">
for ( set variable; condition; action increment/decrement ) {

            ...act code...

}
</pre>
<p>Say we want to set a begin value (we use the variable named $i, which is a common name for integer variables to be used) to 0 and we want to let it count to 10.</p>
<p>Example:</p>
<pre name="code" class="php:nogutter">
for ( $i = 0; $i <= 10; $i++ ) {

            echo $i;

}
</pre>
<p>The variable set is $i, which we set to 0. The condition for the code to be ran is that $i is smaller than or equal to 10. So as long as this is the case, the value of $i will be shown on the screen (echo $i). Each time it shows $i on the screen, $i will be increased by 1 ($i++) and the loop will be repeated as long as the condition ($i<=10) is still true. In other words: it shows the value of $i on the screen, increases it by 1, shows it on the screen again, etc., and repeats this until $i is equal to 10.</p>
<p>Output:</p>
<blockquote><p>
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10
</p></blockquote>
<p>You could also make it count downwards:</p>
<pre name="code" class="php:nogutter">
for ( $i = 10; $i => 0; $i--) {

            echo $i;

}
</pre>
<p>You see we now set $i (begin value) to 10 and set the condition to $i is larger or equal to 0, and the action to repeat each time the loop is executed to $i--(which means: decrease $i by 1).</p>
<p>Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>While Loop</title>
		<link>http://www.webcodez.net/php-mysql/while-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/while-loop/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 10:02:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/wordpress/php-mysql/while-loop/</guid>
		<description><![CDATA[This loop can be used to repeat a code several times untill the condition of the loop is not true anymore. The strucuture of a while loop looks like this:

while( ... condition ... )
    ... act code ...
}
So, while the condition between brackets is still true, it will keep repeating the &#8216;act [...]]]></description>
			<content:encoded><![CDATA[<p>This loop can be used to repeat a code several times untill the condition of the loop is not true anymore. The strucuture of a while loop looks like this:</p>
<pre name="code" class="php:nogutter">
while( ... condition ... )
    ... act code ...
}</pre>
<p>So, while the condition between brackets is still true, it will keep repeating the &#8216;act code&#8217; (you can put any valid php code there). Each time it has executed the &#8216;act code&#8217; , it will go back to the first line of the loop and check whether the condition is still true. If this is the case, it will repeat the code, if not: the loop will stop (break) and the script will continue with the next following code, skipping the loop code this time. A simple example:</p>
<pre name="code" class="php:nogutter">&lt;?php
$start = 0;
$end = 10;
$counter = $start;

while($counter &lt;= $end) { 

echo "counter: ".$counter;
     $counter++; 

}

?&gt;</pre>
<p>This code sets a variable $counter equal to a start value ( 0 ) and then executes a loop which is repeated as long as the value of the counter is less than the end value ( set in variable $end = 10 ). Then it shows the current value of the counter and increases the counter value by one. It repeats the while loop as long as the $counter value is still smaller than or equal to the $end value. The result of this while loop would be:</p>
<blockquote><p>counter: 0<br />
counter: 1<br />
counter: 2<br />
counter: 3<br />
counter: 4<br />
counter: 5<br />
counter: 6<br />
counter: 8<br />
counter: 9<br />
counter: 10</p></blockquote>
<p>It counts to 10!<br />
Another good use of the while loop is for getting all results from the database. I won&#8217;t go in detail here as MySql queries aren&#8217;t discussed yet. But in that case the condition puts the first result it finds from a query, into a variable. Then it will execute the &#8216;act code&#8217; and then check the condition again: it will check if there are any more results from the database, if so , it will put the next found result in the variable and execute the &#8216;act code&#8217; again but then with that new found result as value of the variable, etc..<br />
The code looks like this (just an example) :</p>
<pre name="code" class="php:nogutter">
while ( $result = msql_fetch_assoc($query) ) {
//while there are still results to be handled from the query
    echo $result['message']; //for example: show the value of the field 'message' from this result
}</pre>
<p>This can be used to (for example) show all messages for forums, guestbooks, etc..<br />
Now we&#8217;ve come to the end of this chapter. In the next chapter we&#8217;ll be discussing the for loop, which can as well be used instead of the while loop in several cases.</p>
<p>Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/while-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic IF Loop</title>
		<link>http://www.webcodez.net/php-mysql/basic-if-loop/</link>
		<comments>http://www.webcodez.net/php-mysql/basic-if-loop/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 09:48:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[if loop]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/wordpress/php-mysql/basic-if-loop/</guid>
		<description><![CDATA[Note: everything put between &#8230; and &#8230; is NOT an actual code but are just &#8216;comments&#8217; that indicate what should be put in there.
Loops are very useful and used a lot in PHP. They&#8217;re used to check conditions and act based on whether the condition checked was true or false. A basic loop to do [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note:</strong> everything put between &#8230; and &#8230; is NOT an actual code but are just &#8216;comments&#8217; that indicate what should be put in there.</p>
<p>Loops are very useful and used a lot in PHP. They&#8217;re used to check conditions and act based on whether the condition checked was true or false. A basic loop to do this is the If loop.As mentioned above a loop consists of a condition and an &#8216;act&#8217; (what if the condition is true? What if the condition is false?). A basic if loop looks like this:</p>
<pre name="code" class="php:nogutter">
If( ... condtion ... ) {
  ...act when true ...
} else {
  ... act when false ...
}</pre>
<p>You can see the condition is put between the brackets, and the act is between { and }. The &#8216;act&#8217; of the else part of the loop runs when the condition was false. When you don&#8217;t want the system to do anything if the condition was false, then you can just leave out the else part, so you&#8217;d get:</p>
<pre name="code" class="php:nogutter">
If( ... condtion ... ) {
  ... act when true ...
}</pre>
<p>Ok so let&#8217;s actually use this. Take for example that you sell products on your website, and members have their own balance which represents the money they own. Now when they are going to use this balance to buy a product, you will need to check whether his balance is high enough to buy that product. Therefore an if-loop is needed! A simplified example :</p>
<pre name="code" class="php:nogutter">
&lt;?php

$price = 10.00;
$balance = 11.95;

If ( $price > $balance ) {

echo "Sorry, your balance is not high enough to buy this product.";
exit(); //cancel the process

}else{

echo "Product bought!";
$balance = $balance-$price; //set the new balance

}

?></pre>
<p>In this case $price contains the price of the product and $balance the amount of money the user<br />
owns on his account. The if loop now checks the condition $price > $balance, which means: $price is<br />
greater than $balance. When this condition is true (when the price is higher than the user’s balance),<br />
the user can not afford to buy the product and the error message is shown – the process is canceled.<br />
However when the price is not higher than the balance (so the user’s balance is high enough to buy<br />
the product) the product is bought and the new balance is set (the price is reducted from the<br />
balance).<br />
So, In this case the above script would return:</p>
<blockquote><p>Product bought!</p></blockquote>
<p>And after the loop, the variable $price would contain the value: 1.95<br />
Here we used the operator > to check whether a value was greater than another variable, but of<br />
course there are more operators to use in a loop. Here’s a list of them and their meanings:</p>
<blockquote><p>== Is equal to<br />
!= Is not equal to<br />
= Set equal to<br />
> Greater than<br />
=> Equal to or greater than<br />
< Smaller than<br />
<= Equal to or smaller than</p></blockquote>
<p><strong>Alert:</strong> Don’t ever use the = operator to CHECK a condition, it’s used to SET variables equal to values.</p>
<p>Admin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/basic-if-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
