Jump to content

file_exists wont activate if statement


cgimusic

Recommended Posts

I have a really weird problem with some code I wrote:

<?php
echo file_exists("includes/".$_GET["page"].".inc.php");
if($_GET["page"]=""||!isset($_GET["page"])){
include "includes/main.inc.php";
}
elseif(file_exists("includes/".$_GET["page"].".inc.php")){
include "includes/".$_GET["page"].".inc.php";
};
?>

The elseif part does not execute despite the fact that

echo file_exists("includes/".$_GET["page"].".inc.php");

returns "1" w/o quotes and the file definitely does exist. I believe it is the $_GET part that is causing the problem because when I do

elseif(!file_exists("includes/".$_GET["page"].".inc.php")){

I get the error "Warning: include(includes/.inc.php) [function.include]: failed to open stream: No such file or directory"

 

It is probably just a really stupid mistake but I can't figure out :wtf: is wrong. I would much appreciate any enlightenment you may have to offer.

Link to comment
https://forums.phpfreaks.com/topic/183904-file_exists-wont-activate-if-statement/
Share on other sites

Also this statement

if($_GET["page"]=""||!isset($_GET["page"])){

 

Is incorrect, it should be:

if (!isset($_GET["page"]) || $_GET["page"] == ""){

 

A: you put the isset first, because if the variable is not set, there is nothing in it to test and will cause a notice error.

B: you have 1 equals sign comparing the $_GET to "", that just assigns the $_GET to be "". So changing that to == fixes that issue as that is the comparison operator.

Also this statement

if($_GET["page"]=""||!isset($_GET["page"])){

 

Is incorrect, it should be:

if (!isset($_GET["page"]) || $_GET["page"] == ""){

 

A: you put the isset first, because if the variable is not set, there is nothing in it to test and will cause a notice error.

B: you have 1 equals sign comparing the $_GET to "", that just assigns the $_GET to be "". So changing that to == fixes that issue as that is the comparison operator.

 

Thank you. I completely forgot that == is the comparison operator. I said it would be a stupid mistake.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.