In PHP is there a way to access the value that I assign to a variable in one case block of a switch statement in another case block in that same switch statement? For instance, I want to access the value I assigned to $task_to_modify in the 'Modify Task' case block in the 'Save Changes' case block below. Here's my code. Any advice would help as I'm new to PHP. Thanks.
$task_to_modify
switch( $_POST['action'] ) {
case 'Add Task':
$new_task = $_POST['newtask'];
if (empty($new_task)) {
$errors[] = 'The new task cannot be empty.';
} else {
//$task_list[] = $new_task; //original code
array_push($task_list, $new_task);
}
break;
case 'Delete Task':
$task_index = $_POST['taskid'];
unset($task_list[$task_index]);
$task_list = array_values($task_list);
break;
case 'Modify Task':
global $key;
$key = $_POST['taskid']; //taskid is index of selected task from tasks selection list
global $task_to_modify;
$task_to_modify = $task_list[$key];
set_task_to_modify($task_to_modify);
echo "modify task case reached. task_to_modify is " . $task_to_modify;
break;
case 'Save Changes':
global $task_to_modify;
if(in_array($new_task, $task_list)) { //if old value is in the list
$index = array_search($new_task, $task_list); //return index # of old value
$task_list[$index] = $task_to_modify; //replaces old value with newly entered value
unset($task_to_modify); //deletes var so original form will show
}
break;
case 'Cancel Changes': //works correctly
echo 'no changes saved';
unset($task_to_modify);
break;
}