IF statements and includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

IF statements and includes

Post 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
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: IF statements and includes

Post 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');
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a thread linked from Useful Posts that may be of interest regarding includes.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
  // ...
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good advice volka. I probably should have mentioned that as well :oops:.
Post Reply