PHP Foreach Loop

This item was filled under [ PHP & MySql, References ]

A total different kind of loop is the foreach loop. This loop is used to separately handle each sub-variable out of an array. Each sub-variable may be split into its name and its value.

foreach($array AS $key => $value) {
	... execute code ...
}

Each sub-variable of the array $array will be split into its key (stored into $key) and its value (stored into $value) and may be used in the code that’s to be executed ( between the accolades ). It’s a very efficient way to loop through all sub-variables of an array. Each sub-variable will have its key and value stored into separate variables and will go through the code between accolades. Example:

$products = new array(
			0 => “product example”,
			1 => “another product”
		         );
foreach($products as $id => $name) {
	echo “Product Id: “.$id.” <br />”;
	echo “Product Name: “.$name.” <p>”;
}

The output of this script will be:

Product Id: 0
Product Name: product example
Product Id: 1
Product Name: another product

Do realize that the variables inside the foreach loop ( between brackets ) may be given any names. It’s also possible to only use the values of the sub-variables if you don’t need the keys:

$products = new array(
			0 => “product example”,
			1 => “another product”
		         );
foreach($products as $name) {
	echo “Product Name: “.$name.” <br />”;
}

The output of this script will be:

Product Name: product example
Product Name: another product

Tagged with: [ , , , ]

PHP For Loop

This item was filled under [ PHP & MySql, References ]

The For loop is used to repeat executing a certain code for a specific amount of times. This could as well be done with the while loop however the for loop is made for this. The while loop may be used for much more complex stuff and for various other purposes. The for loop is sorely used for repeating.

for( … declaration …; … condition …; …action… ) {
	… execute code …
}

The arguments between brackets are 3: a declaration, a condition and an action. The declaration is usually an integer variable containing a start value, which is usually increased in the action. For example, we could make a for loop which is repeated 10 times:

for($i=1;$i<=10;$i++) {
	echo “<p>”.$i.”</p>”;
}

This will result in displaying the numbers 1 through 10.

Tagged with: [ , , , ]

PHP If Loop

This item was filled under [ PHP & MySql, References ]

The If Loop is a very basic loop and used often. It’s used to check a condition and execute a code based on whether the condition was true or not.

if( … condition … ) {
	… execute code …
}else{
	… execute other code …
}

The condition is put between brackets and the code between accolades. The first code is only executed when the condition between brackets is valid ( true ), otherwise the 2nd code is executed.
For example:

$num = 1;
if($num == 1) {
	echo “The variable num is equal to 1.”;
}else{
	echo “The variable num is not equal to 1.”;
}

The above code will return:

The variable num is equal to 1.

Because $num == 1 is true and so the first code(the if-part of the loop) is executed. If num was not equal to 1 the 2nd code would have been executed ( the else-part of the loop ).

If you only want to execute a code if the condition is true, and don’t do anything if it isn’t, then you can leave out the else-part. Like this:

//execute a code if $num == 1
if($num == 1) {
	echo “The variable num is equal to 1.”;
}
//otherwise do nothing

Sometimes, checking one condition is not enough. Therefore you can use else if -parts for your loop.

if($rank == “admin”) {
	echo “Welcome Admin!”;
}else if($rank == “moderator”) {
	echo “Welcome moderator!”;
}else if($rank == “guest”) {
	echo “You’re not allowed to be here, guest!”;
}

The above code, for example, has multiple conditions. If the first condition is true, it will execute the first code between accolades, if the second condition is true, it will execute the second code between accolades, etc.. Do notice it will check the conditions in the order of how they’re written down. So it will first check the first condition and if that condition is true, it will not check the other conditions in the rest of the else if -parts anymore. It will only execute ONE ( or none ) of the codes between accolades. When multiple conditions may be true and so you want to check all the conditions and make it possible to have multiple codes executed, then multiple separate if loops should be used instead of the else if-parts.

It’s also possible to have multiple comparisions within one condition. These are the operators that may be used:

[TABLE=12]

And for comparing:

[TABLE=13]

The difference between == and = is that == is used to compare variables/values, and = is used to set a variable equal to a value.

Tagged with: [ , , ]

PHP Loops

This item was filled under [ PHP & MySql, References ]

In PHP loops are used to execute codes under certain conditions and may also be used to repeat the code. There are different loops to do different kind of things:

If loop
The If loop is used to execute a certain code only when a certain condition is true.

While loop
The While loop is used to execute a certain code only when a condition is true, and repeat the code as long as the condition is still true.

Do While loop
The Do While loop is used to execute a certain code and repeat it as long as a condition is true. The difference with the while loop is that it executes the code at least once, then checks the condition for further repeating.

For loop
The For loop is used to repeat executing a code a certain amount of times. It does not only consist of a condition, but also a declaration of a variable and an action to perform. For example an integer would be declared and the action would be to increase the integer by 1 each time the loop’s code has been executed. Then a condition could be to execute the code as long as this variable is not greater than 10, to execute the code 10 times.

Foreach loop
This is a somewhat different kind of loop. It may be used to handle each sub-variable of an array apart, seperating the key and belonging value. This makes it easy to loop through all sub-variables ( all combinations of a key and a value ) of an array.

Each of these loops are explained inside apart references. Click on the title of the loop to get a reference of that specific loop.

PHP Variables

This item was filled under [ PHP & MySql, References ]

Like most other programming languages, PHP as well uses variables. Variables are meant to store data ( for example an integer or a string ) into. You can give them a name and a value. Once you call them anywhere in your script, the value that’s stored inside the variable is automaticly used. In PHP a variable is indicated with the dollar sign prefix.

$variable_name = variable_value;

The value of the variable can be of any type. Do notice that string values ( text ) should be put between quotes. Integers do not.

//so:
$some_integer = 10;

//… but:
$some_string = “Some String Text”;

When calling the variable, the combination of the dollar sign prefix, followed by the name of the variable, should be used. The value of the variable will be represented this way.

$i = 1;
echo “The value of the variable /$i is equal to “.$i;

When referring to variable’s values, the variable should be put outside of quotes ( separated by a dot ), otherwise the variable (name) will appear as text instead of the variable’s value, represented by the variable. In some cases single quotes may be used for variables as well.

Tagged with: [ , ]