A simple PHP code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

A simple PHP code...

Post by Berethorn »

I am basically new to PHP. I know HTML well, but what I needed was a way to insert an html page within a page. My website will have many many pages, all with the same header and footer, and I don't want to have to change all of those pages just to make a simple change to the header or footer. So I looked for a solution, and I think I found it - the PHP "require".

So I need some tips,

If all I use is this code:

Code: Select all

<?php
require ("header.php");
require ("footer.php");
?>
do I need to change the extension of the parent page from .html to .php?
And do I have to change the "required" pages from .html to .php, even if they contain no PHP whatsoever?

What tags in the the HTML document must be changed to support PHP, e.g. When I had

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
in the code it didn't work.

And are there any compatibility issues with using PHP? Do any old browsers mess up when they read it?

How do I change the layout of the ?php tag with CSS or HTML, to adjust the width, etc? :)
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

do I need to change the extension of the parent page from .html to .php?
As far as I know that depends on the server-configuration. My experience (lol) is that it's a good thing to do.
And do I have to change the "required" pages from .html to .php, even if they contain no PHP whatsoever?
No, you don't. But then you need to require (or
include) them as such.

You can include any php code in an HTML-document, as long as you put it between the PHP-code tags:
<?php for opening
?> for closing

But you'll have to be sure your ISP supports PHP for your hosting account. If you don't have PHP-support in your hosting-account, you'll need a new ISP.
How do I change the layout of the ?php tag with CSS or HTML, to adjust the width, etc?
I don't understand what you mean here...sorry.
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

I believe you'll have to change the extension of all files containing php, from .html to .php. Otherwise the server, regardless of whether it supports php or not, will serve up what it thinks is pure html. So, the php code will be output in the browser, as it's written in the file.

The .php extension tells the server that there's php code in the file and it needs to be run through the php interpreter before being sent to the client.

This is my experience anyway.
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Ok, thanks!

I do know my web host supports PHP, and I have tried the said code, and it worked.

What I mean by the last question is, when I insert the header into my page, how do I configure the width or height of it? Right now it stretches across the whole page, but what if I don't want it too. Do I use PHP or HTML for this? :)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Everything regarded to design or appearance of your page is related to HTML, PHP has nothing to do with it. PHP is only server-side and sends the HTML back to the browser. What you see and how it appears is HTML.

Cheers,
Scorphus.
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Ok, I finally get it... :D

And is there any problems with browser compatability?

in any case it is darn better than "iframes" for what I need it for. Thanks, PHP! :D
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

Everything regarded to design or appearance of your page is related to HTML, PHP has nothing to do with it. PHP is only server-side and sends the HTML back to the browser. What you see and how it appears is HTML.
What he said...

So, you can either use php to output the formatting or use a css to control formatting. I personally use css, by defining classes is the tags for which I want to control formatting.

Very Simple Example:

Using PHP to create the HTML:

Code: Select all

// Create a hard rule divider...
        print("<TR>\n");
        echo "<TD class="header">\n";
        echo "<HR>\n";
        print("</TD>\n");
        print("</TR>\n");
What the CSS will look like this:

Code: Select all

/* Header Appearance */
.header &#123;
    text-align: center;
    border-width: 0pt;
&#125;
I find it easier to control formatting from a central location (i.e. css), rather than sticking the formatting in the PHP code.[/b]
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

As far as browser compatibility is concerned, PHP is a server side scripting language...the html is created on the server and sent to the client. The only browser compatibility issues that could arise are dependant on the type of HTML you choose to output in your code (i.e. making sure you use tags that are supported by all browsers).

The browser doesn't have any idea how the html was created, it just renders it. It doesn't know if it was PHP, ASP or JSP...or even just a static page.

Browsers are dumb.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Berethorn wrote:And is there any problems with browser compatability?
PHP has nothing to do with browser compatibility nor page formating.

Regarded to PHP, the only compatibility will be among the server. Let's say you want to use the mysql_stat(). It requires PHP version 4.3.0 or superior. So your server must be equiped with PHP 4.3.0 or superior.

Cheers,
Scorphus.
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Ok, thanks guys - that answers all my questions. It'll be a really big help to my site. :D
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Oh, one more question: can I use this in my parent.php file?

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You forgot the:

Code: Select all

&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
For PHP write a XML/XHTML document you must do:

Code: Select all

<?php echo "<?xml version="1.0" encoding="iso-8859-1"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Regards,
Scorphus.
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Oh, great. the ?xml tag had given me trouble. :)

BTW, love your sig. :D
User avatar
Berethorn
Forum Commoner
Posts: 30
Joined: Wed Oct 01, 2003 9:39 am
Location: The Frozen North

Post by Berethorn »

Is there some way I can view .php pages on my computer? A program I can download or something I can install? :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, it's called php and can be downloaded from http://php.net/downloads ;-)
Post Reply