PHP Tricks for Non-Programmer

There are several PHP tricks you can use easily on your websites. I do recommend reading Jeffery Way’s “Learn PHP from Scratch: A Training Regimen” at NETUTS. These tricks will make it easy for you to maintain your website. If you have used a CMS like WordPress before this might be nothing new.


Automatic Copyrights

Most of us have our copyrights on the bottom with the year labeled. Let’s make this year label dynamic so you don’t have to keep on changing it every year.

1
©  <?php echo date('Y'); ?> RAGARD. All Rights Reserved.

Pretty simple. Basically date(’Y') prints out the year in 4 digit. The outcome would be like:

© 2008 RAGARD. All Rights Reserved.

Easy Template

You don’t want to keep changing each file when you have a common header, sidebar or footer. Let’s make it a template so we don’t have to go over every single page to change a menu.

First, let’s start off cutting out the common part and make a new file. In this example we will use the heading as our common part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
 
<title>Site Title</title>
 
<div id="header">
<h1>Site Title</h1>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/about/">About</a></li>
<li><a href="/contact/">Contact</a></li>
</ul>
</div>

We will save this file as header.php. Next we write this php script on the page where the header will be needed.

1
<?php include('header.php'); ?>

You’re done! Same thing will apply for sidebar, footer and any other parts you have in common.

Manage Information in One File

Sometimes you might have a same kind of information in a different parts or pages. Some example could be like e-mail address, phone number, address and name. Have you ever thought having these information in one file to make changes easier?

Start making a file name information.php with the php script like this:

1
2
3
4
5
<?php
$name = 'ne-design';
$tel = '(000) 000-0000';
$email = 'sample@ragard-jp.com';
?>

Now that we have some information we could use, insert the following code on every page you might use this information.

1
<?php include('information.php'); ?>

Everything is now ready to go. All we need is to write a script to echo the information. Here’s the sample of echo.

1
<?php echo $email; ?>

From now on you will just need to change your information.php to change your e-mail. This is actually the basic of how PHP works.

Example

Some people were getting confused on how the echo would be used. In this example, you will see the purpose for echo is to tell where you want the certain information to be displayed. Instead of writing ne-design, I would write <?php echo $name; ?>.

Here is an example of my index.php:

1
2
3
4
<?php include('information.php'); ?>
 
<p>Hi my name is <?php echo $name; ?>!<br />
Need any help? You could contact me by calling <?php echo $tel; ?> or email me to <?php echo $email; ?>.</p>

The outcome for this would be:

1
2
<p>Hi my name is ne-design!<br />
Need any help? You could contact me by calling (000) 000-0000 or email me to sample@ragard-jp.com.</p>

I will be revising this php trick post to help you guys.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google
  • StumbleUpon
  • Facebook
  • Furl
  • Mixx
  • Technorati
  • Blue Dot
  • Design Float
  • Live
  • Ma.gnolia
  • Reddit
  • TwitThis

Tags:

Comments

  1. John Deszell
    August 20th, 2008

    Great Tricks I learned some new things.

  2. ZEUS__
    August 20th, 2008

    Thanks!!but please don’t stop..I love sugar candy tricks so much:)

  3. Miguel
    August 21st, 2008

    Hi your tutorial on how to Manage Information in One File is exactly what i’ve been looking however am a rookie right now when it comes to php and for some reason i can’t get this thing to work could you give a bit more info on how to get it done?

  4. ne-design
    August 21st, 2008

    Thanks for your comments!
    I’m really glad that this tutorial helped you guys.

    @Miguel - No problem. What kind of information would you like to have?

  5. Shannon Snow
    August 22nd, 2008

    Great post! I am a php rookie as well, a designer trying to learn more developer tools and tricks. I understand the php file, the include tag, but why the echo tag? Where does that go and why do you need it when you already used the include tag? Thanks for your help!

  6. Miguel
    August 23rd, 2008

    awesome post man i finally got it thanks

  7. Trucos básicos en PHP para tu sitio web
    August 27th, 2008

    [...] no eres programador y conoces poco de códigos, este artículo “PHP Tricks for Non-Programmer” dedicado a los que no suelen programar pero necesitan en algún momento de trucos para hacer [...]

  8. What Are the Best Sources of Design Community News? | Vandelay Website Design
    August 27th, 2008

    [...] PHP Tricks for the Non-Programmer [...]

  9. El CoDiGo K » Trucos básico en PHP para tu sitio Web
    August 27th, 2008

    [...] web. En esta oportunidad encuentro en MaestrosDelWeb la redirección a un artículo llamado “PHP Tricks for Non-Programmer” dedicado a los programadores del magnífico lenguaje de programación PHP indicando algunos [...]

  10. Ariel
    August 28th, 2008

    Excelent!!! Keep Going MAN!!!!

  11. Janckos
    September 6th, 2008

    you should write more tricks! =D

Leave A Comment