TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   include in string ? (http://www.talkphp.com/general/5454-include-string.html)

CΛSTΞX 06-21-2010 02:16 PM

include in string ?
 
Hello,

Is it possible to include something in string or with function ? If not, how to do that ?
I have to make it in string. When I use the string, it should includes it.

$include = include("file/admin.php");

Tanax 06-21-2010 03:21 PM

Wait, what?
You want to include that file into a variable? AFAIK it's not possible. But why would you want to do that?

CΛSTΞX 06-21-2010 04:01 PM

My script's template system uses tpl files. In tpl files, you can only use $value. If you use include("file");
it would be bad because the included file contains appear at the very top of the page. I mean, how to include file instead of include("file.php");

is there anyway to include file with function or anything else ?

tony 06-21-2010 04:37 PM

you can use include to save the return content of a document. the manual gives an example:
php Code:
return.php
<?php

$var = 'PHP';

return $var;

?>

noreturn.php
<?php

$var = 'PHP';

?>

testreturns.php
<?php

$foo = include 'return.php';

echo $foo; // prints 'PHP'

$bar = include 'noreturn.php';

echo $bar; // prints 1

?>

if you want the whole content and not just the return value of the file, you can always use file_get_contents

Another thing you might want to get into account here, is the performance too, specially if they are big files. You don't want them all to be hold in the memory if they are big.

CΛSTΞX 06-21-2010 04:39 PM

That is the point. Thank you.

tony 06-21-2010 04:43 PM

great, glad I could help. Sounds like you are trying to make an CMS or at least a templates system, good luck.

CΛSTΞX 06-21-2010 04:51 PM

But wait. The file which I include isnt easy as much as only a string in it. It includes many functions and etc. How to make it return ?

Here is the file xcheaphotel.com/comments/talkphp.php

delayedinsanity 06-21-2010 05:41 PM

I'm not fully sure I'm understanding so I hope I don't point you in the wrong direction; you might be interested in output buffering. Check out the following Output Control Functions, which will allow you to do something akin to the following;

php Code:
<?php

ob_start();
include( 'some/file.php' );
$somefile = ob_get_clean(); // Contents of the include are now stored in a variable which can be manipulated or echo'd at your mercy.
 

This is just a very rudimentary example but could be more on the right track?

CΛSTΞX 06-21-2010 07:35 PM

Yes! That works pretty nice. Thanks...


All times are GMT. The time now is 04:59 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0