Php 返回错误结果的If、elseif和else语句
我在PHP中创建这个失败捕获时遇到了一些问题。我想要Php 返回错误结果的If、elseif和else语句,php,html,if-statement,routing,require,Php,Html,If Statement,Routing,Require,我在PHP中创建这个失败捕获时遇到了一些问题。我想要要求'error.php',返回$echo\u error\u 404=1。 我使用普通的PHP路由,这意味着,我的URL被分成/example1/example2/example3/ 我有这个页面projects,它与我进入该页面时的$routes[1]相同。文件projects.php不存在。正确理解项目应归入第二条elseif语句。然后在使用文件存在()时,给出$error\u echo\u 404=1。然而。。。出于某种奇怪的原因,它一
要求'error.php'当用户输入错误URL时显示的代码>,返回$echo\u error\u 404=1
。
我使用普通的PHP路由,这意味着,我的URL被分成/example1/example2/example3/
我有这个页面projects
,它与我进入该页面时的$routes[1]
相同。文件projects.php
不存在。正确理解项目
应归入第二条elseif
语句。然后在使用文件存在()时,给出$error\u echo\u 404=1
。然而。。。出于某种奇怪的原因,它一直延续到$echo_content='.$php_doc_html'代码>
另外,我知道我的很多代码格式很差,但是,我这样做是为了解决我的问题
检查需要哪些文件并运行错误检查的代码:
// FIND WEBSITE CONTENT
$echo_content = "";
if(empty($routes[1])){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] == "page")){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] != "page")){
$php_doc = $routes[1];
$file_exist = $php_doc.".php";
if(file_exists($file_exist)){
require $php_doc.".php";
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
}
if(empty($echo_content)){
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
if($echo_error_404 == 1){
require 'error.php';
$error_index_head = <<< EOF
HTML TAG OPENING, TITLE, HEAD AND BODY OPENING ETC.
EOF;
echo $error_index_head;
echo $header_html;
echo '<div id="content-wrap">'.$error_page_html.'</div>';
echo $echo_index_after;
echo $footer_html;
}
else{
echo $echo_index_head;
echo $header_html;
echo $echo_content;
echo $echo_index_after;
echo $footer_html;
}
这是我在浏览器中得到的返回值,清楚地显示$echo\u error\u 404
未分配值1
:
// FIND WEBSITE CONTENT
$echo_content = "";
if(empty($routes[1])){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] == "page")){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
}
elseif((!empty($routes[1])) && ($routes[1] != "page")){
$php_doc = $routes[1];
$file_exist = $php_doc.".php";
if(file_exists($file_exist)){
require $php_doc.".php";
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
}
if(empty($echo_content)){
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
}
}
else{
$echo_error_404 = 1;
if($echo_error_404 == 1){
require 'error.php';
$error_index_head = <<< EOF
HTML TAG OPENING, TITLE, HEAD AND BODY OPENING ETC.
EOF;
echo $error_index_head;
echo $header_html;
echo '<div id="content-wrap">'.$error_page_html.'</div>';
echo $echo_index_after;
echo $footer_html;
}
else{
echo $echo_index_head;
echo $header_html;
echo $echo_content;
echo $echo_index_after;
echo $footer_html;
}
var\u dump()
结果:
elseif((!empty($routes[1])) && ($routes[1] != "page")){
$php_doc = $routes[1];
var_dump($php_doc);
$php_doc_html = "";
var_dump($php_doc_html);
// THE CODE INBETWEEN
else{
$echo_error_404 = 1;
}
var_dump($php_doc_html);
var_dump($echo_error_404);
Return显示项目位于$routes[1]
中,并且$echo\u error\u 404
为=1
:
string(8) "projects"
string(0) ""
string(0) ""
int(1)
使用switch()
可能会更干净一些。如果我们首先检查$routes[1]
是否为空,则在以后的情况下不需要再次检查。如果你在你的elseif
中再次检查它,我觉得没有意义。如果为空,则满足第一个If
,而不转到语句的下一部分
// Assume no content
$echo_content = "";
// Check for content
if(empty($routes[1])){
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
} else {
switch($routes[1]){
case "page":
require 'frontpage.php';
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$front_page_html."</div>";
}
break;
default:
$file_exist = "{$routes[1]}.php";
if(file_exists($file_exist)){
require $file_exist;
if($echo_error_404 == 0){
$echo_content = '<div id="content-wrap">'.$php_doc_html."</div>";
}
} else {
$echo_error_404 = 1;
}
}
}
if(empty($echo_content) || $echo_content == ""){
$echo_error_404 = 1;
}
//假设没有内容
$echo_content=“”;
//检查内容
if(空($routes[1])){
需要“frontpage.php”;
如果($echo\u error\u 404==0){
$echo_content=''.$front_page_html.'';
}
}否则{
交换机($routes[1]){
案例“第页”:
需要“frontpage.php”;
如果($echo\u error\u 404==0){
$echo_content=''.$front_page_html.'';
}
打破
违约:
$file_exist=“{$routes[1]}.php”;
如果(文件存在($file\u存在)){
需要$file\u存在;
如果($echo\u error\u 404==0){
$echo_content='.$php_doc_html.“;
}
}否则{
$echo_error_404=1;
}
}
}
if(空($echo_content)|$echo_content==“”){
$echo_error_404=1;
}
switch()
语句是否能更好地工作?我还没有想过,但仍然。我的代码对我来说是正确的?应该给出这样一个奇怪的输出。var_转储每个阶段的所有内容。在每个if语句内部以及紧接其后的var_dump($echo_error_404);如果它没有在应该的时候切换,var_会在If语句之前转储If语句中的条件。您应该看到意外发生的地方。@Terminus,谢谢。使用var_dump()和Twistys解决方案解决问题:)如果出现问题,每个require页面将$echo_error_404
的值设置为1。因此,我需要将预设设置为$echo\u error\u 404=0
。我可以假设没有内容很难。我注意到了,我调整了它。尝试了你的答案。仍然不起作用。然而,在我自己的解决方案中使用var_dump,我可以看到正在设置“`$echo_error_404`”。由于某些原因,它无法运行正确的语句…我运行了另一个var_转储。想办法。。。我在footer.php前后使用了var_dump。它在之前设置,之后取消设置。现在就检查。很好,刚刚更新了文件,您的解决方案成功了!:)