Jump to content

Recommended Posts

Hello,

New guy here (sorry), I'm trying this but does not work:

 

<?php

$foo='<html>

<head>

<title>some title</title>

</head>

 

<body>

<table>

<tr><td>

some text '.<?php include("file.php"); ?>.' some more text

</tr></td>

</table>

</body>

</html>';

 

echo $foo;

?>

 

I want to display file.php in between some html code.

Please help. I've spent about two hours looking but nothing.

 

Thank you in advance,

Jesse

Link to comment
https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/
Share on other sites

Thanks Redixx for the rapid response.

 

I've tried your suggestion, just that it outputs everything including to top secret php code ;) on the webpage when it's called. I at least get something now :D, thank you.

 

Any suggestions?

 

I assumed that's what you wanted to happen.

 

You can't assign php code to a variable, unless you plan to use the exec() function or something (which is a generally ill-advised).

 

Why not just echo the HTML and then include the page, instead of assigning it to a variable and then echoing it. What is it exactly you are trying to do?

For what it's worth, you should probably review the manual for include():

http://www.php.net/manual/en/function.include.php

 

They provide a few examples of how it can be used. Also note that include isn't a function, so you don't need the parenthesis.

 

 

require() is pretty much the same as include() except that it throws a fatal error if require() fails:

http://www.php.net/manual/en/function.require.php

I guess this is what I'm trying to do:

 

*******

form.php

*******

 

some php code...

<form>

some php and html ...

php form...

</form>

 

 

-----------------------

 

 

******

config.php

******

 

<?php

$foo='<table>

<tr><td>

// I would like to get the contents of form.php and add the to the rest of the contents of $foo.

some text ' . file_get_contents("file.php") . ' some more text

</tr></td>

</table>';

?>

 

 

-----------------------

 

 

******

file.php

******

<?php include("config.php"); ?>

<html>

<head>

<?php

// Some php code here.

?>

<title>title</title>

</head>

<body>

Please fill out our form.<br>

<?php echo $foo; ?>

<?php

// Some php code here too.

?>

</body>

</html>

 

 

cyberRobot: Thank you for your help. I've gone over it but nothing in it seems to help.

@jdrjsbl - First, let's ignore form.php. If you remove that include(), does file.php echo the $foo variable as expected?

 

 

******

config.php

******

 

<?php
$foo='<table>
<tr><td>
// I would like to get the contents of form.php and add the to the rest of the contents of $foo.
some text -- form.php goes here eventually -- some more text
</tr></td>
</table>';
?>

 

 

******

file.php

******

 

<?php include("config.php"); ?>
<html>
<head>
<?php
// Some php code here.
?>
<title>title</title>
</head>
<body>
Please fill out our form.<br>
<?php echo $foo; ?>
<?php
// Some php code here too.
?>
</body>
</html>

*******

form.php

*******

<?php
$form='
some php code...
<form>
some php and html ...
php form...
</form>';
?>

 

 

******

config.php

******

<?php
include 'form.php';
$foo='<table>
<tr><td>
// I would like to get the contents of form.php and add the to the rest of the contents of $foo.
some text ' .$form. ' some more text
</tr></td>
</table>';
?>


 

 

******

file.php

******

<?php include("config.php"); ?>
<html>
<head>
<?php
// Some php code here.
?>
<title>title</title>
</head>
<body>
Please fill out our form.<br>
<?php echo $foo; 
// Some php code here too.
?>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.