PHP4实际应用经验篇(9)

  •   2009-08-01/08:10
  • 作者:孙运动

    “ ===” 操作符
    ------------------------

    我们上面已经提到过, PHP4 增加了一个新的 === 操作符来测试是否变量具有相同的类型。这儿有一个例子:


    --------------------------------------------------------------------------------
    < ?

    if (!$submit)
    {
    // 如果$submit不存在, 这暗示表单还没有提交
    // 所以显示第一个页面

    ?>

    < html>
    < head>
    < style type="text/css">
    td {font-family: Arial;}
    < /style>
    < /head>

    < body>

    < form method="GET" action="cookie.php">
    < table cellspacing="5" cellpadding="5" border="0>

    < tr>
    < td align="center">
    请输入任何数值或数据
    < /td>
    < td align="right">
    < input type="text" name="yin">
    < /td>
    < /tr>

    < tr>
    < td align="center">
    请再输入一些数值或数据
    < /td>
    < td align="right">
    < input type="text" name="yang">
    < /td>
    < /tr>

    < tr>
    < tr>
    < td colspan="2" align="center">
    < input type="submit" name="submit" value="Test!">
    < /td>
    < /tr>

    < /table>
    < /form>
    < /body>

    < /html>


    < ?
    }
    else
    {

    // 如果$submit 确实存在了,表单已经被提交了
    // 所以用以下代码处理


    if ($yin === $yang)
    {
    $result = "两个变量值和类型完全相同";
    }
    else
    {
    $result = "两个变量值和类型完全不相同";
    }

    ?>

    < html>
    < head>
    < basefont face="Arial">
    < /head>

    < body>
    < b>< ? echo $result; ?>< /b>

    < /body>
    < /html>

    < ?
    }
    ?>
    --------------------------------------------------------------------------------


    选择句法
    ------------------

    PHP对于目前讨论的多种控制结构也支持一种选择句法。例如,你可以这样做:

    --------------------------------------------------------------------------------
    < ?

    if ($elvis == 0)
        {
        echo "Elvis 已经离开了大楼!";
        }
    else
        {
        echo "Elvis 还在这儿!";
        }

    ?>

    或者你也可以这样做:

    < ?

    if ($elvis == 0):
        echo "Elvis 已经离开了大楼!";
    else:
        echo "Elvis 还在这儿!";
    endif;

    ?>
    --------------------------------------------------------------------------------

    第二种选择和第一种完全相同,在这种结构中简单的用一个冒号[:]替换第一个大括弧,移去第二个大括弧,使用 "endif"语句来结束。

    这些就是第二部分的内容。下次,我们将带给你循环,数组和更多的表单-所以一定不要错过啊!


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多