Advertisement
andybeak

Loops and logic branches

Feb 26th, 2025
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.21 KB | None | 0 0
  1. <?php
  2. /*
  3.  * PHP LOOPS AND LOGIC BRANCHES
  4.  * This file demonstrates all the different types of loops and logic branches in PHP
  5.  */
  6.  
  7. echo "==========================================\n";
  8. echo "IF/ELSE STATEMENTS\n";
  9. echo "==========================================\n";
  10.  
  11. // Basic if statement
  12. $condition = true;
  13. if ($condition) {
  14.     echo "This will be executed because the condition is true\n";
  15. }
  16.  
  17. // If/else statement
  18. $temperature = 25;
  19. if ($temperature > 30) {
  20.     echo "It's hot outside\n";
  21. } else {
  22.     echo "It's not that hot\n";
  23. }
  24.  
  25. // If/elseif/else statement
  26. $score = 85;
  27. if ($score >= 90) {
  28.     echo "Grade: A\n";
  29. } elseif ($score >= 80) {
  30.     echo "Grade: B\n";
  31. } elseif ($score >= 70) {
  32.     echo "Grade: C\n";
  33. } elseif ($score >= 60) {
  34.     echo "Grade: D\n";
  35. } else {
  36.     echo "Grade: F\n";
  37. }
  38.  
  39. // Ternary operator (short if/else)
  40. $age = 20;
  41. $canVote = ($age >= 18) ? "Yes" : "No";
  42. echo "Can vote: $canVote\n";
  43.  
  44. // Null coalescing operator (PHP 7+)
  45. $username = null;
  46. $displayName = $username ?? "Guest";
  47. echo "Hello, $displayName\n";
  48.  
  49. // Nested if statements
  50. $isLoggedIn = true;
  51. $isAdmin = true;
  52. if ($isLoggedIn) {
  53.     echo "User is logged in\n";
  54.     if ($isAdmin) {
  55.         echo "User has admin privileges\n";
  56.     } else {
  57.         echo "User has regular privileges\n";
  58.     }
  59. } else {
  60.     echo "User is not logged in\n";
  61. }
  62.  
  63. echo "\n==========================================\n";
  64. echo "SWITCH STATEMENTS\n";
  65. echo "==========================================\n";
  66.  
  67. // Switch statement
  68. $dayOfWeek = 3;
  69. switch ($dayOfWeek) {
  70.     case 1:
  71.         echo "Monday\n";
  72.         break;
  73.     case 2:
  74.         echo "Tuesday\n";
  75.         break;
  76.     case 3:
  77.         echo "Wednesday\n";
  78.         break;
  79.     case 4:
  80.         echo "Thursday\n";
  81.         break;
  82.     case 5:
  83.         echo "Friday\n";
  84.         break;
  85.     case 6:
  86.     case 7:
  87.         echo "Weekend\n";
  88.         break;
  89.     default:
  90.         echo "Invalid day\n";
  91.         break;
  92. }
  93.  
  94. // Switch with fall-through (no break)
  95. $month = "Feb";
  96. switch ($month) {
  97.     case "Jan":
  98.         echo "31 days\n";
  99.         break;
  100.     case "Feb":
  101.         echo "28 or 29 days\n";
  102.         break;
  103.     case "Mar":
  104.         echo "31 days\n";
  105.         break;
  106.     case "Apr":
  107.         echo "30 days\n";
  108.         break;
  109.     // ... more cases
  110. }
  111.  
  112. echo "\n==========================================\n";
  113. echo "MATCH EXPRESSION (PHP 8+)\n";
  114. echo "==========================================\n";
  115.  
  116. // Match expression (PHP 8+)
  117. // Will only work if you're running PHP 8 or higher
  118. $statusCode = 404;
  119. /*
  120. $message = match($statusCode) {
  121.     200, 201 => "Success",
  122.     400 => "Bad request",
  123.     401 => "Unauthorized",
  124.     403 => "Forbidden",
  125.     404 => "Not found",
  126.     default => "Unknown status code"
  127. };
  128. echo "HTTP Status: $message\n";
  129. */
  130. // For PHP 7 and below, use switch statement instead
  131. $message = "";
  132. switch($statusCode) {
  133.     case 200:
  134.     case 201:
  135.         $message = "Success";
  136.         break;
  137.     case 400:
  138.         $message = "Bad request";
  139.         break;
  140.     case 404:
  141.         $message = "Not found";
  142.         break;
  143.     default:
  144.         $message = "Unknown status code";
  145. }
  146. echo "HTTP Status: $message\n";
  147.  
  148. echo "\n==========================================\n";
  149. echo "FOR LOOPS\n";
  150. echo "==========================================\n";
  151.  
  152. // Basic for loop
  153. echo "Basic for loop:\n";
  154. for ($i = 0; $i < 5; $i++) {
  155.     echo "Iteration $i\n";
  156. }
  157.  
  158. // For loop with multiple expressions
  159. echo "\nFor loop with multiple expressions:\n";
  160. for ($i = 0, $j = 10; $i < 5; $i++, $j--) {
  161.     echo "i = $i, j = $j\n";
  162. }
  163.  
  164. // For loop with break
  165. echo "\nFor loop with break:\n";
  166. for ($i = 0; $i < 10; $i++) {
  167.     if ($i == 5) {
  168.         break; // Exit the loop when $i is 5
  169.     }
  170.     echo "Iteration $i\n";
  171. }
  172.  
  173. // For loop with continue
  174. echo "\nFor loop with continue:\n";
  175. for ($i = 0; $i < 10; $i++) {
  176.     if ($i % 2 == 0) {
  177.         continue; // Skip even numbers
  178.     }
  179.     echo "Odd number: $i\n";
  180. }
  181.  
  182. echo "\n==========================================\n";
  183. echo "FOREACH LOOPS\n";
  184. echo "==========================================\n";
  185.  
  186. // Foreach loop with array
  187. $fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
  188. echo "Fruits list:\n";
  189. foreach ($fruits as $fruit) {
  190.     echo "- $fruit\n";
  191. }
  192.  
  193. // Foreach with key => value
  194. $person = [
  195.     "name" => "John Doe",
  196.     "age" => 30,
  197.     "email" => "john@example.com",
  198.     "location" => "New York"
  199. ];
  200. echo "\nPerson details:\n";
  201. foreach ($person as $key => $value) {
  202.     echo "$key: $value\n";
  203. }
  204.  
  205. // Foreach with reference (&) to modify values
  206. echo "\nModifying array values with foreach:\n";
  207. $numbers = [1, 2, 3, 4, 5];
  208. echo "Before: " . implode(", ", $numbers) . "\n";
  209. foreach ($numbers as &$number) {
  210.     $number *= 2; // Double each number
  211. }
  212. unset($number); // Unset the reference
  213. echo "After: " . implode(", ", $numbers) . "\n";
  214.  
  215. echo "\n==========================================\n";
  216. echo "WHILE LOOPS\n";
  217. echo "==========================================\n";
  218.  
  219. // Basic while loop
  220. echo "Basic while loop:\n";
  221. $count = 0;
  222. while ($count < 5) {
  223.     echo "Count: $count\n";
  224.     $count++;
  225. }
  226.  
  227. // While loop with break
  228. echo "\nWhile loop with break:\n";
  229. $count = 0;
  230. while (true) { // Infinite loop
  231.     echo "Count: $count\n";
  232.     $count++;
  233.     if ($count >= 5) {
  234.         break; // Exit the loop
  235.     }
  236. }
  237.  
  238. // While loop with continue
  239. echo "\nWhile loop with continue:\n";
  240. $count = 0;
  241. while ($count < 10) {
  242.     $count++;
  243.     if ($count % 2 == 0) {
  244.         continue; // Skip even numbers
  245.     }
  246.     echo "Odd count: $count\n";
  247. }
  248.  
  249. echo "\n==========================================\n";
  250. echo "DO-WHILE LOOPS\n";
  251. echo "==========================================\n";
  252.  
  253. // Basic do-while loop
  254. echo "Basic do-while loop:\n";
  255. $count = 0;
  256. do {
  257.     echo "Count: $count\n";
  258.     $count++;
  259. } while ($count < 5);
  260.  
  261. // Do-while that executes at least once
  262. echo "\nDo-while with condition that would fail immediately in a while loop:\n";
  263. $count = 10;
  264. do {
  265.     echo "This will execute once even though count is already 10\n";
  266.     $count++;
  267. } while ($count < 5);
  268.  
  269. echo "\n==========================================\n";
  270. echo "NESTED LOOPS\n";
  271. echo "==========================================\n";
  272.  
  273. // Nested for loops (creating a simple multiplication table)
  274. echo "Multiplication table (3x3):\n";
  275. for ($i = 1; $i <= 3; $i++) {
  276.     for ($j = 1; $j <= 3; $j++) {
  277.         $product = $i * $j;
  278.         echo "$i × $j = $product\n";
  279.     }
  280.     echo "-----\n";
  281. }
  282.  
  283. // Nested loop with break/continue
  284. echo "\nNested loops with break and continue:\n";
  285. for ($i = 0; $i < 3; $i++) {
  286.     echo "Outer loop $i:\n";
  287.     for ($j = 0; $j < 5; $j++) {
  288.         if ($j == 1) {
  289.             continue; // Skip when $j is 1
  290.         }
  291.         if ($j == 4) {
  292.             break; // Exit inner loop when $j is 4
  293.         }
  294.         echo "  Inner loop $j\n";
  295.     }
  296. }
  297.  
  298. // Breaking out of nested loops with labels (PHP 8+)
  299. echo "\nLabels can be used to break out of nested loops (PHP GOTO):\n";
  300.  
  301. for ($i = 0; $i < 3; $i++) {
  302.     echo "Outer loop $i:\n";
  303.     for ($j = 0; $j < 3; $j++) {
  304.         echo "  Inner loop $j\n";
  305.         if ($i == 1 && $j == 1) {
  306.             echo "  Breaking out of all loops\n";
  307.             goto end_of_loops; // Break out of both loops
  308.         }
  309.     }
  310. }
  311. end_of_loops:
  312. echo "Loops have ended\n";
  313.  
  314. echo "\n==========================================\n";
  315. echo "ALTERNATIVE SYNTAX (used in templates)\n";
  316. echo "==========================================\n";
  317.  
  318. // Alternative syntax for if statements
  319. $showSection = true;
  320. if ($showSection): ?>
  321.     This is HTML that would be shown if $showSection is true.
  322. <?php else: ?>
  323.     This is HTML that would be shown if $showSection is false.
  324. <?php endif;
  325.  
  326. // Alternative syntax for loops
  327. $colors = ["Red", "Green", "Blue"];
  328. ?>
  329.  
  330. <ul>
  331. <?php foreach ($colors as $color): ?>
  332.     <li><?php echo $color; ?></li>
  333. <?php endforeach; ?>
  334. </ul>
  335.  
  336. <?php
  337. // Alternative switch syntax
  338. $userRole = "admin";
  339. switch ($userRole):
  340.     case "admin":
  341.         echo "Welcome, Administrator!";
  342.         break;
  343.     case "editor":
  344.         echo "Welcome, Editor!";
  345.         break;
  346.     default:
  347.         echo "Welcome, User!";
  348.         break;
  349. endswitch;
  350. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement