Page 1 of 1

IF statements and includes

Posted: Sat Mar 03, 2007 11:22 pm
by michlcamp
Is there a way to use IF statements with includes?

I want to put specific include files on specific pages -

something like

Code: Select all

IF (thispageurl = http://mydomain.com/aboutme.php) 
          {include("aboutme_menu.php")}

ELSEIF (thispageurl = http://mydomain.com/contactme.php)
          {include("contactme_menu.php")}

,etc...
know what I want to do but don't know how to write it..
thanks in advance...
mc

Posted: Sun Mar 04, 2007 12:48 am
by dibyendrah
Correction to your code to work with PHP.

Code: Select all

<?php
if($thispageurl = "http://mydomain.com/aboutme.php"){
	include("aboutme_menu.php");
}elseif ($thispageurl = "http://mydomain.com/contactme.php"){
	include("contactme_menu.php");
}
?>
See control structure of php manual for more information
http://php.net/manual/en/language.contr ... ctures.php

Re: IF statements and includes

Posted: Sun Mar 04, 2007 12:55 am
by jmut
sure it is possible but seems you should read little about php first. Variables, control structures etc.

Code: Select all

if ($variable == 'someString') {
     include('script1.php');
}elseif ($variable == 'someString2') {
     include('script2.php');
}

Posted: Sun Mar 04, 2007 9:05 am
by feyd
There's a thread linked from Useful Posts that may be of interest regarding includes.

Posted: Mon Mar 05, 2007 3:33 pm
by RobertGonzalez
And make sure when checking equality that you use == (or === for type and value equality), not a single =, as this is assignment, not equality.

This one has bit me on more than one occassion I'm afraid, so I tell as many people as I can to watch for it. :oops:

Posted: Mon Mar 05, 2007 4:52 pm
by volka
That's why it's a good idea to write the literal on the left side.

Code: Select all

<?php
if ( 'xyz'=$var ) { // syntax error
  // ...
}

Posted: Mon Mar 05, 2007 4:56 pm
by RobertGonzalez
Good advice volka. I probably should have mentioned that as well :oops:.