Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.