Breaking up PHP Websites - php

I am wondering how I can break up my index.php homepage to multiple php pages (i.e. header.php, footer.php) and build a working index.php page using those separate php pages. I know WordPress uses this with different functions like:
GetHeader();
GetFoodter();
But when I tried to use those functions, it errors. I am guessing they are not native functions to PHP.
What would I need to do to get this functionality?

include 'header.php';
include 'footer.php';

Go with an MVC framework like Zend's. That way you'll keep more maintainable code.

You could do the following:
<?php
include('header.php');
// Template Processing Code
include('footer.php');
?>

The include() statement includes and evaluates the specified file.
so if you create index.php as:
<?php
include("1.php"); include("2.php"); include("3.php");
?>
processing it will combine three php files (result of parsing them by php) into output of your index.php ... check more at http://pl.php.net/manual/pl/function.include.php

Also, if i recall correctly, you can also use
<?php
require('filename');
?>
the difference being, if php can't find the file you want to include, it will stop right there instead of keep excecuting the script...

If your server is configured accordingly, you can use PHP's built in auto append/prepend settings and set it in a .htaccess file:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
Info:
www.php.net/manual/en/configuration.changes.php#configuration.changes.apache
www.php.net/ini.core#ini.auto-prepend-file
www.php.net/ini.core#ini.auto-append-file

I realize this is an old question, which already has a perfectly valid accepted answer, but I wanted to add a little more information.
While include 'file.php'; is fine on it's own, there are benefits to wrapping these sorts of things up in functions, such as providing scope.
I'm somewhat new to PHP, so last night I was playing with breaking things into files such as 'header.php', 'footer.php', 'menu.php' for the first time.
One issue I had was that I wanted to have the menu item for a page/section highlighted differently when you were on that page or in that section. I.e. the same way 'Questions' is highlighted in orange on this page on StackOverflow. I could define a variable on each page which would be used in the include, but this made the variable sort of global. If you wrap the include in a function, you can define variables with local scope to handle it.

You could also look into a template engine like Smarty. That way you define the the header and footer and all other common elements in a single file, then fill in the rest through smaller templates or direct output.

Use include statements to just include those files to your Page
I think it's
include '[filename]'

Related

html extract header and footer to separate documents

I have several html documents which share the same header and footer and would like to extract header and footer to separate html files and include them to other html documents.
The idea is very simple: when I change something in header, the change has to be made on all pages.
Until now I used PHP include function. But I have read some articles that this has impact on performance.
What is the best way to do this for clean html pages ?
Is the PHP the way to go ?
If it is, should I insert the whole html content into PHP echo ?
There isn't really a good way doing it in pure HTML, since iframe or an ajax-request using Javascript wouldn't be a good solution here.
I would say the best way is to use the PHP include or PHP require-function:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
You won't see any performance issues. Remember that include is a little bit faster than include_once, since include_once will have to make an extra check if the file has already been included.
You want to include header in all html pages, so php include is better.
no need of echo, just create a header.html(or any html file).
Then use
<?php
include "header.html"; //or name of your headerfile
?>
add this php code to all your pages.Thus all pages will point to same header file, and i dont think it will affect performance. Same idea for footer too :)

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

In a PHP webpage, reading text from an external PHP to make updating content across pages easy

I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to #br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.
<p><?php footertext.php ?></p>
is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).
How can I go about doing this? I'd prefer an answer involving PHP.
UPDATE:
This is the exact code I'm using. Everything is in the same directory.
Main File:
<html>
<head>
</head>
<body>
<p> Content: <?php include "footertext.php ?></p>
</body>
</html>
Footer Content:
echo 'Test numba TWO!';
Use the include function. Just give it the path to your file. Example:
<?php include "footertext.php"; ?>
There are a few other functions that do similar things, such as require_once(). You can read more about that here.
Response to update
You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.
The best fit solution for your query is include or require functions of php. now you need to identify which one out of those are your choices based on their functional behavior.
PHP include and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Syntax
include 'filename.ext';
or
require 'filename';
You may like to go through the details of
Include,
Require,
Require_once &
Include_once.
Enjoy!
Anand Chavan

HTML treat code within brackets as PHP code

I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.

How does wordpress know about its functions?

Specifically in theme php files, such as say index.php. The very first thing in most theme's index.php file is a call to get_header() which is most certainly not not defined in index.php, so how does it know about that function?
I'm not very familiar with php, but from what I've read just now there is an include and require keyword which work more or less the same way as an import in Java or include in C, which I understand and makes sense. However, the only usage of these keywords in this particular index.php file includes a file that doesn't contain a definition of get_header(), nor does it have any includes or requires of its own (though it does call some more functions it has no right to know about, much like index) so clearly that's now how it knows about this function.
Anyway, I was just hoping to remove some of the 'magic' from wordpress for myself. Thanks in advance!
The index.php in each theme isn't ever called directly, rather those are included by other files in Wordpress.
get_header is actually defined in wp-includes/general-template.php.
The templates files are loaded by require_once function calls in the load_template function of wp-includes/theme.php
Themes' index.php file is not the main file that's processed, that one resides in the root directory of your wordpress installation. It calls several files setting up the environment and then loads the template. You might want to look in the wp-content/plugins directory, maybe starting with the globals.php file to tweak some of the magic.
Also the wp-includes directory contains interesting files, the get_header() function is defined in general-template.php in that directory.
Specifically in theme php files, such as say index.php. The very first thing in most theme's index.php file is a call to get_header() which is most certainly not not defined in index.php, so how does it know about that function?
A theme's index.php file is never executed on its own. All requests to a WordPress install go through the main WordPress index.php file.
Most likely, index.php is being included in another file and that file either defines the get_header() function or - more likely - contains yet another included php file which in turns defines the get_header() function.
You have to understand that the php include and require functions behave like an in-line include. It simply treats the include file as being part of the original script.. a big concatenated script (so not really like an import in Java)
You may want to start at the .htaccess file which will tell you which .php file is assigned to handle the request. Based on what you said in your question, it will probably not be index.php. When you find the top-most php script, you can work your way from there with the includes and requires..
in c and in java you declare the import at the top of you files. In php you can set the includes almost anyware you want.
so this is valid:
<?php
// define some functions
?>
<html>
<head>
<?php
include('head.php');
?>
</head>
<body></body></html>
it is possible that these get_header() methods are declared somewhere before the index.php page is included.
If you want to leave out the <?php get_header(); ?> then there will be no problem

Categories