Re: Include / Require

From: Date: Fri, 06 Apr 2001 00:39:57 +0000
Subject: Re: Include / Require
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-47362@lists.php.net to get a copy of this message
This is a good time for a quick lesson on just exactly what include/require actually does. The following two examples are exactly the same, in almost every way: === Example 1 === example1A.php: <?php $greeting = "Hello"; include("example1B.php"); echo $greeting; ?> example1B.php: <?php $greeting .= " World!"; ?> The output is "Hello World!", and that's all. === Example 2 === example2.php: <?php $greeting = "Hello"; $greeting .= " World!"; echo $greeting; ?> The output is "Hello World!" When you include a file it is almost exactly the same as if you went into the included file, cut out all the code, then went back into your first file and pasted it in. So what you can use an included file almost exactly as you would use a function, without declaring anything as global! But there are obvious reasons why functions are better than includes, but you probably know that ;) Example: first file: <?php $greeting = "Hello there"; // Let's pretend these variables are actually set by someone POSTing data. $var1 = "Hi there"; $var2 = "Ho there"; $var3 = "Me there"; $var4 = "My there"; include("include.php"); echo $text; ?> include.php: <?php if ($var1) { $greeting .= ", " . $var1; } if ($var2) { $greeting .= ", " . $var2; } if ($var3) { $greeting .= ", " . $var3; } if ($var4) { $greeting .= ", " . $var4; } ?> Now you could do the exact same thing by just making the include file into a function. Or you could just paste it into your original file. But the output remainds the same, no matter how you slice it. QUESTION ANSWER: The following: > $sql = "..." > $result = ....; > include 'variables.php?table=$table'; Can be like this: $table = "tablename"; $sql = "..."; $result = "..."; include ("variables.php"); The values of $table, $sql, and $result will all be the same in variables.php as they are in your source file. Isn't that nice? Aren't you glad that I took 4 pages of text just to explain that single phrase? Shouldn't I get a pulitser for taking so little content and putting it into so much display? I should work for microsoft, shouldn't I? -- Plutarck Should be working on something... ...but forgot what it was. ""Ashley M. Kirchner"" <ashley@pcraft.com> wrote in message news:3ACCB1DC.C282F34A@pcraft.com... > > I have a DB project going and every time I query the DB for data > from a particular table, I want to include a set of variables (that get > set based on the query result). I was thinking of doing something like > this: > > $sql = "..." > $result = ....; > include 'variables.php?table=$table'; > > However, every time I try that, it tells me it can't find > 'variables.php'. However, if I rename the file to 'variables.inc', it > does find it (though it doesn't pass any arguments to it then). > > The first method tells me it's not in the include path. Okay, > easily fixable in php.ini, however, I don't want this extra path info to > apply to the whole server (which is running several vhosts). How can I > adjust the include path just for this particular vhost? > > Or, is there a better way to do what I'm trying to accomplish? Make > it a function perhaps? A call that would look kinda like this perhaps: > variables($table) ? But then, how do I get to the actual variables? > > AMK4 > > -- > W | > | I haven't lost my mind; it's backed up on tape somewhere. > |____________________________________________________________________ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Ashley M. Kirchner <mailto:ashley@pcraft.com> . > 303.442.6410 x130 > SysAdmin / Websmith . 800.441.3873 x130 > Photo Craft Laboratories, Inc. . eFax 248.671.0909 > http://www.pcraft.com . 3550 > Arapahoe Ave #6 > .................. . . . . Boulder, CO 80303, USA > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#47362) next »