4

Click here to load reader

Advanced php tips to improve your programming – kuliko.ca

Embed Size (px)

DESCRIPTION

It’s a nasty thing. An SQL injection is a security exploit that allows a hacker to dive into your database using vulnerability in your code.

Citation preview

Page 1: Advanced php tips to improve your programming – kuliko.ca

ADVANCED PHP TIPS TO IMPROVE YOUR PROGRAMMING – KULIKO.CA

1. Use an SQL Injection Cheat Sheet –

It’s a nasty thing. An SQL injection is a security exploit that allows a hacker to dive into your database using vulnerability in your code.

While this article isn’t about MySQL, many PHP programs use MYSQL database with PHP, so knowing what to avoid is handy. If you can avoid the practices the cheat sheet identifies your code will be less prone to script…

2. Know the Difference between Comparison Operators –

Comparison operators, as their name implies, allow you to compare two values. You may also interest in viewing the type comparison tables…

These are a huge part of PHP, and some programmers may not be as well-versed in their differences as they ought…

3. Shortcut the else –

Else is used when you want to execute a statement if a certain condition is met and a different one otherwise. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to False.

4. Usual else statement:

If the $x is start is going to be 10 by default, just start with 10.While it may not seem like a huge difference in the space saved in the code, if there are a lot of else statements in your programming, it will definitely add up…

Page 2: Advanced php tips to improve your programming – kuliko.ca

5. Drop those Brackets :

Much like using shortcuts when writing else functions; you can also save some characters in the code by dropping the brackets in a single expression following a control structure...

Take a look...This is the same as...

You can even use multiple instances like …

Favour str_replace() over ereg_replace() and

preg_replae() – str_replace()

Is efficient than regular expression at replacing strings.

If you’re using regular expressions, then ereg_replace() and preg_replace() will be much faster than str_replace()…

6. Use Ternary Operators –

Instead of using and if/else statement altogether, consider using a ternary operator. PHP vale gives an excellent of what a ternary operator looks like …

7. Memcached –

While there are tons of caching options out there, Memcached keeps topping the list as the most efficient for database caching. It’s not the easiest caching system to implement, but if you’re going to build a website in PHP that uses a database Memcached can certainly speed it up. The caching structure for Memcached was first built for the PHP-based blogging website LiveJournal.

Page 3: Advanced php tips to improve your programming – kuliko.ca

PHP.net has an excellent tutorial on installing and using memcached with your PHP projects.

8. Use the Suppression Operator Correctly

The error suppression operator (or, in the PHP manual”) is the @ symbol. When placed in front of an expression in PHP, it simply tells any errors that were generated from that expression to now show up.

The Variable is quite handy if you’re not sure of a value and don’t want the script to throw out when run. Programmers use erroe suppression operator incorrectly. The @ operator is slow and costly if you write code with performance in mind.

This is an excellent example on how to sidestep the @ operator with alternative methods. Here’s an example of how you can use isset to replace the error suppression operator: But while this seconds form is good syntax it runs about two times slower. A better solution is to assign the variable by reference, which will not trigger any notice …

It’s important to note that these changes can have some accidental side effects and should be used only in performance- critical areas and places that aren’t going to be affected.

9. Use isset instead of strlen -

If you’re going to be checking the length of a string, use isset instead of strlen. By using isset, your calls will be about five times quicker. It should also be noted that by using isset, your call will still be valid if the variable doesn’t exist.

It’s a small change but, like all this tips we’ve covered today, adds up to quicker learner code.

Kuliko.ca is an award winning Web Design & Development Company specializing in a whole Web services. Kuliko.ca offers over 5 years if experience to its customer base and has offices located in Toronto in Canada.