全局变量$_POST[name]的引用哪个写法正确?
$_POST[pwd]
$_POST['pwd']
$_POST["pwd"]PS:本人初学PHP,原有C语言基础,感觉PHP语言的语句规范没有C那么严谨,而且教材(《PHP从入门到精髓》)错漏百出,感觉挺别扭的,有没有什么权威的资料可以查询的?

解决方案 »

  1.   

    应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么 $foo[bar] 错了呢?可能在老的脚本中见过如下语法: 
    <?php
    $foo[bar] = 'enemy';
    echo $foo[bar];
    // etc
    ?>  
    这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,PHP 将把它替代为 'bar' 并使用之。 注: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 PHP 不能解析它们。 
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    ini_set('html_errors', false);
    // Simple array:
    $array = array(1, 2);
    $count = count($array);
    for ($i = 0; $i < $count; $i++) {
        echo "\nChecking $i: \n";
        echo "Bad: " . $array['$i'] . "\n";
        echo "Good: " . $array[$i] . "\n";
        echo "Bad: {$array['$i']}\n";
        echo "Good: {$array[$i]}\n";
    }
    ?>  
    注: 上例将输出:Checking 0:
    Notice: Undefined index:  $i in /path/to/script.html on line 9
    Bad:
    Good: 1
    Notice: Undefined index:  $i in /path/to/script.html on line 11
    Bad:
    Good: 1Checking 1:
    Notice: Undefined index:  $i in /path/to/script.html on line 9
    Bad:
    Good: 2
    Notice: Undefined index:  $i in /path/to/script.html on line 11
    Bad:
    Good: 2
     演示此效应的更多例子: 
    <?php
    // 显示所有错误
    error_reporting(E_ALL);$arr = array('fruit' => 'apple', 'veggie' => 'carrot');// 正确
    print $arr['fruit'];  // apple
    print $arr['veggie']; // carrot// 不正确。This works but also throws a PHP error of
    // level E_NOTICE because of an undefined constant named fruit
    //
    // Notice: Use of undefined constant fruit - assumed 'fruit' in...
    print $arr[fruit];    // apple// Let's define a constant to demonstrate what's going on.  We
    // will assign value 'veggie' to a constant named fruit.
    define('fruit','veggie');// Notice the difference now
    print $arr['fruit'];  // apple
    print $arr[fruit];    // carrot// The following is okay as it's inside a string.  Constants are not
    // looked for within strings so no E_NOTICE error here
    print "Hello $arr[fruit]";      // Hello apple// With one exception, braces surrounding arrays within strings
    // allows constants to be looked for
    print "Hello {$arr[fruit]}";    // Hello carrot
    print "Hello {$arr['fruit']}";  // Hello apple// This will not work, results in a parse error such as:
    // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
    // This of course applies to using autoglobals in strings as well
    print "Hello $arr['fruit']";
    print "Hello $_GET['foo']";// Concatenation is another option
    print "Hello " . $arr['fruit']; // Hello apple
    ?>  当打开 error_reporting() 来显示 E_NOTICE 级别的错误(例如将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。
      

  2.   

    " "双引号里面的字段会经过编译器解释,然后再当作HTML代码输出。 ' '单引号里面的不进行解释,直接输出。 例如: 
    $abc='my name is tome'; 
    echo $abc //结果是:my name is tom 
    echo '$abc' //结果是:$abc 
    echo "$abc" //结果是:my name is tom