What are cycles? Commands repeated more than once. Is it usefull to copy/paste the same functions more than once? No.
PHP is able to avoid this, with these functions: for, while, switch, foreach.

So let’s start with an example:

<?php

echo "Number 1<br />";
echo "Number 2<br />";
echo "Number 3<br />";
echo "Number 4<br />";
echo "Number 5<br />";
echo "Number 6<br />";
echo "Number 7<br />";
echo "Number 8<br />";
echo "Number 9<br />";

?>
<?php
for($i = 0; $i < 9; $i++)
	echo "Number $i<br />";
?>

The two codes up here are exactly the same. The first one is a repeated action, the second one is the same repetition, but it’s less chaotic and better readable. This is the function for. As all the cycle functions it doesn’t require any graphs if the functions that must be executed are one, elsewise you’ll have to delimit the functions of the cycle in graphs {}. This is an example:

<?php
for($i = 0; $i < 9; $i++) {
	echo "Number $i<br />";
	$array[] = $i;
}
?>

Clear? Great.

Returning to the cycle functionfor: it’s used when you know the exact number of iterations. It requires three parameters: the starting conditional, the ending conditional and the action taken at each iteration. In the example above $i is initialised to 0, then the cycle will go on untill $i will be inferior to 9, and at each iteration $i will be incremented by one (by the way $n++ will firstly return the value of $n and then increment the variable, ++$n will do the same but in the inverse order. Think about $array[$i++] and $array[++$i]).

Well, after the for cycle, here it is the while one. It can be used in two different ways:

<?php
	$found = false;
	while($found == false) {
		if( // condition //) $found = true;
		else // do something else //
	}

	do {
		// some code here
	} while($found == false);
?>

As you can see, the while cycle will continue untill the parameter will return TRUE (if $found is false, “$found == false” will return true!). In the first case there may be no iteration at all (if $found is true from the beginning, no iteration will be done). In the second case there will be at least one iteration, because the check is at the end of the cycle and not at the beginning.
ATTENTION: the while cycle (as all the cycles) may go in a “loop”, which is a state in which the cycle will run forever. PHP is designed to stop this kind of scripts, but it may load your CPU and RAM before stopping.

switch: is used to execute some code depending on a variable’s value. An example is far more easy to understand:

<?php
	$myvar = //something dynamically got //;
	switch($myvar) {
		case 0:
			// do something //
			break;
		case 1:
			// do something else //
			break;
		default:
			// if none of the cases are respected,
			// the switch cycle will execute this part.
			break;
	}
?>

Let’s start explaining the code: the structure is switch($variable) { cases here }. Cases may be all the types of variables but not arrays, so numbers (integers, floats…), strings (single characters, phrases) and so on. There are no graphs delimiting the cases, but it’s used the function “break”, which will break indeed the cycle. If you leave the cases without the break in the end, all the functions under the “succesfull” case will be run (which isn’t what you would expect from a switch cycle). The default case is optional, and it’s the code executed when the value is not one of the scripted cases. In this case, whenever of the default case should miss, nothing will be executed.

foreach: used to iterate for each element of a pluri-dimensional array. This is an example:

<?php
	$data = array(
		0 => array(
			"var1" => "a",
			"var2" => "b",
			"var3" => 6,
		),
		1 => array(
			"var1" => "g",
			"var2" => "T",
			"var3" => 0,
		)
	);

	foreach($data as $case) {
		echo $case['var1'];
	}
?>

The syntax is foreach($variable as $newname). In this way you’ll be able to access to the elements of an array variable in a more confortable way instead of doing some maybe difficult while or for cycles (in case of strings as array indexing –> $array["my index 1"]). In this way $newname will be a new array (or value in case of mono-dimensional arrays), which will have the same behaviour of the mother array, but it will have the values of the internal arrays and not all of them. It’s normally used to access to XML files (see Data storage: read an XML file for further informations about it).

For example here it is what $newname will be at the first iteration:

$newname = array(
	"var1" => "a",
	"var2" => "b",
	"var3" => 6,
);

Which is the sub-array with index 0 of the mother $data.

It’s the most complicated cycle to understand, but indeed is pretty simple when you’ve understood it.