473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</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">Who We Are Not</h2>'); ?>

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

thanks again,

ewholz

Dec 22 '06 #1
5 1507
Better... No. Different... Yes.

Read this:

http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

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("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</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">Who We Are Not</h2>'); ?>

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

thanks again,

ewholz
Dec 22 '06 #2
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("phpincludes/server-nav.php");?>
With - <?=require_once("phpincludes/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("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</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">Who We Are Not</h2>'); ?>

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

thanks again,

ewholz
Dec 25 '06 #3
>
Like this:
<div id="body">
<?php include("phpincludes/server-nav.php"); ?>
</div>

I would write this as
<?php
require_once(dirname(__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
Dec 25 '06 #4
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("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</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">Who We Are Not</h2>'); ?>

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

thanks again,

ewholz
Dec 26 '06 #5
ha******@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("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id=\"currentpage\""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</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">Who 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("phpincludes/server-nav.php");?>
With - <?=require_once("phpincludes/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.
js*******@attglobal.net
==================
Dec 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: byrocat | last post by:
I'm chasing after a documetn that was available on one of the Microsoft websites that was titled somethign like "MS SQL Server Best Practices" and detailed a nyumber of best practices about...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
13
by: john doe | last post by:
A quick question, about so-called 'best practices', I'm interested in which of A/B of the two examples people would choose, and why. public enum MyEnum { Option1 = 0, Option2 = 1, Option3 =...
1
by: | last post by:
Hi can someone send or point me to Any nice Material on .NET Best Practices -regards
2
by: Amelyan | last post by:
Could anyone recommend a book (or a web site) that defines best practices in ASP.NET application development? E.g. 1) Precede your control id's with type of control btnSubmit, txtName, etc. 2)...
4
by: Luis Esteban Valencia | last post by:
Hello. Can somebody recomend me books of design patterns in c# and best practices too.
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
0
by: Louis Aslett | last post by:
I hope this is the correct newsgroup for this query (if not please give me a pointer to where is best): I understand the theory of normalisation etc and am trying to follow best practices in the...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
3
by: John Dalberg | last post by:
I am looking for an ASP.NET application on CodePlex which exemplifies best practices for the following: - Use of interfaces - Seperation of the UI, business and data tiers - Data Tier that uses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.