Starting PHP page 2. To include instead of hello world :)

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Ok you have your development system ready?


What's next? You probably suspect some kind of php hello world tutorial right?
Wrong. I don't like hello world. So instead we are going to do something useful already.

Now before I continue, Ask yourself if you know html and CSS?
If not then please first learn html and css before you learn php.

If you know html and css great. Lets talk about includes.

What are includes?
Why are they useful?
How do I use them?
What are includes?
The php include (); function (don't worry if you don't understand this)
will include a page into another and when you call the page with the includes
it will be rendered by your browser as one page.

Why are they useful?
Imagine you have created a website with 150 pages. They all have the same header
Now some marketing guy changed the company's logo and name. And you need to change the website.
The logo is ok you can simply change it but the text that is right under the logo is something else.
You need to change it on each pages. wouldn't it be fun that you could just change the text one time?
include () will be the solution.
How to use it?

As an example write a simple html page.

<html>
<head>
<title>Hello world (hmmm: did I say I don't like it?)</title>
</head>
<body>
This is the website of howtoforge.bla bla bla bla bla

copyright howtoforge
</body>
</html>

Now imagine that "This is the website of howtoforge" is a text that is on every page.
And that "copyright howtoforge" is also a sentence that is on every page.

and this html file is called index.html.

Lets split it up.
Rename the file to index.php instead.
Copy out the part :
<html>
<head>
<title>Hello world (hmmm: did I say I don't like it?)</title>
</head>
<body>
This is the website of howtoforge.

And paste it into a file called header.php

Copy the part
copyright howtoforge
</body>
</html>

and paste it into a file called footer.php

I simply use footer and header as file name because it is clear that they will be the header and footer.
You could name it whatever you like.

index.php will look like this:

bla bla bla bla bla

Lets start including the header and footer.

you can add php code right into this file by using the opening tag
<?php
and closing tag ?>.
All you want to do with php code will be between those tags.

So we want to use an include function which is a php function we will need to open the tag <?php
Between these tags we will write include("<path>/<file name>") where <path> is the path to the file to include and <file name> is guess what. The file name of the file you want to include.

So or index.php file will look like this:

<?php
include ("./header.php");
?>bla bla bla bla bla

<?php
include ("./footer.php");
?>

notice the ;
In php you need to add a ; after each command you give. In this case or command is a function call to include.

Some advanced issues.

Imagine that you also use includes in the header.php file than you have to be careful if you do not include the same stuff twice.
In this case it won't affect anything but from the moment you start writing functions of you own and you include them it could happen that you try to define the function twice because of a double include.

you can solve this by using include_once(). This will check if the file is already included or not. If it is it will not include it any more.

More information about php functions can be found at www.php.net

Use this site. and also use google. These are the best tips I can give.

good luck.



Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Wed, 2006-02-15 21:58.

Speaking of knowing HTML, don't forget your paragraph tags! Otherwise, all those lines in your HTML will be inline with each other.

Thanks for this howto!

-JazzCrazed
www.jazzcrazed.com

Sponsored Links: Unified Communications: Thoughts, Strategies and Predictions
Join the discussion.
www.seamlessenterprise.com

IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com

Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com

Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com

Red Hat Virtual Experience - a free virtual event. Dec. 9th