在var中存储字符串会因串联而产生错误?

时间:2011-11-16 10:09:10

标签: php html variables

我在PHP中执行以下操作

public $message = "<p>A new user has requested to signup for Company Name</p>
                   <p><strong>Name:</strong>". $name ."</p>
                   <p><strong>Email Address:</strong>" . $email . "</p>
                   <p><strong>Contact Number:</strong>" . $contact_number . "</p>
                   <p>Please check the attached file, for further details.</p>
                   <p>Thanks,<br/>Company Name Email Centre</p>";

正如您所看到的,我想创建一小部分HTML,其中散布着一些PHP变量,但是我收到以下错误,

  

解析错误:语法错误,意外'。',期待','或';'

它在第31行说明这是第二个<p>标记,但是我看不到问题

5 个答案:

答案 0 :(得分:2)

您不应在类var定义中使用var / string连接。为什么不编写getMessage()printMessage()或其他方法?

答案 1 :(得分:0)

PHP在换行符处终止字符串。每次换行开始时都需要连接:

public $message = "<p>A new user has requested to signup for Company Name</p>"
                   ."<p><strong>Name:</strong>". $name ."</p>"
                   ."<p><strong>Email Address:</strong>" . $email . "</p>"
                   ."<p><strong>Contact Number:</strong>" . $contact_number . "</p>"
                   ."<p>Please check the attached file, for further details.</p>"
                   ."<p>Thanks,<br/>Company Name Email Centre</p>";

答案 2 :(得分:0)

以这种方式试试。并查看http://php.net/manual/de/language.types.string.php

public $message = "<p>A new user has requested to signup for Company Name</p>" .
                  "<p><strong>Name:</strong>". $name ."</p>" . 
                  "<p><strong>Email Address:</strong>" . $email . "</p>" .
                  "<p><strong>Contact Number:</strong>" . $contact_number . "</p>" .
                  "<p>Please check the attached file, for further details.</p>" .
                  "<p>Thanks,<br/>Company Name Email Centre</p>";

答案 3 :(得分:0)

public $message= "<p>A new user has requested to signup for Company Name</p>\
                   <p><strong>Name:</strong>". $name ."</p>\
                   <p><strong>Email Address:</strong>" . $email . "</p>\
                   <p><strong>Contact Number:</strong>" . $contact_number . "</p>\
                   <p>Please check the attached file, for further details.</p>\
                   <p>Thanks,<br/>Company Name Email Centre</p>";


/* Try this it will work ... ( u need to escape lines using \ ) */

答案 4 :(得分:0)

你不能在类属性定义中这样做,而是定义属性,然后在类构造函数中给它一个值:

class Test {
    public $message;
    public $name = 'Demo Name';
    public function __construct() {
        $this->message = 'anything you want' . $this->name; # etc
    }
}

在实际定义中,您只能提供litterals,而不能进行变量评估。

干杯