PHP-strpos function

The strpos function in PHP is used to determine whether a string is contained in another string.

For Example

If there is a string A that contains the following words “The cat is sleeping” and you have another string B that contains this “cat”

One could use the strpos function to determine whether string B belongs to any element is string A. In this case the word cat from string B is present in string A

 

The If Statement

 if( (strpos($A,$B)!==false){

echo “True”;

}

else{

echo “False”;

}

Will return True since string B belongs to string A

The common use of this function is to determine the position of the first occurrence of the substring in a string

strpos(string,substring)

 

Advertisements

Error Reporting in PHP

Error Reporting is very Important in order to know exactly which part of the code is not working and to get the cause or the reason for it to not work. We as developers might always think that our code would not run Into any errors after we make the design. But errors are something that happens and it’s something that one does not foresee while coding. It is akin to driving on the road and getting Involved in an accident or if your car breaks down. These events are not planned.

So Suppose we get an error, we need to know what exactly is causing the Issue in order to fix it. Each language has it’s own way for reporting errors. In PHP, in order to enable error reporting, one needs to create a special file in the Project Folder. The file is called php.ini

In this file all you have to do is Include this one line of code-

display_errors = on ;

This turns on error reporting on the project folder.

So, next time if you are working on a project make sure that error reporting is enabled. This is not done by default.

PHP-Array_unique method

Array_unique is a method in PHP which is used to display unique elements in an Array. It removes duplicate elements. The method takes the array as an Input and returns an array that contains only unique values.

Array_unique($array)

 Keep in mind that the array will not re- position the elements after it removes duplicates. 

For example-

$arr=$array (1,2,2,3);

In this array, $arr [0]=1 ,  $arr[1]=2,  $arr[2]=2, $arr[3]=3

Then, if we apply the method to this array.

$arr=Array_unique ($arr);

We get an array which has elements-

 $arr[0]=1,  $arr[1]=2 and $arr [3]=3;
There will be a null element at index 2, since that element was removed. So the element which was at index 3, will still remain at 3 and it would not re position itself to index 2.

PHP strstr function 

This function in PHP takes a string  and a token as an Input and it outputs the part of the string before the occurrence of the token.

For example

Let’s say there is a string 

$word=”Handle”

and we have a token ‘l’

Then strstr ($word, ‘l’,true); would return “Hand” as the output.

Reasons for Upgrading PHP Version

There have been times where you might have observed that your PHP code does not run simply because the plugin or application requires a later version of PHP. The PHPMailer class hosted in Github supports only PHP versions from 5.3 onwards. Now why are applications forcing us to upgrade?

There are many reasons why one should upgrade to a later version of PHP-

Reasons

Security: Older Versions are not supported with Security updates. This makes your web application in secure. For folks using PHP 5.4, the support for that version officially ended on Sept 2015.

For every stable version released by Zend Technologies (The company which develops ad supports PHP), it has an active support for two years followed by one year of support based on an as-needed basis. So, it is always advisable to upgrade to a stable release of PHP. The current stable versions are PHP 7.0.5 and PHP 5.6.20

PHP Accelerator: There is no need to run a PHP Acclerator like APC to improve performance. From PHP 5.5 onwards, opcode cache is integrated with PHP which helps in improving the performance of the site.

New Features: PHP 5.5 added a finally block to the already existing try-catch block. The finally block always gets executed irrespective of whether there is an exception in the program or not.

Built in Webserver: They have come into existence since PHP 5.4 . It serves as a reliable way of testing your PHP Application before deploying it live on the web.  Previously one would have to set up a virtual host on the local web server and test it. Installing and configuring the virtual host was something one would not like to do as a source of enjoyment!

Password Hashing:  Improved encryption mechanisms for protecting passwords.

 

Even after such compelling reasons, there are many people who still don’t upgrade. The reason being that doing an upgrade would cause a break in the application code since it relies on older features. This is in fact quite true. Unlike major programming languages like Java, PHP is not backward compatible and hence newer versions of PHP do not support older code.

Performing an Upgrade

Now, there are many ways  one could perform an upgrade. People using web hosting sites like Hostgator would find this helpful

http://support.hostgator.com/articles/cpanel/php-configuration-plugin

And for the ones coding in Linux/ Ubuntu can look at this

http://askubuntu.com/questions/565784/how-do-i-upgrade-php-version-to-the-latest-stable-released-version

 

Here are some other links for those Interested in further reading-

  1. http://php.net/supported-versions.php
  2. http://php.net/eol.php
  3. http://www.sitepoint.com/multiple-websites-apache-virtual-hosts/
  4. http://stackoverflow.com/questions/4693575/is-php-5-3-backwards-compatible-with-php-5-2
  5. Evolution of PHP
  6. https://github.com/PHPMailer/PHPMailer
  7. Zend Website