PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Using a variable for include statement
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using a variable for include statement

Réponse
 
LinkBack Outils de la discussion
Vieux 11/09/2007, 18h00   #1
Jeff Benetti
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using a variable for include statement

Sorry if this is a noob question, I have used PERL, TCL and VB but I am just
getting into PHP. If there is a better place to ask noobie questions then
let me know.

I want to use the include statement but I want to pass the name of the file
rather than hard code it. It seems that PHP needs the file to be in its
defined PATH in order to do this (or am I wrong?).

My code works OK if I use hard coding

Example (works)

include ('somefile.php')

Example (doesn't work)

$TheFile = "somefile.php"

include ($TheFile)

Am I trying to do something that is impossible?

If it is a path problem then how do I get around this, I can control my
local server config but I only have a local server for development.

Thanks,
Jeff
  Réponse avec citation
Vieux 11/09/2007, 18h10   #2
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Using a variable for include statement

On Tue, 2007-09-11 at 14:00 -0300, Jeff Benetti wrote:
> Sorry if this is a noob question, I have used PERL, TCL and VB but I am just
> getting into PHP. If there is a better place to ask noobie questions then
> let me know.
>
> I want to use the include statement but I want to pass the name of the file
> rather than hard code it. It seems that PHP needs the file to be in its
> defined PATH in order to do this (or am I wrong?).
>
> My code works OK if I use hard coding
>
> Example (works)
>
> include ('somefile.php')
>
> Example (doesn't work)
>
> $TheFile = "somefile.php"
>
> include ($TheFile)
>
> Am I trying to do something that is impossible?
>
> If it is a path problem then how do I get around this, I can control my
> local server config but I only have a local server for development.


If the first works, then the second will work unless you've trimmed out
some useful information for us to see... or maybe it's the lack of a
semi-colon after the assignment (but that's probably just a quick typo
mistake).

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Vieux 11/09/2007, 19h52   #3
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Using a variable for include statement

you can definately use variables for filenames. i do it all the time.
it's not like XSLT or other languages where it has to be included at
the start or you're screwed.

On 9/11/07, Jeff Benetti <intergalactech@ns.sympatico.ca> wrote:
> Sorry if this is a noob question, I have used PERL, TCL and VB but I am just
> getting into PHP. If there is a better place to ask noobie questions then
> let me know.
>
> I want to use the include statement but I want to pass the name of the file
> rather than hard code it. It seems that PHP needs the file to be in its
> defined PATH in order to do this (or am I wrong?).
>
> My code works OK if I use hard coding
>
> Example (works)
>
> include ('somefile.php')
>
> Example (doesn't work)
>
> $TheFile = "somefile.php"
>
> include ($TheFile)
>
> Am I trying to do something that is impossible?
>
> If it is a path problem then how do I get around this, I can control my
> local server config but I only have a local server for development.
>
> Thanks,
> Jeff
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

  Réponse avec citation
Vieux 11/09/2007, 21h38   #4
Tom Ray [Lists]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Using a variable for include statement

Jeff Benetti wrote:
> Sorry if this is a noob question, I have used PERL, TCL and VB but I am just
> getting into PHP. If there is a better place to ask noobie questions then
> let me know.
>
> I want to use the include statement but I want to pass the name of the file
> rather than hard code it. It seems that PHP needs the file to be in its
> defined PATH in order to do this (or am I wrong?).
>
> My code works OK if I use hard coding
>
> Example (works)
>
> include ('somefile.php')
>
> Example (doesn't work)
>
> $TheFile = "somefile.php"
>
> include ($TheFile)
>
> Am I trying to do something that is impossible?
>
> If it is a path problem then how do I get around this, I can control my
> local server config but I only have a local server for development.
>
> Thanks,
> Jeff
>
>

Jeff, there are a couple ways you can do this.

1) include("/path/to/".$TheFile."/");

2) include $TheFile;

Notice the lack of brackets in the last one. Either way has worked for me.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11738 seconds with 12 queries