RAGARD

Tutorials

PHP Tricks for Non-Programmer

by ne-design, Aug 19th, 2008 in Tutorials,

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.

©  <?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.

<!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.

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

<?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.

<?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.

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

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

<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.

  • Twitter
  • Facebook
  • StumbleUpon
  • Design Float
  • del.icio.us
  • Digg
  • Reddit
  • Technorati
  • RSS

11 Comments

  1. John Deszell says:

    Great Tricks I learned some new things.

  2. ZEUS__ says:

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

  3. Miguel says:

    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 says:

    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 says:

    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 says:

    awesome post man i finally got it thanks

  7. Trucos básicos en PHP para tu sitio web says:

    [...] 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 says:

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

  9. El CoDiGo K » Trucos básico en PHP para tu sitio Web says:

    [...] 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 says:

    Excelent!!! Keep Going MAN!!!!

  11. Janckos says:

    you should write more tricks! =D

Leave a Reply

Banner Ad Banner Ad