欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php独特的语法

程序员文章站 2022-05-12 11:33:57
...

今天写一个程序的时候遇到一个很有意思的问题,这个和php独特的语法有关,首先我们看一下代码是怎么写的。 ? php $db = mysql_connect ('localhost','root','Ctrip07185419') or die ('can not connect to databas' ); mysql_select_db ('moviesite', $db )

今天写一个程序的时候遇到一个很有意思的问题,这个和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?
'; break; case 'people': echo 'Are you sure you want to delete this people?
'; default: break; } echo 'Yes or no'; } 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)); ?>

Your person has been deleted

Return to Index php break; case 'movie': $query = 'delete from movie where movie_id='.$_GET['id']; $result = mysql_query($query , $db) or die (mysql_error($db)); ?>

Your movie has been deleted

Return to Index 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;            
                    
?>

Your person has been deleted

Return to Index

把break放在了case下面的语句下面,这样会导致不能输出删除成功信息,

Your pers
Return to Index

这两句是可以当成case语句的一部分的,如果把它放在外面将永远也不会执行,所以要break要放在下一个case的开头,将上面两行html片段放在case语句中,这样就可以正常显示删除成功的信息了。

php面条语句的特性在这里表现的淋漓尽致。