Cursussen/Courses Codesnippets     Top 
PHP - Structures


1. If...elseif...else
By specifying a condition after the if statement, you can execute a code block if the condition is met. You can use a "boolean" variable for this or test the contents of a variable for a certain value with a comparison operator.
The elseif part allows you to test additional conditions if the previous condition was not met.
The else part then serves to execute statements if the previous conditions are not met.
if (($tel % $test_getal) == 0) {
     $is_priemgetal = FALSE;
}
// schrikkeljaar test
if (date("L",$timestamp)) {
	$aantaldagen = 29 ;
} else {
	$aantaldagen = 28;
}


2. Switch... case... default
With the switch statement you can test the value of a variable in a comprehensive way.
The case part indicates the test value followed by the statements to be executed if the test value is met. If the test value does not match, the following cases are run until a match is found. With the reserved word “break” you can prevent the other cases from being tested if the test value is met.
The default part is executed if no test values match.
switch ($maandnr) {
	case '1':
		return "Januari";
		break;
	case '2':
		return "Februari";
		break;
	case '3':
		return "Maart";
		break;
	case '4':
		return "April";
		break;
	case '5':
		return "Mei";
		break;
	case '6':
		return "Juni";
		break;
	case '7':
		return "Juli";
		break;
	case '8':
		return "Augustus";
		break;
	case '9':
		return "September";
		break;
	case '10':
		return "Oktober";
		break;
	case '11':
		return "November";
		break;
	case '12':
		return "December";
		break;
	default:
		return "";
}


3. For
The for statement allows you to execute a series of statements a predetermined number of times.
You first specify a start condition, then the condition that will determine when the loop ends, and then the statement that will be executed if the condition is "True".
<?php
echo "<select name='jaar'>";
for ($j = 1900; $j <= date('Y'); $j++) {
    echo "<option value='" . $j . "'>" . $j . "</option>";
}
echo '</select>';


4. Foreach
This structure applies to arrays.
The foreach statement can be used in two ways:
- with the variable for the array and a variable for the value of the element of the array.
- with the variable for the array and a variable for the key and a variable for the value of the element of the array.
foreach($item as $sleutel => $waarde) {
	echo "<td>" . $waarde ."</td>";
}


5. While
As long as the specified condition is met, the statements of the while loop will be executed.
The condition is tested first.
echo "<select name='dag'><option>Kies</option>\n";
$dag = 1;
while ($dag <= 31) {
	echo "<option value=$dag>$dag</option>\n";
	$dag++;
}
echo "</select>\n";


6. Do...while
A block of code is first executed and then it is tested whether the condition is met to stop executing the statements.
<?php
$startwaarde = 0;
$eindwaarde = 9;
$waarde = $startwaarde;
do {
	echo "Huidige waarde is $waarde <br />";
	$waarde++;
	// voortijdig de lus verlaten
	if ($waarde == 7) break;
} while($waarde <= $eindwaarde)