The time goes by and PHP is evolving steadily providing developers with more intricate and efficient tools. In this article, we attempted to gather the most profitable php tricks and hacks both for newbies and more experienced website makers. Let’s go!
PHP Advanced Programming for Everybody
- Work With Sessions
The newest version of php enables session_start() to accept an array of options to define php.ini configurations. One of the advanced php techniques is to use session.lazy_write option for rewriting session data automatically and read_and_close function for preventing changing them.
- Enable Error Reporting
One of the php tricks for beginners, which are known but underestimated by professionals. These two lines should be included into all advanced php programming tutorials:
error_reporting ( E_ALL ) ;
ini_set ( ‘display_errors’ , 1 );
Save your time and effort just by adding them in the beginning of your code.
- Handle Ternary Operator
The ternary operator, as it’s claimed in advanced php books and advanced php w3schools, in some cases is exactly what you need to write an efficient and lightweight code. However, the advisability of its use depends on the variable’s data size. If the operand is a sizable structure it will be copied which may lead to memory leaks. In some cases it is better to use the “if” operator.
- Improved Generators
In PHP7 there are several new advanced php topics and tricks concerning generators, namely the return statement and generator delegation. These features allow returning the very last generator’s value or delegate the values of one generator to the other using yield from.
- Anonymous Classes
Advanced php concepts claim that an anonymous class as a can be generated from new class if you need a one-time class and its object does not make sense. Such php programming tricks are described in advanced php topics tutorials and help to use resources advisably.
- Forget MySQL Driver
It’s time to follow php advanced concepts and switch to PDO. This extension helps to connect various database managers to your website. You can simply find an advanced php tutorial with examples on the Internet (try to enter advanced php tutorial pdf or php tips and tricks pdf) and start working with state-of-the-art tools.
- Utilize .htaccess
Clean URLs are great for SEO, so do not hesitate to improve them using .htaccess file:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
The method is adopted form advanced php books. This would improve redirection performance either.
- Improve isset()
In PHP7 a ?? operator was added. It returns the first operand if it is set and is not NULL, and in the opposite case returns the second operand. It’s very convenient in a combination of a ternary operator and the isset () function, which is stated in many php advanced concepts with examples.
- Avoid Using require_once/include_once statements
All php tricks ratings are shouting at coders not to use *_once language constructs in the code, and so do we. By the idea, these statements should help to save the server resources and this is a exactly a case in simpler apps, but at high-load and mission critical servers these constructs’ often lead to the reduction in efficiency. Most of advanced PHP tutorials recommend using the traditional require()/include() functions using the relative paths instead.
- Keep Your SQL Safe
The developers of all levels often ask how to become a good php developer. Our answer is: learn to write secure code. That’s why we recommend always adhere to simple SQL injection prevention rules. Escape any variable that you’ll use in the database:
$query_result = mysql_query (“SELECT * FROM WHERE Ex_table ex_field = \” ” . mysql_real_escape_string( $ ex_field ) . ” \ ” ” ) ;
Usually, php coding tips contain universal pieces of advice or ignore new implementations. Our php tips and tricks advanced further and tried to find the freshest solutions to improve php coding. Hope it will be useful for you!