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

php独特的语法

WBOY
풀어 주다: 2016-06-06 19:46:47
원래의
1330명이 탐색했습니다.

今天写一个程序的时候遇到一个很有意思的问题,这个和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으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿