PHP Best Practices

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eholz1

    PHP Best Practices

    Hello, I have a basic design question, in terms of what is "better"
    programming.

    I have a web site, and am using php pages as includes. some of the php
    pages have php tags, and "echo" or "print" the php variables and html.

    Like this:
    <div id="body">
    <?php include("phpinc ludes/server-nav.php"); ?>
    </div>

    the php page has code like this:
    <div id="header">
    <ul>
    <li class="tab" <?php if ($thisPage=="Ho me")
    echo " id=\"currentpag e\""; ?>>
    <a href="http://abf966/LBA/index.php">Home </a>
    </li>
    <li class="tab" <?php if ($thisPage=="In troduction")
    echo " id=\"currentpag e\""; ?>>
    <a href="http://abf966/LBA/intro.php">Intr oduction</a></li...
    etc

    is it better (or not) to not only echo divs, etc but to create
    everything within php tags like this?

    <?php echo ('<h2 class="decco" id="hdrTopic">W ho We Are Not</h2>'); ?>

    OR is this really the same thing, but looks different???

    thanks again,

    ewholz

  • mvandenb@gmail.com

    #2
    Re: PHP Best Practices

    Better... No. Different... Yes.

    Read this:



    better then me just restating what is there!

    Matthew

    eholz1 wrote:
    Hello, I have a basic design question, in terms of what is "better"
    programming.
    >
    I have a web site, and am using php pages as includes. some of the php
    pages have php tags, and "echo" or "print" the php variables and html.
    >
    Like this:
    <div id="body">
    <?php include("phpinc ludes/server-nav.php"); ?>
    </div>
    >
    the php page has code like this:
    <div id="header">
    <ul>
    <li class="tab" <?php if ($thisPage=="Ho me")
    echo " id=\"currentpag e\""; ?>>
    <a href="http://abf966/LBA/index.php">Home </a>
    </li>
    <li class="tab" <?php if ($thisPage=="In troduction")
    echo " id=\"currentpag e\""; ?>>
    <a href="http://abf966/LBA/intro.php">Intr oduction</a></li...
    etc
    >
    is it better (or not) to not only echo divs, etc but to create
    everything within php tags like this?
    >
    <?php echo ('<h2 class="decco" id="hdrTopic">W ho We Are Not</h2>'); ?>
    >
    OR is this really the same thing, but looks different???
    >
    thanks again,
    >
    ewholz

    Comment

    • hackajar@gmail.com

      #3
      Re: PHP Best Practices

      For future programming the following, IMHO, works really well:

      1.) Use "require_once() " instead of "include()" . Some of my
      applications get so harry that for saftey checks I will call a file
      more then once, this protects re-establishing code. Also, require will
      cause fatal error while include will only warn. You probably will want
      to know for sure if a file made it ok.

      2.) If your going to program in this way, use some of PHP's built in
      short cuts:
      Replace - <?php include("phpinc ludes/server-nav.php");?>
      With - <?=require_once ("phpinclude s/server-nav.php")?>
      Notice lack of "php" after "?" instead an "=" and no ";" at end. This
      works and shrinks code ;)

      Cheers,
      Hackajar
      eholz1 wrote:
      Hello, I have a basic design question, in terms of what is "better"
      programming.
      >
      I have a web site, and am using php pages as includes. some of the php
      pages have php tags, and "echo" or "print" the php variables and html.
      >
      Like this:
      <div id="body">
      <?php include("phpinc ludes/server-nav.php"); ?>
      </div>
      >
      the php page has code like this:
      <div id="header">
      <ul>
      <li class="tab" <?php if ($thisPage=="Ho me")
      echo " id=\"currentpag e\""; ?>>
      <a href="http://abf966/LBA/index.php">Home </a>
      </li>
      <li class="tab" <?php if ($thisPage=="In troduction")
      echo " id=\"currentpag e\""; ?>>
      <a href="http://abf966/LBA/intro.php">Intr oduction</a></li...
      etc
      >
      is it better (or not) to not only echo divs, etc but to create
      everything within php tags like this?
      >
      <?php echo ('<h2 class="decco" id="hdrTopic">W ho We Are Not</h2>'); ?>
      >
      OR is this really the same thing, but looks different???
      >
      thanks again,
      >
      ewholz

      Comment

      • Dikkie Dik

        #4
        Re: PHP Best Practices

        >
        Like this:
        <div id="body">
        <?php include("phpinc ludes/server-nav.php"); ?>
        </div>

        I would write this as
        <?php
        require_once(di rname(__FILE__) . '/phpincludes/server-nav.php');
        ?>

        I use double quotes only for newlines and tabs. Also, PHP has a nasty
        tendency to resolve relative paths with respect to the calling page, not
        to the current page. So if this file was called from another directory,
        the include file would be searched relative to _that_ directory first.
        So I always use absolute paths.

        Best regards

        Comment

        • m91.org@gmail.com

          #5
          Re: PHP Best Practices

          Try to check any source code from PEAR distribution. It's very clean.
          And Óan be an example for imitation.


          """eholz1 ÐÉÓÁÌ(Á):
          """
          Hello, I have a basic design question, in terms of what is "better"
          programming.
          >
          I have a web site, and am using php pages as includes. some of the php
          pages have php tags, and "echo" or "print" the php variables and html.
          >
          Like this:
          <div id="body">
          <?php include("phpinc ludes/server-nav.php"); ?>
          </div>
          >
          the php page has code like this:
          <div id="header">
          <ul>
          <li class="tab" <?php if ($thisPage=="Ho me")
          echo " id=\"currentpag e\""; ?>>
          <a href="http://abf966/LBA/index.php">Home </a>
          </li>
          <li class="tab" <?php if ($thisPage=="In troduction")
          echo " id=\"currentpag e\""; ?>>
          <a href="http://abf966/LBA/intro.php">Intr oduction</a></li...
          etc
          >
          is it better (or not) to not only echo divs, etc but to create
          everything within php tags like this?
          >
          <?php echo ('<h2 class="decco" id="hdrTopic">W ho We Are Not</h2>'); ?>
          >
          OR is this really the same thing, but looks different???
          >
          thanks again,
          >
          ewholz

          Comment

          • Jerry Stuckle

            #6
            Re: PHP Best Practices

            hackajar@gmail. com wrote:
            >>Hello, I have a basic design question, in terms of what is "better"
            >>programming .
            >>
            >>I have a web site, and am using php pages as includes. some of the php
            >>pages have php tags, and "echo" or "print" the php variables and html.
            >>
            >>Like this:
            >><div id="body">
            >> <?php include("phpinc ludes/server-nav.php"); ?>
            >></div>
            >>
            >>the php page has code like this:
            >><div id="header">
            > <ul>
            > <li class="tab" <?php if ($thisPage=="Ho me")
            > echo " id=\"currentpag e\""; ?>>
            > <a href="http://abf966/LBA/index.php">Home </a>
            > </li>
            > <li class="tab" <?php if ($thisPage=="In troduction")
            > echo " id=\"currentpag e\""; ?>>
            > <a href="http://abf966/LBA/intro.php">Intr oduction</a></li...
            >>etc
            >>
            >>is it better (or not) to not only echo divs, etc but to create
            >>everything within php tags like this?
            >>
            >><?php echo ('<h2 class="decco" id="hdrTopic">W ho We Are Not</h2>'); ?>
            >>
            >>OR is this really the same thing, but looks different???
            >>
            >>thanks again,
            >>
            >>ewholz
            >
            >
            (Top posting fixed)
            For future programming the following, IMHO, works really well:
            >
            1.) Use "require_once() " instead of "include()" . Some of my
            applications get so harry that for saftey checks I will call a file
            more then once, this protects re-establishing code. Also, require will
            cause fatal error while include will only warn. You probably will want
            to know for sure if a file made it ok.
            >
            2.) If your going to program in this way, use some of PHP's built in
            short cuts:
            Replace - <?php include("phpinc ludes/server-nav.php");?>
            With - <?=require_once ("phpinclude s/server-nav.php")?>
            Notice lack of "php" after "?" instead an "=" and no ";" at end. This
            works and shrinks code ;)
            >
            And fails on any host which has short_open_tags turned off, which is
            more and more common. Much better to use <?php.
            Cheers,
            Hackajar
            eholz1 wrote:
            >
            P.S. Please don't top post.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...