mari
February 24, 2010, 1:20pm
1
Hi guys
when using $content = file_get_contents(‘form.php’); and preg_replace together they work fine
however i wanted to add some php in the form.php file and it doesnt pick it up unless i change this
$content = file_get_contents(‘form.php’);
to this
$content = include(‘form.php’);
the php now works in the file but the preg_replace has messed up and the formatting of the form
is there a way i can use php in a file and also preg_replace?
thanks
You can’t do:
$content = include("form.php");
Include includes and evaluates the file. It doesn’t return anything (it’s a statement, not a function).
What problem are you trying to solve? There probably is another, better way.
mari
February 24, 2010, 2:29pm
3
basically i have a template file that is calling form.php
it is currently using $content = file_get_contents(‘form’.php)
form.php only contain html at the moment, i want to add php code to it, The php code is not being picked up
if i change it to $content = include(‘form’.php)
the php is picked up but the formatting breaks
system
February 25, 2010, 9:40am
5
Don’t fall between two stools.
Use only one technique: preg replace or PHP.
Do not mix it.
mari
February 25, 2010, 9:56am
6
so if i got rid of the preg replace and used echo $variable in the form.php would that be right?
system
February 25, 2010, 10:06am
7
yes.
It is called “Native php template system” and widely used nowadays
decowski:
You can’t do:
$content = include("form.php");
Include includes and evaluates the file. It doesn’t return anything (it’s a statement, not a function).
What problem are you trying to solve? There probably is another, better way.
Actually. You can do that.
If the file you include has “return” outside of a function block.
I’ve used that method to bring in an array I exported out with var_dump many of times.
You’re right! I didn’t know that. The point still holds, though: it doesn’t return the included file’s output. It returns, well, whatever you want it to return (which can be the output of the included file with a little help of output buffering).
http://php.net/manual/en/function.include.php:
Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it’s possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
system
February 25, 2010, 12:05pm
10
But OP were asking if it can return something different though
It can, and you can even demonstrate it to show your knowledge.
But the answer remain the same - better not to mix technologies.
I agree. I always use PHP as a template engine. PHP is a template engine. There is no point adding parsing (performance) and readability overhead.