TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   $content = include('file2include.php'); (http://www.talkphp.com/general/4529-content-include-file2include-php.html)

Sakakuchi 06-11-2009 09:29 AM

$content = include('file2include.php');
 
Well I tried something funny some days ago - include a file with include and put it in a variable :-)

file2include.php
PHP Code:

<table>
<tr><td><?php echo 'hello world' ?></td></tr>
</table>

now I include that file like:
PHP Code:

<?php
$content 
= include('file2include.php');

echo 
$content;
?>


Now what I get in the Browser is:

HTML Code:

<table>
<tr><td>Hello World</td></tr>
</table>1

Now I wondered where that 1 comes from? Is it a true which comes since the file was successfully included - and get's converted to a 1?

(This thread is not about - if we should use something like that above or not ;-) )

dschreck 06-11-2009 09:49 AM

include comes back as a boolean

Salathe 06-11-2009 10:00 AM

If the file has been included successfully, and there is no return statement in the included file, $content will be an integer 1. If the file returned something, then $content will be that value. If the file could not be included for whatever reason, then $content would be a boolean FALSE and an E_WARNING would be issued.

Sakakuchi 06-12-2009 11:41 AM

lol, sorry for an dumb question like that, but I got a code like that

PHP Code:

$content = include('pages/ok_cart_product.php');
$content  substr($content,0,-1);
echo 
$content

So I thought the one was added at the end of $content -since he cutted the last character away :-D

changed it now to
PHP Code:

include('pages/ok_cart_product.php'); 

since there is no need of $content ;-) | THX for clearing that up

Tanax 06-12-2009 03:33 PM

In a more learning purpose.. How would you get the content of a PHP file(with the PHP intact), and assign in to a variable? Is that even possible?

Village Idiot 06-12-2009 03:39 PM

Quote:

Originally Posted by Tanax (Post 25319)
In a more learning purpose.. How would you get the content of a PHP file(with the PHP intact), and assign in to a variable? Is that even possible?

Same way you would any text file, fopen.

Wildhoney 06-12-2009 03:39 PM

It is possible with file_get_contents and eval if you wish to execute the code, too. If nobody else has posted the code for you by the time I get my laptop back, I will write an example :-) !

Sakakuchi 06-12-2009 07:55 PM

No idea how wildhoney wants to go with eval - but you can do it like:

PHP Code:

<?php
ob_start
();
include(
'file2include.php');
$var ob_get_contents();
ob_end_clean();
?>

<html>
<head></head>
<body><?=$var ?></body>
</html>

and the file which gets included:

PHP Code:

<table>
  <tr>
    <th>Column 1 Heading</th>
    <th>Column 2 Heading</th>
  </tr>
  <tr>
    <td><?='content1' ?></td>
    <td><?='content2' ?></td>
  </tr>
</table>

Would give that output:

HTML Code:

<html>
<head></head>
<body><table>
  <tr>
    <th>Column 1 Heading</th>
    <th>Column 2 Heading</th>
  </tr>
  <tr>
    <td>content1</td>

    <td>content2</td>
  </tr>
</table>
</body>
</html>

:-)

Edit: sorry forgot to explain what happens actually:

PHP Code:

<?php
ob_start
(); //This is starting the buffer
include('file2include.php');//now we load the file into the buffer
$var ob_get_contents();// load the buffer to $var
ob_end_clean();//delete the buffer
//and thats it ;-)
?>


sketchMedia 06-12-2009 09:42 PM

ARGH, just read the post above sigh :'-(

Long day.

rguy84 06-15-2009 07:15 AM

@Saka why would you do all that?
I usually just say include (...)

Sakakuchi 06-16-2009 05:52 AM

Well as I said it was not my code -and I changed it later on to just "include" - but you could use for something like:

We want to build an small login widget. The html code is somewhere in an extra html file, and we are currently in a class which builds the sidebar widgets.

Now we dont want to "echo" the code when it is executed since we still have some other html which goes first (<head></head><body> etc.). So we could go, copy the content in our variable - and include it anywhere in our site.

Well, it might not be the best example - but I cannot think of any better atm :-D

rguy84 06-16-2009 06:53 AM

That lost me...
If you had a function that spit out a your login/join box, you would say:
PHP Code:

<?php
 display_login
();
?>
// display_login would be a div with a form. Right?

Personally I would have a file called loginbox.php with my form code, then if I needed that my code would be:
PHP Code:

<h1>Welcome please log in:</h1>
<?php include_once('loginbox.php'); ?>

If I did the function, I'd just have it <<<EOF out, then do my code...


All times are GMT. The time now is 08:48 PM.

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