PHP is a recursive acronym for “PHP: Hypertext Preprocessor”. It is a server side scripting language which is embedded in HTML. With the help of this many e-commerce sites are built and it manages dynamic content, databases, session tracking etc. It can be integrated with number of popular databases, comprising MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
Some of the important functions, classes and features are:
Every programmer should have a basic understanding of PHP functions, classes and methods. We have gathered a list of important functions, classes and features below that every coder should know.
1. PDO Class
PDO (PHP Data Object) are used for connecting to MySQL databases. It provides an abstraction layer for a set of database drivers such as MySQL, PostgreSQL and MS SQL. This means that whatsoever database you are using, if PDO supports it, one can use the same functions to perform the same actions on the database. This makes the code more portable web application to be used in variety of databases with no additional development time.
- It provides functions such as creating transactions, creating prepared statements and allowing the escape of the variables if required.
- It can also help in protecting web applications from SQL injection attacks.
2. json_encode & json_decode
PHP provides two functions that can manipulate data both to and from JSON.
- json_decode is the opposite of json_encode. The function will take the JSON string as the first argument and tries to parse the string into a PHP variable. Second argument is optional which can convert the string to an object or an associative array. By parsing ‘true’ as the second argument array will be returned.
3. DateTime Class
It allows to manage time and dates in a more object-ordinated way.
- By using DateInterval and DateTimeZone classes, adding or subtracting time intervals on a date or changing the current time zone can be done.
- It can be used to provide additional functionality which will make the code cleaner and easier to read.
- It stores all the dates as a 64-bit integer, which provides a workaround for the 32-bit 2038 bug where the dates will cease to increase it any further.
PHP5 supports the implementation of exceptions. Exceptions are used to ‘throw’ errors that can be caught at any point by bubbling up through your code stack.
- Exception can be created easily using the throw keyword followed by an Exception object.
- Catching exceptions is done by wrapping the code in try block. Catch block needs to be created to ‘catch’ any exception that the code in the try block throws. PHP interpreter will try and parse everything in the block as usual. If no exception is thrown, interpreter will continue down the script ignoring anything in the catch block. However, if an exception is thrown, the code in block is ignored and will execute the catch block.
- Exceptions can be extended as they are just classes. By extending to the standard Exception class one can give extra functionality to the exception.
- It allows multiple catch blocks that can refer to each exception class by utilising type hinting.
With the use of namespace keyword followed by the namespace name at the top of a PHP file, it puts all the code below the reflective namespace that cannot be accessed by any other namespaces unless explicitly instructed to.
- With the use of namespaces, well-structured and readable code can be created that’s easier to read and manage
This function provides an easy one-line solution for creating an array of path names using a pattern you provide.
- searching for a particular file type such as an image, then using glob could be a good solution to your problem.
It allows the developer to sort an array based on a compare function that’s configurable by the developer.
- It creates more complex sorting algorithms that are not possible with standard core array sort functions.
One great example for usort is to sort a simple multi-dimensional array. For example, there is an array of students that consists of an associative array of name and age. With the use of usort function along with a callback/closure function, we can pick the age and sort by ascending or descending order.
8. Hashing API
The hashing API provides an easy-to-use layer on top of bcrypt to create secure hashes that are a lot harder to solve by hackers.
- It provides a native password hashing API that safely handles both hashing and verifying passwords in a secure manner.
There are enormous options are available in PHP, but every programmer needs to grasp the base features mentioned in this article in order to become a PHP expert. By knowing these features you will be in position to pass any hurdles while coding in PHP. Without knowledge of these, it would be difficult to understand different frameworks.