Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP语法分析?_Php_Controls_Flow_Heredoc - Fatal编程技术网

PHP语法分析?

PHP语法分析?,php,controls,flow,heredoc,Php,Controls,Flow,Heredoc,我编辑了Q,因为没有答案 我是PHP的初学者,正在编写CMS模板引擎。这个想法很简单——我会在herdoc字符串中预加载HTML片段,然后在流控件要求时调用它们 问题是herdoc字符串的行为很奇怪。以下是一个例子: $ERROR_MESSAGE = "default"; $tst = <<<EOF <div id="error-message">$ERROR_MESSAGE</div> EOF; function splice_message_out

我编辑了Q,因为没有答案

我是PHP的初学者,正在编写CMS模板引擎。这个想法很简单——我会在herdoc字符串中预加载HTML片段,然后在流控件要求时调用它们

问题是herdoc字符串的行为很奇怪。以下是一个例子:

$ERROR_MESSAGE = "default";
$tst = <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;

function splice_message_output()
    {
    global $ERROR_MESSAGE;
    global $tst;

    if (isset($ERROR_MESSAGE))
        {
        echo <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        echo $tst;
        }
    }
输出为:

<div id="error-message">This is the errormessage text</div>
<div id="error-message">default</div>
这怎么可能?我已经在下面的代码中清楚地定义了调用的strign,herdoc代码段返回刚刚在上面设置的字符串,另一个函数返回刚刚在下面设置的字符串。

您只定义一次$tst,其中包含消息“default”。函数下面的代码设置$ERROR\u MESSAGE=$\u GET[MSG];仅当设置了$_GET[MSGTYPE]且等于“ERR”时。因此,它设置变量,打印嵌入的HEREDOC字符串,然后打印未更改的$tst。这里的埃雷多克一点也不奇怪


我认为您感到困惑的是,您认为herdoc字符串将始终包含对$ERROR\u消息的引用。事实并非如此。在字符串中使用$ERROR\u MESSAGE之类的变量时,该变量将被其内容替换,这与使用$tst=消息时的情况完全相同:$ERROR\u MESSAGE;。之后,$tst的值将是“消息是:默认值”。

如前所述,在更改$ERROR\u消息的值之前,您已经声明了$tst的值。你对heredoc的理解是这里的问题。当你将一个变量插入到一个heredoc或者任何类型的字符串中时,这并不意味着你要插入这个变量本身或者变量的引用。而是将该变量的值插入到herdeoc中。例如,如果你有以下变量

$var1 = "inserted var";
$var2 =
<<<VAR2
My $var1 goes here.
VAR2;
Heredoc函数与其他字符串一样。不同之处在于,在heredoc中,您避免了在“和”和/或任何进一步的“逃离地狱”之间逃跑和交换的需要。总的来说,herdoc只是为了让代码更易于阅读和维护

<?Php
echo "<button onclick='alert(\"double-quote\")'>double-quote</button>";
echo '<button onclick="alert('."'single-quote'".')">single-quote</button>';
echo
<<<HEREDOCSAMPLE
<button onclick='alert("heredoc")'>heredoc</button>
HEREDOCSAMPLE;
?>
现在在您的代码示例中:

$ERROR_MESSAGE = "default";
$tst =
<<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
//THIS WOULD MAKE THE CURRENT VALUE OF $tst as:
//<div id="error-message">default</div>

function splice_message_output()
{   global $ERROR_MESSAGE;
    global $tst;

    if (isset($ERROR_MESSAGE))
    // I DON'T THINK THIS VALIDATION MAKES SENSE SINCE $ERROR_MESSAGE
    // WOULD ALWAYS BE DECLARED
    {   echo <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        echo $tst;
    }
}

if (isset($_GET["MSGTYPE"]))
{   if ($_GET["MSGTYPE"] == "ERR")
    {   $ERROR_MESSAGE = $_GET["MSG"];
        // THE ONLY VARIABLE THAT CHANGED HERE IS $ERROR MESSAGE
        // $tst DID NOT CHANGE AT ALL
    }
}

function splice_message_output()
// RE-DECLARED FUNCTION? OR PERHAPS:
splice_message_output();
// AT THIS POINT ONLY $ERROR_MESSAGE HAS CHANGED ITS VALUE.
现在,如果我提议对该代码进行修订,则如下所示:

$ERROR_MESSAGE = "default";
function splice_message_output()
{   global $ERROR_MESSAGE;
    global $tst;

    if (isset($_GET["MSG"]))
    {   echo <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        $tst = <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        // NOW HERE'S A TRICKY ONE. SINCE YOU DECLARED $tst AS A
        // GLOBAL VARIABLE WITHIN THIS FUNCTION, DOING AN if(isset($tst))
        // OUTSIDE THIS FUNCTION AFTER CALLING THIS FUNCTION WOULD
        // BE EQUAL TO 'TRUE'
        echo $tst;
    }
}

if (isset($_GET["MSGTYPE"]))
{   if ($_GET["MSGTYPE"] == "ERR")
    {   $ERROR_MESSAGE = $_GET["MSG"];
        // THE ONLY VARIABLE THAT CHANGED HERE IS $ERROR MESSAGE
        // $tst DID NOT CHANGE AT ALL
    }
}

// THE VARIABLE $tst DOESN'T EXIST AT THIS POINT BUT
splice_message_output(); // ONCE THIS FUNCTION IS CALLED
// $tst WOULD HAVE ALREADY EXISTED
在更改$ERROR_MEGGAGE的值之前,将先评估$tst的值。因此,在将$ERROR\u消息用于$tst的herdeoc语句之前,只需检查您的条件并对其值进行任何更改

<?Php
echo "<button onclick='alert(\"double-quote\")'>double-quote</button>";
echo '<button onclick="alert('."'single-quote'".')">single-quote</button>';
echo
<<<HEREDOCSAMPLE
<button onclick='alert("heredoc")'>heredoc</button>
HEREDOCSAMPLE;
?>
$this = "example";
echo
<<<'NOWDOCSAMPLE'
$this variable would not be treated as a $variable.
NOWDOCSAMPLE;
$this variable would not be treated as a $variable.
$ERROR_MESSAGE = "default";
$tst =
<<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
//THIS WOULD MAKE THE CURRENT VALUE OF $tst as:
//<div id="error-message">default</div>

function splice_message_output()
{   global $ERROR_MESSAGE;
    global $tst;

    if (isset($ERROR_MESSAGE))
    // I DON'T THINK THIS VALIDATION MAKES SENSE SINCE $ERROR_MESSAGE
    // WOULD ALWAYS BE DECLARED
    {   echo <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        echo $tst;
    }
}

if (isset($_GET["MSGTYPE"]))
{   if ($_GET["MSGTYPE"] == "ERR")
    {   $ERROR_MESSAGE = $_GET["MSG"];
        // THE ONLY VARIABLE THAT CHANGED HERE IS $ERROR MESSAGE
        // $tst DID NOT CHANGE AT ALL
    }
}

function splice_message_output()
// RE-DECLARED FUNCTION? OR PERHAPS:
splice_message_output();
// AT THIS POINT ONLY $ERROR_MESSAGE HAS CHANGED ITS VALUE.
$ERROR_MESSAGE = "default";
function splice_message_output()
{   global $ERROR_MESSAGE;
    global $tst;

    if (isset($_GET["MSG"]))
    {   echo <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        $tst = <<<EOF
<div id="error-message">$ERROR_MESSAGE</div>
EOF;
        // NOW HERE'S A TRICKY ONE. SINCE YOU DECLARED $tst AS A
        // GLOBAL VARIABLE WITHIN THIS FUNCTION, DOING AN if(isset($tst))
        // OUTSIDE THIS FUNCTION AFTER CALLING THIS FUNCTION WOULD
        // BE EQUAL TO 'TRUE'
        echo $tst;
    }
}

if (isset($_GET["MSGTYPE"]))
{   if ($_GET["MSGTYPE"] == "ERR")
    {   $ERROR_MESSAGE = $_GET["MSG"];
        // THE ONLY VARIABLE THAT CHANGED HERE IS $ERROR MESSAGE
        // $tst DID NOT CHANGE AT ALL
    }
}

// THE VARIABLE $tst DOESN'T EXIST AT THIS POINT BUT
splice_message_output(); // ONCE THIS FUNCTION IS CALLED
// $tst WOULD HAVE ALREADY EXISTED