>
5 Tips That Every PHP Newbie Should Know
Welcome, Guest

by Himu on Friday, 15 October 2010
5 Tips That Every PHP Newbie Should Know


 Tip 1: MySQL Connection Class
The majority of web applications I've worked with over the past year have used some variation of this connection class:
class DB {     function DB() {         $this->host = "localhost"; // your host         $this->db = "myDatabase"; // your database         $this->user = "root"; // your username         $this->pass = "mysql"; // your password          $this->link = mysql_connect($this->host, $this->user,   $this->pass);         mysql_select_db($this->db);     } }  // calls it to action $db = new $DB; 
Simply edit the variables and include this in your files. This doesn't require any knowledge or special understanding to use. Once you've added it to your repertoire, you won't likely need to create a new connection class any time soon. Now you can get to work and quickly connect to your database without a lot of extra markup:
$result = mysql_query("SELECT * FROM table ORDER BY id ASC LIMIT 0,10"); 
More information can be found in the manual--be sure you read the comments: http://www.php.net/mysql_connect/
 
Tip 2: Dealing with Magic Quotes
PHP "automagically" can apply slashes to your $_POST data for security purposes. It's an important measure to prevent SQL injections. However, slashes in your scripts can wreak havoc. This is an easy method for dealing with them. The way to handle the slashes is to strip them from our variables. However, what if the magic quotes directive is not enabled?
function magicQuotes($post) {  	if (get_magic_quotes_gpc()) { 		if (is_array($post) { 			return array_map('stripslashes',$post); 		} else { 			return stripslashes($post); 		} 	} else { 		return; // magic quotes are not ON so we do nothing 	}  } 
The script above checks to see if magic quotes is enabled. If they are, it will determine if your $_POST data is an array (which it likely is) and then it will strip the slashes accordingly.
 
 Tip 3: Safely Query Database with mysql_real_escape_string
When you are ready to query your database you will need to escape special characters (quotes for instance) for safety's sake by adding slashes. We apply these before we insert variables into our database. Once again, we need to determine which version of PHP you are running first:
function escapeString($post) {  	if (phpversion() >= '4.3.0') { 		return array_map('mysql_real_escape_string',$post); 	} else { 		return array_map('mysql_escape_string',$post);		 	}  
}
 
 Tip 4: Debugging
If you search the forum there are many good threads with rules about debugging. The single most important thing you can do is ask PHP to report errors and notices to you by adding this line at the beginning of your scripts:
error_reporting(E_ALL); 
This will keep you in line as you learn by printing out errors to your screen. The most common error that E_ALL reports is not actually an error, but a notice for an "Undefined index". Typically, it means that you have not properly set your variable. It's easy to fix and keeps you programming correctly.
Another convenient tool while working with queries is print_r(). If your query is returning null or strange results, simply place this after your query command and it will display all the contents of the $result array.
print_r($result); exit; 
The exit command stops your script from executing any further so you can specifically review your query results. 
 
 
 Tip 5: Writing Functions (and Classes)
Initially I thought that tackling functions and classes would be difficult--thankfully I was wrong. Writing a function is something I urge all newbies to start doing immediately--it's really that simple. You are instantly involved in understanding how to produce more efficient code in smaller pieces. Where you might have a line of code that reads like this:
if ($rs['prefix'] == 1) { 	$prfx = 'Mrs. '; } elseif ($rs['prefix'] == 2) { 	$prfx = 'Ms. '; } else { 	$prfx = 'Mr. '; }  echo $prfx.$rs['name'].' '.$rs['last_name']; 
You could rewrite it like this in a function:
function makePrefix($prefix='') { 	if (!$prefix) return ''; 	if ($prefix == 1) return 'Mrs. '; 	if ($prefix == 2) return 'Ms. '; 	if ($prefix == 3) return 'Mr. '; }  echo makePrefix($rs['prefix']) . $rs['name'] . ' ' . $rs['last_name']; 
Now that you've written this function, you can use it in many different projects! 
 
 



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering