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

Php 意料之外的

Php 意料之外的,php,foreach,Php,Foreach,给出: 分析错误:语法错误,第33行C:\WAMP\www\sdgag\index.php中出现意外的T_ELSEIF 我想这应该行得通。。问题可能是什么 谢谢有一个结束括号比其他括号重要得多 应改为: $pages = array("grac", "zamknij", "dolaczyc"); $pagesid = array("showNews", "showThread", "showProfile"); foreach ($pagesid as $page) { if (isset

给出: 分析错误:语法错误,第33行C:\WAMP\www\sdgag\index.php中出现意外的T_ELSEIF

我想这应该行得通。。问题可能是什么

谢谢

有一个结束括号比其他括号重要得多

应改为:

$pages = array("grac", "zamknij", "dolaczyc");
$pagesid = array("showNews", "showThread", "showProfile");

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
  include('sobra/'.$page.'.php');
  }
}

// just pages
elseif (in_array($_GET['page'], $pages)) {
include("$_GET[page].php");
}

// error
else include('error.php');

如果正确缩进源代码,这些错误会很快出现,您可以自己修复。

如果没有将elseif和else附加到If,您就将它们放在foreach循环块之外。

也许是另一种方法。按照您的逻辑,确定最终要包含的页面。完成所有逻辑后,包括您确定的页面

$pages = array("grac", "zamknij", "dolaczyc");
$pagesid = array("showNews", "showThread", "showProfile");

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
    include('sobra/'.$page.'.php');
  }
  // just pages
  else if (in_array($_GET['page'], $pages)) {
    include("$_GET[page].php");
  }
  // error
  else include('error.php');
}   
以下内容未经测试,可能包含错误。让我知道,我会更新代码


您的else未连接到条件语句。它在跟踪您的foreach。最后一个else仍然不在if子句中啊,sh**您的右边,修复了它输出所有错误。php 3次:/。如果这不是你期望的,你可能应该重新考虑你的方法。
$pages = array('grac', 'zamknij', 'dolaczyc');
$pagesid = array('showNews', 'showThread', 'showProfile');

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
    include('sobra/'.$page.'.php');
  }
  // just pages
  else if (in_array($_GET['page'], $pages)) {
    include($_GET[$page].'.php'); // fixed missing $, restylized to match previous style
  }
  else include('error.php');
}
<?php

  // Predefined list of acceptable pages
  $pages = array("one","two","three");
  $pagesid = array("four","five","six");

  // Gather any user-defined page request
  $desPage = trim($_GET["page"]);

  // Assume they are wrong, and need to see error.php
  $pageToLoad = "error.php";

  // If the user request is not empty
  if (!empty($desPage)) {
    if (in_array($desPage,$pages)) {
      $pageToLoad = $desPage . ".php";
    }
  } else {
  // User request is empty, check other variables
    foreach ($pagesid as $pageid) {
      if (isset($_GET[$pageid])) {
        $pageToLoad = $pageid . ".php";
      }
    }
  }

  // Show output page
  include($pageToLoad);

?>