Jump to content

Recommended Posts

Hi all, i am just just learning PHP and wrote my first if statement and it worked and i thought i was the bees knees, but it turns out it does not in fact work:

 

I have a basic index page structure like this:

 

<?php

$page = $_GET['page'];

if (!$page) {
$page = "home";
}

include_once("header.php");
include_once("content/".$page.".php");
include_once("menu.php");
include_once("footer.php");

?>

 

there are several PHP docs in the contents folder. in the header i want certain scripts only to load with a certain page so i did this in the header.php file:

 

<?php

if ($page = "portfolio") {
include_once("content/portScripts.php");
}

?>

 

I was hoping that, the file "portScripts.php" would only load when the page selected had the variable "portfolio" in the URL.

 

the thing is, it loads "portscripts.php" just fine, but every page has the "portfolio" content, no matter what the $page is in the URL it always comes up portfolio now.

 

before I added this IF statement to the header.php everything worked just fine. it seems somehow i made the $page variable always be "portfolio"

 

let me know if i need to include more info.

 

Thanks!

Instead of this:

 

$page = $_GET['page'];

if (!$page) {
$page = "home";
}

 

Just do this:

 

$page = (isset($_GET['page'])) ? $_GET['page'] : "home";

 

It's called a ternary  operator.

 

And this line should use double '=='.  Single is for assigning, double and triple is for comparing. (triple compares datatypes as well as value)

 

if ($page == "portfolio") {

<?php

if ($page = "portfolio") {
include_once("content/portScripts.php");
}

?>

 

In that if statement you're assigning the string 'portfolio' to the variable 'page', so all your if statementes will come true. If you want to test equality you need two equals signs;

 

<?php

if ($page == "portfolio") {
include_once("content/portScripts.php");
}

?>

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.