10 most used PHP features

10 most used PHP features
by Janeth Kent Date: 22-05-2013

We have compiled a selection of top 10 PHP native functions, classes and features.

We know first-hand how invaluable these features are, so we hope you find them to be useful too.

1. PDO Class

We use the PDO (PHP Data Object) exclusively for connecting to our MySQL databases. PDO provides a nice abstraction layer around a set of database drivers such as MySQL, PostgreSQL and MS SQL. This means that whichever database you are using, as long as PDO supports it, you can use the same functions to perform the same actions on the database. This makes your code more portable for your web application to be used on a whole range of databases with no extra development time.

The example below shows a connection to a MySQL database that selects the name, colour and calories from each row in fruit table. By using a prepared statement, PDO converts the placeholders to match the array that is parsed in the execute function. By using the prepared statement, all the values are escaped to protect from SQL injection. The whole piece of code has been added to a try block, which can be used to catch any exceptions.

  1. <?php
  2. try{
  3.     /* Connect to Database */
  4.     $dsn = 'mysql:dbname=test;host=localhost';
  5.     $dbh = new PDO($dsn,'username','password');
  6.  
  7.     /* Create SQL String with parameters */
  8.     $sql = 'SELECT name, colour, calories
  9.    FROM fruit
  10.    WHERE calories < :calories AND colour = :colour';
  11.     $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  12.     /* add values and execute request on database */
  13.     $sth->execute(array(':calories' => 150, ':colour' => 'red'));
  14.     /* Get the returned Data */
  15.     $dataSet = $sth->fetchAll();
  16.     /* Print Results */
  17.     print_r($dataSet);
  18.  
  19. }catch (PDOException $e){
  20.     echo 'PDO Error: '.$e->getMessage();
  21. }catch(Exception $e){
  22.     echo 'A Unknown Error Occurred: '.$e->getMessage();
  23. }

 

2. json_encode & json_decode

PHP provides two functions that allow you to manipulate your data both to and from JSON.json_encode is utilised by setting the first argument as your data. Be that a simple string or a multi dimensional array, PHP will try to return a string of your data that has been converted to a JSON formatted string. This string can then be used to output on an API or included in your HTML template to be read by your JavaScript code on the front end.

The example below shows an array of animals that is converted to a JSON string then sent to the user. You could call this script from an Ajax request via a JavaScript framework which will then return a JavaScript object ready for you to iterate over.

  1. <?php
  2. /* Set Content Encoding Header */
  3. header('Content-type: application/json; charset=utf-8');
  4. /* Array of animals*/
  5. $myArray = array(
  6.     array('type'=>'cat','name'=>'Alfie','age'=>2),
  7.     array('type'=>'dog','name'=>'Bella','age'=>6)
  8. );
  9. /* Encode to JSON */
  10. $jsonString = json_encode($myArray);
  11. echo $jsonString;

json_decode is the opposite of json_encode. The function takes the JSON string as the first argument and tries to parse the string into a PHP variable. There is an optional second argument to convert the string to an object or an associative array. By parsing ‘true’ as the second argument (as shown below) you can return an array.

  1. <?php
  2. /* Set Content Encoding Header */
  3. $json = '[{"type":"cat","name":"Alfie","age":2},{"type":"dog","name":"Bella","age":6}]';
  4. /* Parse JSON to Object */
  5. $object = json_decode($json);
  6. /* Parse JSON to Array */
  7. $array = json_decode($json,true);
  8. /* Print Object */
  9. var_dump($object);
  10. /* Print Array */
  11. var_dump($array);

The advantage of using JSON over XML is its simplicity; with only a few data types PHP makes it very easy to parse simple data. JSON may not be as fast as the XML parsing in the majority of cases, but json_decode provides you with direct access to your data in an array or object format. This is unlike simpleXML, which returns a simpleXML object which you then have to process further to gain access to your data. Consequently, json_decode can be considered to be just as fast.

3. DateTime Class

The DateTime class allows you to manage time and dates in a more object-ordinated way, which is impossible using the standard core functions such as mktime and strtotime. By utilising the DateInterval and DateTimeZone classes, we are able to perform standard functionality like adding or subtracting time intervals on a date or changing the current time zone. The DateTime class can also be extended so we can provide extra functionality whilst making code cleaner and easier to read.

An important reason to use the DateTime class is that 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 it increase any further.

The code example below demonstrates how the DateTime Class can be used to add a day to the current server time then convert the time zone to UTC.

  1. <?php
  2. /* Get server time*/
  3. $date = new DateTime();
  4. echo $date->format('Y-m-d H:i:s');
  5. /* Add 1 Day */
  6. $date->add(new DateInterval('P1D'));
  7. echo '<br>'.$date->format('Y-m-d H:i:s');
  8. /* Change Timezone to UTC */
  9. $date->setTimezone(new DateTimeZone('etc/UTC'));
  10. echo '<br>'.$date->format('Y-m-d H:i:s');

4. Exceptions

With PHP5 came the implementation of exceptions. Like other programming languages such as Java and C#, exceptions are used to 'throw' errors that can be caught at any point by bubbling up through your code stack. An exception can be created easily using the throw keyword followed by an Exception object.

Catching exceptions is implemented by wrapping the code you want to test in try block. You will then need to create a catch block to 'catch' any exception that the code in the try block throws.

The PHP interpreter will try and parse everything in the block as usual. If no exception is thrown, then the interpreter will continue down the script ignoring anything in the catch block. However, if an exception is thrown, the rest of the code in the try block is ignored and will execute everything in the catch block.

The beauty of exceptions is that they can be extended as they are just classes. By extending the standard Exception class you can give extra functionality to your exception. Another advantage of extending Exceptions is that it allows you to have multiple catch blocks that can refer to each exception class by utilising type hinting.

With PHP5.5, we are now given a third block called finally. Any code in this area will be run regardless of whether there has been an exception thrown or not.

Like in the PDO example, the example below is contained in a try block. If the script fails to connect for any reason then we are able to capture that by introducing a catch block to capture any PDOExceptions or Exceptions. In the case below any errors will be printed to the screen and the script will still be executed to the end.

  1. <?php
  2. try{
  3.     $dsn = 'mysql:dbname=test;host=localhost';
  4.     $dbh = new PDO($dsn,'username','password');
  5.     echo 'Connected to Database';
  6. }catch (PDOException $e){
  7.     echo 'Connection Failed: '.$e->getMessage();
  8. }catch(Exception $e){
  9.     echo 'A Unknown Error Occurred: '.$e->getMessage();
  10. }
  11. echo 'I am still parsed, even if there was an error';

5. Namespaces

In the times before namespaces, PHP put every variable, function and class under the global namespace. This limited developers to ensure that their applications only had unique function and class names. Developers got round this issue by prefixing class and function names to provide a pseudo effect of namespaces, but the problem with this was that class names could end up being huge, making it sometime hard to read and write them without the use of an intelligent IDE.

Thankfully, with the introduction of namespaces, this problem was resolved. By using the namespace keyword followed by the namespace name at the top of a PHP file, it puts all the code below into that reflective namespace that cannot be accessed by any other namespaces unless explicitly instructed to. With the use of namespaces, we are now able to create well-structured and readable code that's easier to read and manage.

The example below shows how we can implement our own myNamespace namespace and create a new DateTime Class that is completely independent from the standard one provided in the global namespace. Notably, we are able to still use the default standard DateTime class in the namespace by adding a backslash to the front of the name when creating a new instance.

  1. <?php
  2. namespace myNamespace;
  3.  
  4. class DateTime{
  5.     public function customMethod(){
  6.         return 'Fun Times';
  7.     }
  8. }
  9. /*
  10.  * Create a new DateTime object that's in this current namespace
  11.  * If we are outside this namespace we can create it by:  
  12.  *        $customDate = new \myNamespace\DateTime();
  13.  */
  14. $customDate = new DateTime();
  15. echo $customDate->customMethod();
  16. /* We can still create the default DateTime object */
  17. $standardDate = new \DateTime();
  18. echo $standardDate->format('Y-m-d H:i:s');

6. Usort

usort allows the developer to sort an array based on a compare function that's configurable by the developer. This allows you to create more complex sorting algorithms that are not provided by the standard core array sort functions.

The callback/closure function has two parameters, which are two items in the array you have asked to sort on. It's then for you to decide if the first argument is greater, smaller or equal to the second argument. This is done by returning an integer value. Anything smaller than zero will assume the first argument is smaller than the second. Zero means the arguments are equal. Greater than zero means the first argument is larger than the second.

One great application for usort that we tend to use on a daily basis is to sort a simple multi-dimensional array. For example, let's say we have an array of students that consists of an associative array of name and age. By using the usort function along with acallback/closure function, we are able to pick the age and sort by ascending or descending.

For example, the code below is sorting by the age variable for each student in the students array. The callback/closure function provides the algorithm that compares the two ages and decides if the first age is greater than the second.

  1. <?php
  2.  
  3. $successful = usort($students, function  ($a, $b) {
  4.     return $a['age'] - $b['age'];
  5. });

7. Hashing API

There was an issue not too long ago with big-name sites getting hacked and insecure hashed passwords getting stolen. Back in the day, using the MD5 function was sufficient to hash a password, so it was secure. The problem with MD5 and SHA1 is that they are fast to perform the hashing algorithm. Along with the invention of faster processors and the utilisation of GPUs, people have the ability to process many hundreds of thousands of hashes per second, even on a standard desktop computer or laptop.

The new hashing API introduced in PHP5.5 provides an easy-to-use layer on top of bcrypt to create secure (for now) hashes that are a lot harder to solve by hackers. The example below shows how any string variable can be hashed. A salt will be created automatically and the hashing routing will have a cost of 15. The cost will make the hashing more secure but slower to complete.

  1. <?php
  2. $hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 20]);

8. SimpleXML Class

When we have to deal with XML files we tend to use the SimpleXML class. This easy-to-use class gives you the ability to read and edit XML files via objects. Using these objects gives you the ability to return values and iterate over arrays in the usual ways, as well as a set of simple methods to modify the XML’s structure. If you just need to grab something specific in the XML then using the built-in method of XPath, you can return a set of SimpleXML Element objects for just the path that you provided.

As well as reading XML, SimpleXML also provides the methods to create new documents and/or inject elements back into the imported XML. This can then be saved all in a few lines of code.

The SimpleXML class is provided as a standard extension for PHP. Although it’s not in the core of PHP, it is enabled by default.

The example below shows a very simple example to get a list of public photos from Flickr and display the content html of each photo.

  1. <?php
  2. /* Load flickr Public Feed */
  3. $xml = simplexml_load_file('http://api.flickr.com/services/feeds/photos_public.gne');
  4. if(is_object($xml)){
  5.     /* Get each entry and echo the content tag */
  6.     foreach($xml->entry as $photo) echo $photo->content;
  7. }

9. glob

The glob function provides an easy one-line solution for creating an array of path names using a pattern you provide. glob may not be as fast as using the opendir and readdir combination, but if you just want to iterate over a directory, or you’re searching for a specific file type such as an image, then using glob could be a good solution to your problem.

The first argument is a glob pattern string that's similar to a regular expression in the way that it functions, but the syntax to create the pattern has some differences. This allows you to search for a varied subset of files and folders that are contained in a directory.

One big disadvantage of using glob is that all the results are stored into memory in one go. If you have a very large folder full of files you can soon run out of memory, and in this instance, it would be better to use opendir and readdir as it creates a read stream buffer.

Using the example below we are able to return all the image files in the $imagePath directory. An array will be returned to the $images variable.

  1. <?php
  2. /* Find all image files in image folder */
  3. $images = glob($imagePath.'*.{gif,jpeg,jpg,png}',GLOB_BRACE);

10. array_map

The standard core functions that PHP provide tend to perform faster, than creating your own similar functions using PHP code. A great example of this is utilising array_map instead of creating a for or which loop. array_map allows you to create a function called a callback that will be applied to every item in an array you supply.

For example, if you had an array of lowercase letters, a callback function can be created to uppercase all the characters in the array. On a small-sized array, the speed increase would be quite small, but once you start using larger arrays, you can really see a significant difference in speed.

In the example below we are converting the alphabet from lowercase to uppercase. We have provided the array_map function with a string of the name of the function we want to process. In this case we are using strtoupper to convert each letter in the array.

If there’s a need for more complex functionality, we can also provide the array_map function, a closure like in the usort example above.

  1. <?php
  2. $alphabet=array();
  3. /* Create lower Case Alphabet */
  4. for($i=97;$i<123;$i++) $alphabet[]=chr($i);
  5. /* Convert to Uppercase */
  6. $upperCaseAlphabet = array_map('strtoupper',$alphabet);

 

 

 

source: netmagazine.com

 

 
by Janeth Kent Date: 22-05-2013 hits : 2867  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 
Clicky