普通网友 2014-10-27 22:44
浏览 45
已采纳

如何检索我在PHP中的另一个交换机案例块中的一个交换机案例块中为变量分配的值?

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;

}

  • 写回答

1条回答 默认 最新

  • doujiao9574 2014-10-27 22:48
    关注

    PHP has no scope for each block. So the variable is available in the 'modify task' block and in the 'save changes' block, since they share the same scope. You might also take a look here to understand it a little bit better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。