PHP返回后停止

时间:2015-09-21 17:32:39

标签: php html

我有一个PHP函数,它在表单验证过程中显示错误消息,我在一些HTML中混合为它创建一个页面;

val emp_newBC = sc.broadcast(emp_new.collect())
val joined = emp.mapPartitions({ iter =>
      val m = emp_newBC.value
      for {
        ((t, w)) <- iter
        if m.contains(t)
        amk = m.indexOf(t)
      } yield ((w + '-' + emp_newBC.value(amk)),1) //yield ((t, w), (m.get(t).get))   //((w + '-' + m.get(t).get),1)
    }, preservesPartitioning = true)

当代码运行时,浏览器中的所有内容都是正确的,但是,在显示消息后立即查看它停止的源代码,这样就不会显示结束的div,body和html标记。

如何让它们显示,我已经尝试了echo而不是返回但是显示错误消息但仍处理表单。

5 个答案:

答案 0 :(得分:4)

您使用的功能不正确(类似于require_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);

答案 1 :(得分:1)

来自函数的

instantly going back to whoever called it表示return。意味着function launchApp($ionicPopup, appConfig) { var scheme; if (device.platform === 'iOS') { scheme = appConfig.iosScheme; } else if (device.platform === 'Android') { scheme = appConfig.androidScheme; } else if (device.platform === 'wp') { scheme = appConfig.wpScheme; } else if (device.platform === 'windows8') { scheme = appConfig.windows8Scheme; } navigator.startApp.check(scheme, function(message) { /* success */ navigator.startApp.start(scheme, function(message) { }, function(error) { /* error */ $ionicPopup.alert({ title: appConfig.appName + " not started!", content: appConfig.appName + " not started!" }) }); }, function(error) { $ionicPopup.alert({ title: appConfig.appName + " not installed!", content: appConfig.appName + " not installed! Please install it!" }) }); } myApp.controller("launchSkypeCtrl", function($scope, $ionicPopup, $cordovaInAppBrowser) { document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var skypeConfig = { "appName": "Skype", "iosScheme": "skype://", "androidScheme": "com.skype.raider", "wpScheme": "skype:", "windows8Scheme": "skype:" } $scope.launchSkype = launchApp($ionicPopup, skypeConfig); } }); myApp.controller("launchHangoutsCtrl", function($scope, $ionicPopup, $cordovaInAppBrowser) { document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { var hangoutConfig = { "appName": "Google Hangouts", "iosScheme": "gtalk://", "androidScheme": "com.google.android.talk:", "wpScheme": "gtalk:", "windows8Scheme": "gtalk:" } $scope.launchHangouts = launchApp($ionicPopup, hangoutConfig); } }); 以下的任何一行都不会被执行。

答案 2 :(得分:1)

更改以下内容:

<?php return $msg; ?>

要:

<?php echo $msg; ?>

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

答案 3 :(得分:1)

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

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 echo $msg;  ?>
  </div>
  </body>
  </html>
  <?php
}

答案 4 :(得分:0)

不使用return,而是更改为echoreturn表示您希望将某些内容返回到函数外部。使用退货的一种方式是:

    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 echo displayMessage(); ?>

或者您可以更改为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>'
  }

并调用函数:

<?php displayMessage(); ?>