March 19th, 2008

6 Useful PHP Snippets For Beginners

by John Kolbert as Useful Tools | Tags:

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.

3 Responses to “6 Useful PHP Snippets For Beginners”

  1. Stefanie says:

    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!

  2. John Kolbert says:

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

  3. john says:

    i just began php.so its useful.thanks .

    john’s last blog post..Bilvanis

Leave a Reply