Page 1 of 1

php inside of php

Posted: Thu Apr 22, 2010 10:40 pm
by markosjal
I have been having some very productive and useful fun with URL variables, but now the server is live and I dont want to cause issues unnecesarily, so I want to run this past someone else, as I am not sure it is right.

When a standard include statement is used it is like this

<?php include("header.inc.php"); ?>

Now I want to make a variable out of header.inc.php so the header that is loaded will depend on the value of a variable. In other words header1.inc.php or header2.inc.php, etc.

<?php include=("header<?php echo $variable; ?>.inc.php">

is this correct, or do I need to change the bracketing because of the php statement inside of an open php statement?

Re: php inside of php

Posted: Thu Apr 22, 2010 11:11 pm
by Bind
no php tags are not required inside of php tags.

I always prefer the single-quoted string concatenation method of string construction:

Code: Select all

<?php
include('header'.$variable.'.inc.php');
?>
but you can use string parsing too:

Code: Select all

<?php
include("header$variable.inc.php");
?>
single-quoted string concatenation is the prefered method.

It's less server-resource intensive, because single-quoted strings are not parsed, whereas double-quoted string are parsed by PHP.

Re: php inside of php

Posted: Fri Apr 23, 2010 2:28 am
by markosjal
thanks so much. I am kind of a noob, and get hung up on some of the dumbest things.

I will try this in the coming days.

Re: php inside of php

Posted: Sun Apr 25, 2010 2:21 pm
by markosjal
wonderful!
Thanks so much!

Re: php inside of php

Posted: Sun Apr 25, 2010 10:56 pm
by markosjal
The code indicated seems to work, and thanks so much. I used (with my actual variables):

Code: Select all

include('butad'.$xcatid00.'.inc.php'); 
I now have decided need to take this a step further. With proper foresight I would have recognized that this line of code was key in saving me a lot of trouble, however I did not recognize that till I uploaded the same files today to 20 different directories!

I need to combine the above with a statement like

if @file_exists

so what I mean is that it will check to see if the 'butad'.$xcatid00.'.inc.php' file exists first then load it, if it does. If it does not exist, it would load for instance "butadX.inc.php", or a constant (fixed) file as a catch all.


This would save me from loading 20 directories with the same files, and modifying 20 directories with the same files when I make a change.

any help appreciated,

thanks!

Re: php inside of php

Posted: Sun Apr 25, 2010 11:04 pm
by phu
All you need to do is remove the file name construction from the include; assign it to a variable. You can then test for the file whose name you've generated with the file_exists function and load it or default as necessary.

Re: php inside of php

Posted: Sun Apr 25, 2010 11:49 pm
by markosjal
Phu ,

I know the commands I need to use, I do not know how to construct it! This is precisely what I am looking for help on. too many brackets and weird squiggly lines , not to mention the carriage returns that all seem to make little sense!
I just have to get it done.
As they say if the phu 5hits , wear it.

Re: php inside of php

Posted: Mon Apr 26, 2010 12:21 am
by phu
markosjal wrote:As they say if the phu 5hits , wear it.
Rather than making too many assumptions about where you were going with this one, I'll just wish you good luck.

Re: php inside of php

Posted: Mon Apr 26, 2010 12:43 am
by Bind

Code: Select all

<?php
if(@file_exists('butad'.$xcatid00.'.inc.php'))
   {
       include('butad'.$xcatid00.'.inc.php');
   }
else
   {
       # do something else if it does not exist ?
   }
?>

Re: php inside of php

Posted: Tue Apr 27, 2010 12:27 am
by markosjal
Bind,

Thanks a bunch. You are a savior.

Mark