代码之家  ›  专栏  ›  技术社区  ›  Matthew Daly

php文件中出现意外的$end

php
  •  1
  • Matthew Daly  · 技术社区  · 14 年前

    我正在处理一个PHP联系人表单,但我无法使其正常工作。我在运行在Ubuntu服务器VM上的Apache服务器日志中得到以下错误:

    PHP Parse error:  syntax error, unexpected $end in /home/matthew/Sites/contactFormResponse.php on line 75, referer: http://192.168.1.4/contactForm.php
    

    通过google搜索这个错误,听起来它通常是由于在服务器没有设置识别它们时使用短的php标记,或者是因为有一块代码没有正确关闭。但据我所知,情况并非如此——据我所知,一切都正确地结束了。它所引用的行是文件末尾的一行。

    下面是PHP代码:

     <?php
                                        error_reporting(E_ALL);
                                        // Define variables to hold the name, email address and message, and import the information into the variables
                                        $name = $_POST['NameInput'];
                                        $email = $_POST['EmailAddress'];
                                        $telno = $_POST['ContactNumber'];
                                        $querytype = $_POST['QueryType'];
                                        $bookingstartdate = $_POST['BookingStartDay'] . $_POST['BookingStartMonth'] . $_POST['BookingStartYear'];
                                        $bookingenddate = $_POST['BookingEndDay'] . $_POST['BookingEndMonth'] . $_POST['BookingEndYear'];
                                        $message = $_POST['QueryText'];
    
                                        // Validate the inputs - send it if it's OK
                                        if(3 < strlen($name) && 3 < strlen($email))
                                        {
                                                $email_message = <<< EMAIL
                                                        Message from contact form at holidaychalet.co.uk
                                                        Name: $name
                                                        Email: $email
                                                        Contact Number: $telno
                                                        Query Type: $querytype
                                                        Booking Start Date: $bookingstartdate
                                                        Booking End Date: $bookingenddate
                                                        The message:
                                                        $message
                                                        EMAIL;
                                                $headers = "cc:me@myemailaddress.com\r\n";
                                                if(mail('matthew@localhost','Contact form email', $email_message, $headers))
                                                {
                                                        echo "Thanks for completing the form! I'll be in touch shortly!";
                                                }
                                                else
                                                {
                                                        echo "Something went wrong - please use the back button and try again";
                                                }
                                        }
                                        else
                                        {
                                                echo "You didn't complete the form fully enough! Please use go back using your web browser's back button";
                                        }
                                ?>
    
    3 回复  |  直到 14 年前
        1
  •  5
  •   Gumbo    14 年前

    的结束标识符 here document syntax 必须位于行首,没有任何缩进:

    必须注意,带有结束标识符的行不能包含任何其他字符,除非 可能地 分号(分号) ; )这意味着标识符 不能缩进 ,分号前后不能有任何空格或制表符。重要的是要认识到,结束标识符之前的第一个字符必须是本地操作系统定义的换行符。

    所以在你的例子中:

                                                $email_message = <<< EMAIL
                                                        Message from contact form at holidaychalet.co.uk
                                                        Name: $name
                                                        Email: $email
                                                        Contact Number: $telno
                                                        Query Type: $querytype
                                                        Booking Start Date: $bookingstartdate
                                                        Booking End Date: $bookingenddate
                                                        The message:
                                                        $message
    EMAIL;
    
        2
  •  4
  •   Amber    14 年前
    EMAIL;
    

    不能缩进。 Heredoc 语法要求结束标识符位于 开始 其中不包括前导空格。

        3
  •  3
  •   favo    14 年前

    您在$email_message中填入了一个以email结尾的字符串;

    这个必须在一行中。

    把它改成:

                                                $email_message = <<< EMAIL
                                                        Message from contact form at holidaychalet.co.uk
                                                        Name: $name
                                                        Email: $email
                                                        Contact Number: $telno
                                                        Query Type: $querytype
                                                        Booking Start Date: $bookingstartdate
                                                        Booking End Date: $bookingenddate
                                                        The message:
                                                        $message
    EMAIL;