Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
Make decisions and repeat work with if, switch, for, foreach and while.
<?php
$score = 72;
if ($score >= 90) {
echo 'A';
} elseif ($score >= 60) {
echo 'Pass';
} else {
echo 'Fail';
}Use === (same value and same type) instead of ==. It avoids surprises like '0' == false.
switch ($day) {
case 'sat':
case 'sun':
echo 'Weekend';
break;
default:
echo 'Weekday';
}for ($i = 1; $i <= 3; $i++) { echo $i; } // 123
$fruits = ['apple', 'mango'];
foreach ($fruits as $f) { echo $f, ' '; }
$n = 3;
while ($n > 0) { echo $n--; } // 321foreach is the loop you will use most, for arrays and database rows.break to leave a loop early and continue to skip to the next round.while loop can end, otherwise the page hangs.Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.