php独特的语法
今天写一个程序的时候遇到一个很有意思的问题,这个和php独特的语法有关,首先我们看一下代码是怎么写的。
<?php $db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to databas'); mysql_select_db('moviesite',$db) or die(mysql_error($db)); if(!isset($_GET['do']) || $_GET['do'] != 1) { switch($_GET['type']) { case 'movie': echo 'Are you sure you want to delete this movie?<br />'; break; case 'people': echo 'Are you sure you want to delete this people?<br />'; default: break; } echo '<a href="'.$_SERVER['REQUEST_URI'].'&do=1">Yes</a> or <a href="admin.php">no</a>'; } else { switch($_GET['type']) { case 'people': $query = ' update people set movie_leadactor = 0 where movie_leadactor = '.$_GET['id']; $result = mysql_query($query , $db) or die(mysql_error($db)); $query = ' delete from people where people_id='.$_GET['id']; $result = mysql_query($query , $db) or die(mysql_error($db)); ?> <p style="text-align:'center'">Your person has been deleted</p> <a href="admin.php">Return to Index</a> <?php break; case 'movie': $query = 'delete from movie where movie_id='.$_GET['id']; $result = mysql_query($query , $db) or die (mysql_error($db)); ?> <p style="text-align:'center'">Your movie has been deleted</p> <a href="admin.php">Return to Index</a> <?php break; default: break; } } ?>
注意这里的switc语句,在删除movie或people之后我们要输出成功信息并返回列表页面,一般情况下我们习惯把break语句紧跟在switch下面的主体部分,于是开始的时候我写成这样了
else { switch($_GET['type']) { case 'people': $query = ' update people set movie_leadactor = 0 where movie_leadactor = '.$_GET['id']; $result = mysql_query($query , $db) or die(mysql_error($db)); $query = ' delete from people where people_id='.$_GET['id']; $result = mysql_query($query , $db) or die(mysql_error($db)); break; ?> <p style="text-align:'center'">Your person has been deleted</p> <a href="admin.php">Return to Index</a>
把break放在了case下面的语句下面,这样会导致不能输出删除成功信息,
<p style="text-align:'center'">Your pers
<a href="admin.php">Return to Index</a>
这两句是可以当成case语句的一部分的,如果把它放在外面将永远也不会执行,所以要break要放在下一个case的开头,将上面两行html片段放在case语句中,这样就可以正常显示删除成功的信息了。
php面条语句的特性在这里表现的淋漓尽致。
作者:Tyler Ning
出处:http://www.cnblogs.com/tylerdonet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,请微信联系冬天里的一把火