5 php tips and tricks to enhance your development prowess

Page 1

5 PHP Tips and Tricks to Enhance Your Development Prowess The world of PHP development is only slated to grow in the future. With tons of already existing frameworks and new one getting added as the time progresses, PHP development services are only slated to grow with time. This makes it important, more than ever before, for the developers to brush up on their development skills and outperform themselves with every new project. If you have been meaning to improve upon your PHP skills, please follow the 5 tips and tricks that we have shared in here; we are hopeful that these tips will help you add a further dimension to your development skills. 1.

An advanced and unique way of debugging

A lot has been said and done about the debugging, and if you require high end resources about the same, you can easily find them on various posts and forums. However, without doubting the integrity of the same, we are here to provide you with a unique and not that widely used way of debugging the applications that you develop. By following this method, you will have PHP to go through your project and report any anomalies or bugs the moment it is able to identify the same. Excited already? You may achieve the same simply by writing the following code in the very beginning of the code: error_reporting(E_ALL); This is an amazing way to improve upon your coding practices and learn as you go about it. The code automatically shows an error, the moment it spots an Unidentified Index. What it usually means is that the variable has not been clearly defined or properly set initially. This way, the programs get easy to fix and any anomalies are identified in the very beginning, so that you don’t have to go about doing the same later on. And while we are talking about debugging, we would like to share another tool which gets quite handy while working with the queries. If you have often see the queries return obscure or null results, simply put the below mentioned code at the back of your query and in return, the query will showcase the contents from the $result array. print_r($result); exit; The exit command forbids the script from running the query any further, and hence you get into a better position to review your queries and administer the same. 2.

Come up with better functions; discover how:

A class is nothing but just a collection of similar or related functions. To come up with well defined classes, one needs to brush up the OOP principles and practices of PHP development services and also should be a pro at writing functions. If you have always felt that dealing with functions and classes is a difficult task, we come bearing good news for you. If you, as the PHP developer, are trying to avoid writing functions for whatever reasons, we would like you to urge you to start doing it quickly! We are here to tell you a way of writing functions which is completely and utterly and thoroughly easy. Let us understand the same with the example given below:

New Way of Writing Functions that we endorse

Old Way of Writing the Function Code

if ($rs['prefix'] == 1) { $prfx = 'Mrs. ';

function makePrefix($prefix='') {


} elseif ($rs['prefix'] == 2) {

if (!$prefix) return '';

$prfx = 'Ms. ';

if ($prefix == 1) return 'Mrs. ';

} else {

if ($prefix == 2) return 'Ms. '; $prfx = 'Mr. ';

if ($prefix == 3) return 'Mr. ';

}

}

echo $prfx.$rs['name'].' '.$rs['last_name'];

echo makePrefix($rs['prefix']) . $rs['name'] . ' ' . $rs['last_name'];

Just try writing functions the way we have mentioned, and once through, you’ll be able to use it anywhere as you desire.

3.

Getting the better out of MySQL Connection Class

Regardless of which application has been developed, what its functionality and scope is, the developers may find them making use of the Connection Class invariably always. And hence we decided to provide them with an easy way to do the same here. To know how to improve the program while dealing with MySQL connection class, kindly follow reading:

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; The variables can be easily edited and included into the files. Note that we are not really asking you to get acquainted with any new form of coding or development, merely refine and streamline the knowledge that we already have. With this code in place, you would not really have to create a connection class anytime soon. Without having to use a lot of markup for the same, you can easily get to your work station and easily get connected with your database structures. $result = mysql_query("SELECT * FROM table ORDER BY id ASC LIMIT 0,10");


4.

Managing the Magic Quotes

It is almost magical how PHP applies slashes automatically to the codes and especially to the $_Post Data, in order to make the platform safe and secure, at the same time preventing and blocking injections from the SQL data. This is all the warm and good talks, but what we don’t know is the same magically appearing slashes can wreak havoc on the applications, if you fail to manage and coordinate the same. The easiest way to manage the same is by stripping them off from various variables. If you are wondering how to do the same, please continue reading. 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 above mentioned script first confirms whether Magic Quotes are enabled or not. If they are available, the script would next check the type of $_Post data; if it is an array, only then shall the script strip off the slashes, or else acts accordingly. Here, please note that this process is not a true form of validation. Should you need the same then validate all of the data submitted by the users in the regular and conventional way. 5.

Safety provisions while querying Databases

While querying the database connection, it is important to get away from certain special characters, including quotes etc, for the sake of the very safety. The same can be achieved easily by including slashes. Apply the slashes before you insert variables to your databases. However, before doing that, check that the particular version of PHP that you are using allows you to do the same. function escapeString($post) { if (phpversion() >= '4.3.0') { return array_map('mysql_real_escape_string',$post); } else { return array_map('mysql_escape_string',$post); } } This marks an end to our journey to becoming better PHP developers presently. We shall be back again with further insides into the world of this dynamic web development technology. If you require high end applications, consider opting for the services offered by offshore PHP developers from a certified and state of the art development center. About Author:


Celin Smith is a PHP Developer who works at one of the most reputed PHP Development Services provider company called Xicom Technologies Ltd.

You

may

Hire

PHP

Developer

for

your

high

end

customized

and

bespoke

projects.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.