代码之家  ›  专栏  ›  技术社区  ›  Ibrahim Azhar Armar

一个简单的php语法问题[duplicate]

php
  •  0
  • Ibrahim Azhar Armar  · 技术社区  · 15 年前

    我只想知道下面的代码 ? : 指定,如果有人解释我下面的代码,我将不胜感激。谢谢您

    $country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']); 
    
    5 回复  |  直到 15 年前
        1
  •  3
  •   Sarfraz    15 年前

    它被称为 三元 速记 本规范的适用范围:

    if (empty($_POST['country']))
    {
      die ("ERROR: Enter a country");
    }
    else
    {
      $country = mysql_escape_string($_POST['country']);
    }
    

    语法:

    condition ? used if true : used if false;
    

    或者你可以做作业:

    variable = condition ? used if true : used if false;
    

    http://www.tuxradar.com/practicalphp/3/12/4

        2
  •  1
  •   Community CDub    8 年前

    看这个:

    PHP syntax question: What does the question mark and colon mean?

    它是PHP和其他语言中的三元运算符。

        3
  •  1
  •   dwich    15 年前

    $country = empty($_POST['country']) ? die ("ERROR: Enter a country") :

    我假设这个脚本接受来自POST方法发送的表单的数据。如果country变量为空,则退出脚本并显示错误消息。

    mysql_escape_string($_POST['country']);

    此函数应返回给定变量的转义值。所以应该这样写

    $country = mysql_escape_string($_POST['country']);

    更多信息请点击此处: http://php.net/manual/en/function.mysql-escape-string.php

        4
  •  1
  •   Hammerite    15 年前
    $country = empty($_POST['country']) ?
               die ("ERROR: Enter a country") :
               mysql_escape_string($_POST['country']);
    

    如果表达式 empty($_POST['country']) 计算结果为 true ,那么 die ("ERROR: Enter a country") 将被评估(结果将被分配给 $country 但事实上 die()

    另一方面,如果 计算结果为 false ,那么 mysql_escape_string($_POST['country']) 将进行评估,并将结果分配给 $国家 .

        5
  •  1
  •   ajile    15 年前

    它的测试条件是:如果HTML表单中的变量为空,则打印“ERROR:Enter a country”,否则设置变量 安全字符。。

    推荐文章