php inside of php

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
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

php inside of php

Post 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?
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: php inside of php

Post 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.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: php inside of php

Post 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.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: php inside of php

Post by markosjal »

wonderful!
Thanks so much!
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: php inside of php

Post 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!
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: php inside of php

Post 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.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: php inside of php

Post 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.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: php inside of php

Post 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.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: php inside of php

Post 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 ?
   }
?>
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: php inside of php

Post by markosjal »

Bind,

Thanks a bunch. You are a savior.

Mark
Post Reply