Hello everybody,

There are some techniques which I use that come in REALLY handy when 
writing PHP (or even ASP). I will just go through them here briefly. Any 
questions? Email me.

1. Keep a php file (lib.php) inside a 'code' folder in a directory for 
every site you work on (depending on security needs, you might want to 
keep it out oft he web directory). In this file, keep your functions 
that you are likely to use again and again. In mine, I keep my database 
connection and query functions, and all the other string/array functions 
I create. Your pages become cleaner and to start a new page, all you 
have to type is the following:

<?php include 'code/lib.php';

?>

2. Try to use functions (and classes) to your advantage. Write them. I 
have written a few to make my life easier.

// Open a database connection
dbConnect();

// select and put into 2d associative array
$sql = "SELECT * FROM tblTable";
$data = getData($sql, ASSOC);

If you want to use these functions, send me an email and I will send 
them to you. They are very flexible (although they are only mySQL 
compatible). Here are some functions that I wrote when I first started 
using PHP:

// if you want to quickly test a variable (such as an sql statement) 
chuck this in... saves time.

test($sql);

function test($str){
        echo $str;
        exit();
}

// Useful for: printing arrays and seeing their structure preformatted 
from php
// Example:
/*

plainText();

print_r($array);

*/

function plainText(){
        header ("Content-Type: text/plain");
}


// Useful for: sending someone back to the last page they were on
// Usage: goBack();

function goBack(){
        header ("Location: $HTTP_REFERER");
        exit;
}



// Useful for: quick redirection to another page
// Usage: go("url.php");

function go($URL){
        header ("Location: $URL");
        exit();
}


So you see, these functions aren't big, and they don't have to be, but 
it cleans things up a bit and makes your code more readable... And as a 
huge bonus, a lot of your syntax errors/spelling errors are fixed 
because you don't have to type as much.

Adam


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to