运行文件时出现PHP语法错误

时间:2015-01-05 13:14:36

标签: php syntax

我是PHP的新手,当我运行PHP文件时,我收到以下PHP错误。 非常感谢一点帮助,非常感谢提前

  

解析错误:第21行的C:\ xampp \ htdocs \ facebook \ arrays.php中的语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE),期待标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

这是我的代码:

    <html>
        <head>
            <title> An empty html page </title>
        </head>
        <body>
            <!-- this is an html comment -->
            <!-- currently the body is empty -->
            <?php

            echo "<p>hello world</p>";

            $name = "Peter Pan";
            $address = "Neverland";
            $age = "14.75";

            $unnamed_lost_boys = array(4.5, 6, 7, 5.5, 5.25, 4, 7, 6.75);
            $named_lost_boys = array( "Tootles"=> 4.5,
                                    "Nibs"=>6,
                                    "Slightly"=>7.5);

            echo "The age of Nibs is $named_lost_boys['Nibs']";

            function print_details ($name, $age, $address)
            {
            echo "My name is $name, I'm from $address and I'm $age years old";
            echo "<br>";
            echo 'My name is '.$name.",I'm from ".$address." and I'm $age years old";
            echo "<br>";
            }
            print_details($name, $age, $address);

            ?>
        </body>
    </html> 

2 个答案:

答案 0 :(得分:2)

你不能在字符串插值中使用数组而不用花括号括起来;所以使用:

echo "The age of Nibs is {$named_lost_boys['Nibs']}";

另见the documentation

答案 1 :(得分:2)

尝试:

echo "The age of Nibs is " . $named_lost_boys['Nibs'];