You're looking to use eval [php.net] however there are greater concerns using it at a server than at a client so do some extra research and make sure you absolutely trust where that code is coming from.
Welcome to WebmasterWorld, easypro.
Also, rather than use file_get_contents() you can just include() page2.php instead. And if you need to manipulate the contents you can use output buffering to store the data in a variable first using ob_start() and friends.
thanks for all the great info guys! I actually did try using include() and ob_start() but couldn't get it to work right. I probably just don't know what I'm doing since I have never used ob_start() before.
also, I don't really see how eval() is any more risky than include() - either way the code is being executed, so if there was malicious code being loaded, it executes both ways.
or am I missing something? sorry, I'm still an intermediate coder.
I actually got it to work by using this:
$Content = file_get_contents("../folder/page2.php");
$Content = str_replace('pages/','../folder/pages/',$Content);
$Content = "?> ".$Content;
HI there easypro,
A warm welcome to WebmasterWorld!
>><div id="header"><? include("pages/header.php"); ?></div>
This is poor coding, please declare that you are actually using php so that the parser can read what it is meant to!
<div id="header"><?php include("pages/header.php"); php?></div>
Matthew1980 is correct, you should use the full PHP declaration.
Glad you got it working and welcome to the forums!
welcome to WebmasterWorld, easypro!
As far as I can tell you're supposed to use
|When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. |
I believe the php?> closing to be in error.
I used "<?php" when I first started coding years ago, but then just changed it to the shorter version "<?" and I have never had a problems with it in the past 4 years. Kinda like CSS when you use #000 instead of #000000.
The problem with using <? is there's a flag for it in the php.ini file which your server administrator needs to turn on for it to work. While your code works on your current box, if you change hosts or anything of the like, your code could just stop working.
For the sake of 3 additional keypresses each time you start a new file, you keep your code much more portable.
Get a copy of Advanced Find and Replace, yeah it's $20 but I use it every day I code. Find "<?" and replace with "<?php" and it'll save you hours of time. While I suppose "<?" could be called shorthand it really doesn't apply to PHP like it does CSS.
The stricter your code the better and much more responsive you'll become. Turn up error-reporting to it's maximum setting in PHP...
...and make sure you address all errors including when variables aren't set and are referred to.
Create an .xhtml file on your desktop (from a Notepad template) and open it in Firefox: broken XML. You can code strict or loose, and strict might take a little longer to learn though you'll be so much more capable in the long term.
|For the sake of 3 additional keypresses each time you start a new file |
You never break out of PHP?
|You never break out of PHP? |
I like to split my pages into a PHP script and a template file. In the PHP file I almost never break PHP. In fairness though, there's a lot of things like this in the templates:
<h2><?php echo utils::pageReadyString($page->content['title']); ?></h2>
But those templates are often shared between more than one type of page. E.g., news and information pages both share the same template file as they both have a title, a sub-navigation menu and a body, and I made sure they use the same variable names. News then just includes another template for user comments. Makes it easier to maintain a consistent feel through the site.
So the answer to your question is: "it depends" :)
But yes, point taken, I should have said "for the sake of 3 additional keypresses each time you enter PHP".
|You never break out of PHP? |
I don't if I want/need "flat out fast" processing speed... Check out the PHP Benchmarking Results [webmasterworld.com] in the Library. (Concatenation, accumulation, single quotes, single echo = screamin fast)
It's good that it's working for you. But I don't recommend you to use eval as I don't see any need.
I like simple code. You can put all that content of php2 in a function and pass the value when calling it. In that function accept the value in a variable and use that variable where you want to use.