Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts

    PHP Includes Basic Help

    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 Code:
    <?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 Code:
    <?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:

    PHP Code:
    <!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.

  • #2
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Sure, that works fine, there's nothing wrong with it.

  • Users who have thanked codyodell for this post:

    Scriptet (10-25-2009)

  • #3
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    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'); ?>

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    It is a good programming practice, other languages aren't as forgiving as PHP.

    However, you could shorten your lines like this:
    Code:
    <?=$var;?>

  • #5
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    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?

  • Users who have thanked Scriptet for this post:

    codyodell (10-25-2009)

  • #6
    Regular Coder FWDrew's Avatar
    Join Date
    Apr 2008
    Location
    Missouri
    Posts
    380
    Thanks
    38
    Thanked 45 Times in 43 Posts
    Quote Originally Posted by Scriptet View Post
    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.
    Dev-Tips.com || Employee at the Envato Network || My Portfolio

  • #7
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts
    You should really add a semi-colon, its good coding practise
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here

  • #8
    New Coder
    Join Date
    Feb 2006
    Posts
    18
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I didn't know that, thanks everyone!

    I have a lot of work to do now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •