PHP 5 Windows Hosting: suggestions and adjustments for the compatibility towards PHP 4

The new version of PHP has been considerably optimized thanks to the Zend Engine 2, as well as numerous new implemented functions.

The developers of the platform took into consideration the need for compatibility in the applications and the scripts created for the previous version, but some differences in the engine require certain modifications to the old code and certain adjustments in the new one. Directly from the support pages of php.net, we indicate the main points: all of the links open in new window.

Changes that are not compatible (complete source, php.net)

  • Illegal use of string offsets causes E_ERROR instead of E_WARNING. An example of illegal use is: $str = 'abc'; unset($str[0]);
  • array_merge() was changed to accept only arrays. If a non-array variable is passed, a E_WARNING will be thrown for every such parameter. Be careful because your code may start emitting E_WARNING out of the blue.   
  • The PATH_TRANSLATED server variable was changed: see also the description of $_SERVER['PATH_TRANSLATED'] in the manual.

  • An object with no properties is no longer considered "empty".

  • In some cases classes must be declared before use. It only happens if some of the new features of PHP 5 (such as interfaces) are used. Otherwise the behaviour is the old one.

  • get_class()get_parent_class() and get_class_methods() now return the name of the classes/methods as they were declared (case-sensitive) which may lead to problems in older scripts that rely on the previous behaviour (the class/method name was always returned in lowercase). A possible solution is to search for those functions in all your scripts and use strtolower(). This case sensitivity change also applies to the magical predefined constants __CLASS__, __METHOD__, and __FUNCTION__. The values are returned exactly as they're declared (case-sensitive).
  • ip2long() now returns FALSE when an invalid IP address is passed as argument to the function, and no longer -1 
  • If there are functions defined in the included file, they can be used in the main file regardless of if they are before return() or after. If the file is included twice, PHP 5 issues fatal error because the functions were already declared. We recommend that you use include_once() instead of checking if the file was already included and conditionally return inside the included file.

     

     

Furthermore:

-It is not possible to re-assign $this : the following example works on PHP4 (it gives "OK"), but under PHP5 produces a "fatal error": 

-No comments allowed after shorthand echo block

-Constructors return a reference as default. The following example works under PHP4 but under PHP5 produces an E_NOTICE notice. In this case the problem can be solved by removing &


PHP5 offers also new functions, for example:

  • array_combine() - Creates an array by using one array for keys and another for its values.

     

     

  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function  

     

     

  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison  

     

     

  • array_udiff_assoc() - Computes the difference of arrays with additional index check. The data is compared by using a callback function  

     

     

  • array_udiff_uassoc() -Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also 

     

     

  • array_walk_recursive() - Apply a user function recursively to every member of an array  

     

     

  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check. The data is compared by using a callback function  

     

     

  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check. Both the data and the indexes are compared by using separate callback functions  

     

     

  • array_uintersect() - Computes the intersection of arrays. The data is compared by using a callback function  

If you use third party applications, cms, portals, forums, guestbook, etc, make sure that the application is compatible with PHP5 in the parent site of the application or script in question.