Tuesday, April 22, 2025

PHP Switch, Case, Break, Default

You are advised to check corresponding YouTube video at the end of this article.

In programming, switch is a control structure that allows a program to execute different code based on the value of a variable or expression. The switch statement is followed by one or more case statements, which define the different values that the variable or expression may take. Each case statement contains the code that should be executed if the variable or expression has the corresponding value.

The break keyword is used inside each case statement to terminate the switch statement and prevent the execution of subsequent case statements. Without break, execution will continue with the next case statement until a break or the end of the switch block is reached.

The default keyword is used to define the code that should be executed if none of the case statements match the variable or expression. It is like a fallback option or a default value.

In summary, switch allows the program to evaluate a variable or expression and execute different code depending on its value, case is used to define the different possible values, break is used to terminate the switch statement, and default is used as a fallback option.

form.html 


<form action="grabthings.php" method="post">
<label>First Number:</label>
<input type="text" name=first>
<br><br>

<label>Second Number:</label>
<input type="text" name=second>
<br><br>

<label>Operation:</label>
<input type="text" name=operation>
<br><br>

<input type="submit" value="Submit">
</form>

This form asks the user to input two numbers and an operation (addition, subtraction, multiplication or division).

grabthings.php


<?php 

$first = $_POST["first"];
$second = $_POST["second"];
$operation = $_POST["operation"];

switch ($operation) {
	case "+":
		echo "Addition: " . ($first + $second);
		break;
	case "-":
		echo "Substraction: " . ($first - $second);
		break;
	case "*":
		echo "Multiplication: " . ($first * $second);
		break;
	case "/":
		echo "Division: " . ($first / $second);
		break;
	
	default:
		echo "Weird Operation :)";
}

?>

This PHP script takes three inputs from a form submission: first, second, and operation. It performs an arithmetic operation based on the value of operation and the input numbers first and second.

The script uses a switch statement, which is a control structure used to select one of many code blocks to be executed based on the value of a given expression ($operation in this case).

Each code block is preceded by a case label which specifies the value that the expression must match in order for the block to be executed.

The break statement is used to end the current code block and move on to the next statement after the switch block. If no case label matches the value of the expression, the default block is executed.

In this script, the switch statement evaluates the value of operation and performs the corresponding arithmetic operation using first and second values. If operation is one of the four basic arithmetic operators (+, -, *, /), the script performs the corresponding operation and outputs the result.

If operation is not one of the basic operators, the script outputs the message "Weird Operation :)" using the default block.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...