Include a Variable

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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Include a Variable

Post by iknownothing »

Hey all,
I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Why include it? Just paste it into the file you want it or set it as a constant in the other file and refernence it that way.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

jayshields wrote:Why include it? Just paste it into the file you want it or set it as a constant in the other file and refernence it that way.
because its editable on the other page.

also with this, when the variable includes, it will be a reasonably long paragraph of text, and I was wondering how to add a "..." just before the end of its available area. Prime example being a search engine, when the desciption doesnt fit in.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include a Variable

Post by onion2k »

iknownothing wrote:I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.
You can't do that. Think of the include() function as a "copy and paste" of whatever is in the file being included into the script thats running at the position where the include() is called.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Re: Include a Variable

Post by iknownothing »

onion2k wrote:
iknownothing wrote:I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.
You can't do that. Think of the include() function as a "copy and paste" of whatever is in the file being included into the script thats running at the position where the include() is called.
ok, so can i include the entire file, but have it invisible, so i can "paste" only the variable...?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include a Variable

Post by onion2k »

iknownothing wrote:ok, so can i include the entire file, but have it invisible, so i can "paste" only the variable...?
You could do that with the output buffer I guess. It'd be a pretty awful hack though. Why not refactor the code so that it works properly? EG, put the variable you want into another file (better still, a database table) and include it in both the page you're working on at the moment, and the page it currently resides in.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

yeah i could do that easily enough, just figured it would be great to have something so simple actually work...
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

iknownothing wrote: also with this, when the variable includes, it will be a reasonably long paragraph of text, and I was wondering how to add a "..." just before the end of its available area. Prime example being a search engine, when the desciption doesnt fit in.
so does anyone know anything about this?? perhaps a character counter or something?
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Substr

Post by timclaason »

To do that, use substr()

Code: Select all

print( substr($var, 0, 50) . "..." );
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Looks in our useful posts thread, there is a thread describing how to do this without chopping words in half.
Post Reply