首頁 > php教程 > php手册 > php独特的语法

php独特的语法

WBOY
發布: 2016-06-06 19:46:47
原創
1333 人瀏覽過

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

今天写一个程序的时候遇到一个很有意思的问题,这个和php独特的语法有关,首先我们看一下代码是怎么写的。

<span>php 
</span><span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to databas'<span>);
</span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));

</span><span>if</span>(!<span>isset</span>(<span>$_GET</span>['do']) || <span>$_GET</span>['do'] != 1<span>)
{
    </span><span>switch</span>(<span>$_GET</span>['type'<span>])
    {
        </span><span>case</span> 'movie':
        <span>echo</span> 'Are you sure you want to delete this movie?<br>'<span>;
        </span><span>break</span><span>;
        </span><span>case</span> 'people':
        <span>echo</span> 'Are you sure you want to delete this people?<br>'<span>;
        </span><span>default</span>:
        <span>break</span><span>;
    }
    </span><span>echo</span> '<a href="'.<span>%24_SERVER</span>%5B'REQUEST_URI'%5D.'&do=1">Yes</a> or <a href="admin.php">no</a>'<span>;
}
</span><span>else</span><span>
{
    </span><span>switch</span>(<span>$_GET</span>['type'<span>])
    {
        </span><span>case</span> 'people':
            <span>$query</span> = ' update people set movie_leadactor = 0 where movie_leadactor = '.<span>$_GET</span>['id'<span>];
            </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
            </span><span>$query</span> = ' delete from people where people_id='.<span>$_GET</span>['id'<span>];
            </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
                    
</span>?>
<p style="text-align:'center'">Your person has been deleted</p>
<a href="admin.php"><span>Return</span> to Index</a>
<span>php 
        </span><span>break</span><span>;
        </span><span>case</span> 'movie':
            <span>$query</span> = 'delete from movie where movie_id='.<span>$_GET</span>['id'<span>];
            </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span> (<span>mysql_error</span>(<span>$db</span><span>));
</span>?>
<p style="text-align:'center'">Your movie has been deleted</p>
<a href="admin.php"><span>Return</span> to Index</a>
<span>php
        </span><span>break</span><span>;
        </span><span>default</span>:
        <span>break</span><span>;
    }
}
</span>?>
登入後複製

注意这里的switc语句,在删除movie或people之后我们要输出成功信息并返回列表页面,一般情况下我们习惯把break语句紧跟在switch下面的主体部分,于是开始的时候我写成这样了

<span>else</span><span>
{
    </span><span>switch</span>(<span>$_GET</span>['type'<span>])
    {
        </span><span>case</span> 'people':
            <span>$query</span> = ' update people set movie_leadactor = 0 where movie_leadactor = '.<span>$_GET</span>['id'<span>];
            </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
            </span><span>$query</span> = ' delete from people where people_id='.<span>$_GET</span>['id'<span>];
            </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
        </span><span>break</span><span>;            
                    
</span>?>
<p style="text-align:'center'">Your person has been deleted</p>
<a href="admin.php"><span>Return</span> to Index</a>
登入後複製

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

Your pers
Return to Index

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

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板