Mostly Used PHP Function, Classes and Features

ADMEC Multimedia > Web Development > Mostly Used PHP Function, Classes and Features

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

There are two very useful functions that allows to parse JSON (JavaScript Object Notation) strings. JSON is a text-based standard that is mainly used to send and receive data with a simple structure that was designed to be easily read, both by a computer and by a human.

PHP provides two functions that can manipulate data both to and from JSON.

  • json_encode is used by setting the first argument as your data. It can be a simple string or a multi dimensional array, PHP will try to return a string of the data that has been converted to a JSON formatted string. It will be then used to output on an API or included in any HTML template to be read by the JavaScript code on the front end.
  • 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.

4. Exceptions

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.

5. Namespaces

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

6. glob

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.

7. Usort

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.


View Full Presentation on slideshare:Php string function

Php string function from ADMEC Multimedia Institute

Leave a Reply

Related Posts

Copy link
Powered by Social Snap