Heim >Backend-Entwicklung >PHP-Tutorial >php变量访问权限问题?
方法代码:
<code>function deleteSegment($vsuuid) { $num = db("test")->exec("DELETE FROM t_segment_index WHERE vs_uuid='$vsuuid'"); if($num == 1) { $del = db("test")->exec("DELETE FROM t_video_segment WHERE vs_uuid='$vsuuid'"); if($del == 1){ $vsu = $vsuuid; $videoMsg = db("test")->getQuery("SELECT * FROM `t_video` where v_segments_list like '%$vsuuid%' limit 1"); if(empty($videoMsg)) return $del; $segments = $videoMsg[0]["v_segments_list"]; $v_uuid = $videoMsg[0]["v_uuid"]; $oldSegmentArray = explode("\n", $segments); $newSegmentArray = array_filter($oldSegmentArray, function($s){ if($s === $vsuuid) return false; else return true; }); $newSegments = implode("\n", array_values($newSegmentArray)); $updateV = db("test")->exec("UPDATE t_video SET v_segments_list = '$newSegments' WHERE v_uuid='$v_uuid'"); return 1; } else return 0; } else return 0; } </code>
代码执行到if($s === $vsuuid)的时候报错如下:
Notice: Undefined variable: vsuuid in /Applications/XAMPP/xamppfiles/htdocs/waaa-web/runtime/service_Admin.php on line 1526
请问是什么原因导致的?该怎么解决呢?
方法代码:
<code>function deleteSegment($vsuuid) { $num = db("test")->exec("DELETE FROM t_segment_index WHERE vs_uuid='$vsuuid'"); if($num == 1) { $del = db("test")->exec("DELETE FROM t_video_segment WHERE vs_uuid='$vsuuid'"); if($del == 1){ $vsu = $vsuuid; $videoMsg = db("test")->getQuery("SELECT * FROM `t_video` where v_segments_list like '%$vsuuid%' limit 1"); if(empty($videoMsg)) return $del; $segments = $videoMsg[0]["v_segments_list"]; $v_uuid = $videoMsg[0]["v_uuid"]; $oldSegmentArray = explode("\n", $segments); $newSegmentArray = array_filter($oldSegmentArray, function($s){ if($s === $vsuuid) return false; else return true; }); $newSegments = implode("\n", array_values($newSegmentArray)); $updateV = db("test")->exec("UPDATE t_video SET v_segments_list = '$newSegments' WHERE v_uuid='$v_uuid'"); return 1; } else return 0; } else return 0; } </code>
代码执行到if($s === $vsuuid)的时候报错如下:
Notice: Undefined variable: vsuuid in /Applications/XAMPP/xamppfiles/htdocs/waaa-web/runtime/service_Admin.php on line 1526
请问是什么原因导致的?该怎么解决呢?
<code>array_filter($oldSegmentArray, function($s) use($vsuuid) { return $s !== $vsuuid; }); </code>
顺便帮你改下下函数...
<code>function deleteSegment($vsuuid) { $res = db("test")->exec("DELETE FROM t_segment_index, t_video_segment WHERE vs_uuid='$vsuuid'"); if( !$res ) return 0; $videoMsg = db("test")->getQuery("SELECT * FROM `t_video` where v_segments_list like '%$vsuuid%' limit 1"); if( empty($videoMsg) ) return 0; $segments = $videoMsg[0]["v_segments_list"]; $v_uuid = $videoMsg[0]["v_uuid"]; $oldSegmentArray = explode("\n", $segments); $newSegmentArray = array_filter($oldSegmentArray, function($s) use($vsuuid) { return $s !== $vsuuid; }); $newSegments = implode("\n", array_values($newSegmentArray)); return $updateV = db("test")->exec("UPDATE t_video SET v_segments_list = '$newSegments' WHERE v_uuid='$v_uuid'"); } </code>
闭包(Closure) 中使用外部函数,function($param) use($otherParam){}