php inside of php
Moderator: General Moderators
php inside of php
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?
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
no php tags are not required inside of php tags.
I always prefer the single-quoted string concatenation method of string construction:
but you can use string parsing too:
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.
I always prefer the single-quoted string concatenation method of string construction:
Code: Select all
<?php
include('header'.$variable.'.inc.php');
?>
Code: Select all
<?php
include("header$variable.inc.php");
?>
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
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.
I will try this in the coming days.
Re: php inside of php
wonderful!
Thanks so much!
Thanks so much!
Re: php inside of php
The code indicated seems to work, and thanks so much. I used (with my actual variables):
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!
Code: Select all
include('butad'.$xcatid00.'.inc.php');
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
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
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.
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
Rather than making too many assumptions about where you were going with this one, I'll just wish you good luck.markosjal wrote:As they say if the phu 5hits , wear it.
Re: php inside of php
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
Bind,
Thanks a bunch. You are a savior.
Mark
Thanks a bunch. You are a savior.
Mark