Php 101.

Discussion in 'PHP' started by Lord Matt, Feb 6, 2007.

  1. #1
    This post assumes you know less than nothing about PHP but want to make a simple page.

    php will generally only work when the file ends .php which means that all those .htm and .html files will need to be renamed to take advantage of php.

    You can call a pure html file WHATEVER.php and it will display just as before. The .php adds nothing of itself but does allow us to add php code later (and have it work).

    The first thing to be introduced to is the php tag.

    <?php opens php while
    ?> closes it again.

    Now let us say that you want to make an easy to manage site but it now has 12 pages. It's getting hard work to change the HTML on each of the 12 pages.

    We can fix this with the power of the include command.

    We can make some new files and keep all the common parts in one place.

    Let's call this file head.php

    I'm going to imagin we have some html for making the top part of the page.

    it might look like this.

    <div class="box" id="myhead">
    <img src="http://mysite.com/images/mygreatlogo.png" />
    <h1>This is my GREAT site</h1>
    </div>
    PHP:
    You will, no doubt, notice the lack of php tags. This is because there is no php.

    Now for that pesky menu that we have to update on every page whenever we add a new section.

    Maybe it looks like this.

    <div id="mymenu">
    <ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="mypage.php">my 1st page</a></li>
    <li><a href="mypage2.php">my 2nd page</a></li>
    <li><a href="faq.php">FAQ</a></li>
    <li><a href="./blog/">my premade blog script</a></li>
    <li><a href="http://lordmatt.co.uk">Lord Matt's site</a></li>
    </ul>
    </div>
    PHP:
    How does this help us then.

    on each of our pages we can add this:

    <html>
    <head>
    <!--my meta tags needs to be written-->
    </head>
    <body>
    <?php include ("head.php"); ?>
    <div style="float:left;" id="sidebar">
    <?php include ("menu.php"); ?>
    </div>
    <div id="maintext">
    <!-- my content needs to be written -->
    </div>
    PHP:
    Now when you update your menu every page that holds the menu changes. This makes expanding your msotly static site much simpler.

    Small promo - find more of this sort of thing on my blog (category: php 101) http://lordmatt.co.uk/blog/1/category/38/
     
    Lord Matt, Feb 6, 2007 IP
  2. stickycarrots

    stickycarrots Peon

    Messages:
    4,513
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    thanks for the help
     
    stickycarrots, Feb 6, 2007 IP
  3. alfredn

    alfredn Peon

    Messages:
    1,198
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    great for beginning php users; nice read
     
    alfredn, Feb 6, 2007 IP
  4. Mawt

    Mawt Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Great information for the begainers :)
    And good to brush up on anyones skills haha
     
    Mawt, Feb 7, 2007 IP
  5. PHP5-Hosting

    PHP5-Hosting Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good info!
     
    PHP5-Hosting, Feb 7, 2007 IP