Home » Useful Tools

6 Useful PHP Snippets For Beginners

Published on March 19, 2008 3 CommentsPrint This Post Print This Post

For the past few months I’ve been reading books and going through CDs on how to program in PHP. During this time I’ve come across some useful snippets of PHP code that I’ve had use for in some of my projects. I present to you six of them here. These are very basic scripts that PHP beginners may find use for.

Useful PHP snippets

Include a PHP File

Code:

1
2
3
4
5
<?php 
 
   include "yourfile.php"; 
 
?>

Usage: This script adds the contents of “yourfile.php” to your current file. Simply replace “yourfile.php” with the your desired file name. This is a great time-saving tool when you have many pages that use a common header or footer.

PHP Redirect

Code:

1
2
3
4
5
6
7
<?php 
 
   $URL="http://www.redirecthere.com/";  //URL to be redirected to 
 
   header ("Location: $URL"); 
 
?>

Usage: Place the above code in a blank php file and replace the $URL value with the URL that you want that page to redirect to. The visitor will usually not be able to notice they’ve been redirected (other then the new URL in their browser). It’s an easy fix if you’ve changed a page’s location.

Send Mail Notification

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 
   $to = "admin@yoursite.com"; 
   $subject = "Your Subject Here"; 
   $message = "Your Message Here"; 
 
   //mail headers 
   $headers = "MIME-Version: 1.0\n"; 
   $headers .= "Content-type: text/plain; charset=iso-8859-1\n"; 
   $headers .= "X-Priority: 3\n"; 
   $headers .= "X-MSMail-Priority: Normal\n"; 
   $headers .= "X-Mailer: php\n"; 
   $headers .= "From: \"Admin\" <admin@yoursite.com>\n"; 
   $headers .= "Return-Path: admin@yoursite.com\n"; 
   $headers .= "Return-Receipt-To: admin@yoursite.com\n"; 
 
   mail($to,$subject,$message,$headers); 
?>

Usage: This is a great way to send an email when a form has been submitted or when a page loads. Simply replace “admin@yourside.com” with the email address you want the message sent to. The $message can handle variables too.

Get Current Page

Code:

1
2
3
4
5
6
<?php
   function curPageName() {
        return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
        }
   $page = curPageName();
?>

Usage: This is an easy way to use the current pages file name as a variable. This could be used for setting a navigations class as “active” as in the code below:

Code:

1
2
3
4
5
6
7
<ul>
   <li <?php if (
          $page == "index.php"){ 
           echo 'class="active"';} ?> >
        <a href="index.php">My Link</a>
   </li>
</ul>

Get Variable From URL

Code:

1
2
3
4
5
6
7
<?php
   if (!isset($_GET['URLvariable'])) {
       $variable = "your stuff"; // Default value
    } else {
       $variable = $_GET['URLvariable'];
       }
?>

Usage: This is used to pass variables from the URL into local variables in your PHP document. This can be used when processing forms or can even be used control iframes or other things.

Remove Non-alphanumeric Characters

Code:

1
2
3
4
5
6
7
<?php 
   $string = "Text with #*#* sybols and 13489 #numbers"; 
   $new_string = ereg_replace("[^A-Za-z0-9]", "", $string); 
 
echo $new_string 
 
?>

Usage: This script removes all non-alphanumeric characters (anything besides letters or numbers) from a given string. This is useful for “cleaning” given text input.

Useful Articles

This article was written by John Kolbert on March 19, 2008 and filed as Useful Tools. Get the latest articles by subscribing to the RSS feed. This article, including images and attachments, is property of John Kolbert and is not to be republished or translated without prior written permission.

Post Toolbox

Share It


Print It

3 Comments »

  • Stefanie said: 1 March 25, 2008 at 2:27 pm

    I just came across your blog today, but this one post was enough to make me subscribe to your feed. I suck when it comes to PHP, but I’m improving…slowly. Keep up the good work!

  • John Kolbert (author) said: 2 March 25, 2008 at 7:05 pm

    @Stefanie I’m glad you found it useful! I’ve been learning PHP for some time now, and there’s always more to learn. I hope to see you around the blog!

  • john said: 3 March 27, 2008 at 9:15 pm

    i just began php.so its useful.thanks .

    john’s last blog post..Bilvanis

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Please read the comment policy before commenting.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.