Mostly Used PHP Function, Classes and Features

ADMEC Multimedia Institute > 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 given below.

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.

Let’s explore about PHP classes:

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. 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.

Let’s explore about PHP functions:

1. json_encode & json_decode

There are two very useful functions that allow parsing 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.

2. 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.

3. 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.

4. 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.

Let’s explore about PHP features

1. 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.

2. 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

3. Better Speed

As compared to order versions PHP 7 offers the best speed and saves time of the programmer

4. Better Support

Now, its architecture has 64  bit supporting system. This has improved its architecture.

5. New Operators

There are better and new operators that have been introduced in this such as :

  1. Spaceship Operator
  2. Null Coalesce Operator

6. Better Memory

This update has come up with better consumption of memory. This is done without compromising with the standard of work that is provided.

7. Readable Code

It is better with Scalar Declarations. Also, this has help in providing the better code that is easy to read as well as it is versatile.

8. No unnecessary extensions

In this the best thing that has happened is removal of unnecessary extensions.

9. Secure Random Numbers

Now, in this version random numbers production has become better and secure. It helps in producing Random int as well as Random Byte.

10. Extraordinary features

It is open source, platform independent as well as secure language. Moreover it is capable to integrate with the database without any hurdle.

For more details about features of PHP7 go through our blog on New Features of PHP 7 .

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.

Want to learn more about PHP? Join our expert courses: PHP Master and PHP MySQL Premium


View Full Presentation on slideshare:Php string function

Php string function from ADMEC Multimedia Institute

Related Posts

Leave a Reply

Call Now