PHP: Decisions - Other

Example: Decisions in PHP


Example: Switch statement

Many languages also support the switch statement. PHP does as well. Below is an example


	<?php
         $day = date("D");
         
         switch ($day){
            case "Mon":
               echo "<p>Time to go back to work.</p>";
               break;
            
            case "Tue":
               echo "<p>Professioanl development day today.</p>";
               break;
            
            case "Wed":
               echo "<p>Your department meeting is today.</p>";
               break;
            
            case "Thu":
               echo "<p>Pizza night tonight.</p>";
               break;
            
            case "Fri":
               echo "<<p>TGIF.</p>";
               break;
            
            case "Sat":
               echo "<p>Have a great weekend.</p>";
               break;
            
            case "Sun":
               echo "<p>Time to relax.</p>";
               break;
      
         }
	?>
	

Run it now


Example: Ternary operator logic

Many languages also have what is known as a ternary operator logic. This is a shortcut method to performing if logic and often involves the use of a ? mark. Below is an example of an if-else in PHP using this. As is often the case with programming, shortcuts made code shorter and more efficient, but conversely it can be more difficult to read unless someone is well versed in the syntax.


	<?php
		$temp = 4;
		echo "<p>The temperature out today is ",($temp >= 32 ? "above freezing.</p>" : "below freezing.</p>"); 
	?>
	

Run it now