suede1976 Posted May 8, 2009 Share Posted May 8, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157407-solved-basic-question-about-php-include-in-head/ Share on other sites More sharing options...
Maq Posted May 8, 2009 Share Posted May 8, 2009 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") { Quote Link to comment https://forums.phpfreaks.com/topic/157407-solved-basic-question-about-php-include-in-head/#findComment-829800 Share on other sites More sharing options...
tail Posted May 8, 2009 Share Posted May 8, 2009 You need two "=" <?php if ($page == "portfolio") { include_once("content/portScripts.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157407-solved-basic-question-about-php-include-in-head/#findComment-829801 Share on other sites More sharing options...
gevans Posted May 8, 2009 Share Posted May 8, 2009 <?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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157407-solved-basic-question-about-php-include-in-head/#findComment-829802 Share on other sites More sharing options...
suede1976 Posted May 8, 2009 Author Share Posted May 8, 2009 Thank you, i appreciate the explanations. Quote Link to comment https://forums.phpfreaks.com/topic/157407-solved-basic-question-about-php-include-in-head/#findComment-829814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.