...

View Full Version : PHP Includes Basic Help



Scriptet
10-25-2009, 12:29 AM
Hi,

I want to use a PHP include for the header of my document. So I include the header into my pages like this:


<?php include('header.php'); ?>
//Page Contents Go Here


OK now I want unique titles/desciptions/keywords for each page, is this the correct way to go about it:

MyPage:


<?php
$title = "Page title would go here";
$desc = "Page description would go here";
$keywords = "Keywords, would go, here";
include('header.php');
?>
//Page Contents Go Here


Then header.php would be:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="<?php echo $desc; ?>">
<meta name="keywords" content="<?php echo $keywords;?>">
</head>
<body>
<div id="header">
<h1>blah blah</h1>
</div>


Is this a correct way to go about it, meaning does everyone do it this way? Defining PHP variables like I am before my Doctype won't send the page into quirks mode will it?

Thanks for any help, it's really appreciated.

codyodell
10-25-2009, 12:42 AM
Sure, that works fine, there's nothing wrong with it.

Scriptet
10-25-2009, 01:19 AM
OK Thanks i'll take your word for it!!

Just one other last quick question whilst i'm here. I know there's no need to add a ; after the last statement in a PHP block, as it's always executed.

Therefore is it OK if I omit this, saves on a character I guess and maybe a coding habit to pick up? e.g

<?php echo "hello" ?>
and not
<?php echo "hello"; ?>

<?php include('header.php') ?>
and not
<?php include('header.php'); ?>

codyodell
10-25-2009, 01:29 AM
It is a good programming practice, other languages aren't as forgiving as PHP.

However, you could shorten your lines like this:


<?=$var;?>

Scriptet
10-25-2009, 01:52 AM
I used to code like this, I think short-tags are great, but then I was informed (on here I think) that short-tags are to be removed as of PHP 6? So when all of my hosts, and my clients hosts upgrade to PHP 6, won't everything go to pot?

FWDrew
10-25-2009, 04:19 AM
I used to code like this, I think short-tags are great, but then I was informed (on here I think) that short-tags are to be removed as of PHP 6? So when all of my hosts, and my clients hosts upgrade to PHP 6, won't everything go to pot?
Yup, using short tags is just lazy and a good way to break your application whenever a server is changed or upgraded.

karlosio
10-25-2009, 04:44 AM
You should really add a semi-colon, its good coding practise

codyodell
10-25-2009, 05:05 AM
I didn't know that, thanks everyone!

I have a lot of work to do now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum