Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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_Html - Fatal编程技术网

返回后PHP停止

返回后PHP停止,php,html,Php,Html,我有一个PHP函数,显示一条错误消息作为表单验证的一部分,我混合了一些HTML为它创建了一个页面 function displayMessage($msg) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmln

我有一个PHP函数,显示一条错误消息作为表单验证的一部分,我混合了一些HTML为它创建了一个页面

  function displayMessage($msg)
  {
      ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>          
        <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
        <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
        <title>Error sending message</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
      </head>
      <body>
      <div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
        <?php return $msg;  ?>
      </div>
      </body>
      </html>
      <?php
  }
函数显示消息($msg)
{
?>
发送消息时出错

return
从函数返回意味着
立即返回给调用它的人
。意味着
return
下面的任何一行都不会执行。

而不是使用
return
,改为
echo
返回意味着您希望返回函数外部的内容。使用retur的一种方法n将是:

    function displayMessage($msg)
      {
          return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>          
            <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
            <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
            <title>Error sending message</title>
            <meta name="viewport" content="width=device-width, initial-scale=1" />
          </head>
          <body>
          <div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
          '.$msg.'
          </div>
          </body>
          </html>';
      }
并调用函数:

<?php displayMessage(); ?>

更改以下内容:

<?php return $msg; ?>

致:


return
关键字只能在函数调用中使用。

return
表示“从函数返回此值,并停止执行函数”。您实际查找的关键字是
echo

function displayMessage($msg)
  {
      echo '
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>          
        <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
        <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
        <title>Error sending message</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
      </head>
      <body>
      <div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
        '.$msg.'
      </div>
      </body>
      </html>'
  }
函数显示消息($msg)
{
?>
发送消息时出错

您使用的函数不正确(类似于
需要\u once
).函数用于调用并返回一个值,不用于以这种方式包含动态代码。函数在
return$msg
之后停止的原因是,您告诉函数返回一个名为$msg的变量,并结束函数块的进一步执行

我建议用这样的东西

  function displayMessage($msg)
  {
      $html = <<<EOD
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>          
        <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
        <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
        <title>Error sending message</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
      </head>
      <body>
      <div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
         $msg;
      </div>
      </body>
      </html>
      EOD;
    return $html;
  }
echo displayMessage($msg);
函数显示消息($msg)
{

$html=您如何调用
displayMessage()
?我想您的意思是使用
echo
而不是
return
。无论如何,这是不正确的。您认为PHP就像JS。为什么OP“试试这个”呢一个好的答案总是有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。是的,你说得对。但我想他会做回声功能的研究。下次我会发布整个答案。你可以编辑你的答案。这样更好吗?
<?php echo $msg; ?>
  function displayMessage($msg)
  {
      $html = <<<EOD
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>          
        <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
        <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
        <title>Error sending message</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
      </head>
      <body>
      <div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
         $msg;
      </div>
      </body>
      </html>
      EOD;
    return $html;
  }
echo displayMessage($msg);