Thursday, 28 December 2017

Difference between PHP5 and PHP7

1. 1st difference is name i.e PHP7. The stable release PHP5x version is PHP5.6 After some dispute the development team decided they would omit the PHP6 name for next major release.PHP 6 already existed in the past as an experimental project but never reached the production phase.

2. PHP7 receives a brand new version of the Zend engine coming under the code name PHP#NG(Next Generation). PHP5 uses Zend Engine II.

3. The biggest change in PHP7 is speed.By using PHP 7 not only your code will be executed faster but you will also need fewer servers to serve the same amount of users.

4. Error handling. To say the least, handling fatal and catchable fatal errors have never been an easy task for PHP coders. The new Engine Exceptions will allow you to replace this kind of errors with exceptions. If the exception is not caught, PHP will continue to return the same fatal errors as it does in the current 5.X series.

5. 64-bit windows system support. PHP 7 will change this as it introduces consistent 64-bit support which means both native 64-bit integers and large files will be supported.5.X series don’t yet provide 64-bit integer or large file support, so until now x64 builds have been considered experimental.

6. New Spaceship(<=>) and Null Coalescing Operators(??)
    The Spaceship operator runs under the official name of Combined Comparison Operator.
     The spaceship operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It’s also called a three-way comparison operator.
    The Null Coalescing operator uses when you want to check if something exists and return a default value, in case it doesn’t. The coalesce operator returns the result of its first operand if it exists and is not null, and the second operand in any other cases.
 ex. PHP5 :- $userid=isset($_GET['id'])?$_GET['id']:'null';
     PHP7:- $userid=$_GET['id'] ?? 'null';

7. Enables Accurate Type Declarations
   The new PHP 7 enables developers to enhance the quality of their code with the help of return type declarations.PHP 7 introduces 4 new type declarations for scalar types: int, float, string and bool

8.  Adds Anonymous Classes
     The syntax is the same as what we are used to in traditional PHP classes, only the name is missing. If anonymous classes are used well, they can speed up coding as well execution time.

Tuesday, 26 December 2017

What is .htaccess

  In simple word we can say .htaccess is a configuration file for application which is running on Apache Web Server. .htaccess file is detected and executed by the Apache Web Server. This can be used to enable/disable additional functionality and feature offered by Apache web Server.
Some uses explain below shortly
Redirects
:-
     redirects is use to when you moved your web site content to new location it is help's user to redirect old link to new link
 
Password protection :-
    This is used for protected your directory from unauthorized users. you can give user name and password for loading site or directory.
 
Deny Visitors by IP address :-
    This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site.
 
Preventing access to your PHP includes files :-
    If you have a directory containing PHP includes, that you do not wish to be accessed directly from the browser, there is a way of disabling the directory using Mod_Rewrite.

URL rewriting :-
      This is useful for removing index.php, extension from url.

Saturday, 14 January 2017

Tips to optimize php script for better performance

1. Use PHP native functions :
         As much as possible try to use native PHP functions rather than writing your own functions to achieve the objective.
2. Use single quotes:
        Using single quotes is faster than using double quotes. If you are going to keep only the string inside it avoiding any variables.
3. Use === :
        Use '===' inserted of '==' as the former strictly checks for a closed range which makes it faster.
4. Use appropriate str function :
        str_replace is faster than preg_replace but strstr is faster than str_replace.
5. Calculate only once :
        calculate and assign the value to the variable it that value is getting used numerous time rather than calculating it again and again where it is being used.
eg. for($i=0;$i<count($array); $i++)
better
$len=count($array);
for($i=0;i<$len;$i++)
6. Pass reference to function :
        pass reference to the function if it is not affected your logic.A function manipulation the reference is faster than those manipulating the value been passed as here one more copy of the value is getting created.
7. Created classes when it's required :
        Don't create classes and methods until and unless its really needed. used and reused as well.
8. Disable debugging messages :
        File operations are expensive so if you have written lot of custom functions to log errors and warning during your development process make sure you remove them before you push the code to production.
9. Use caching techniques :
        use cache to reduce the load of database operations as well as the script compilation. mem cache for reducing database load and APC for opcode caching and intermediate code optimization.
10. Close the connection :
           Get into the habit to unset the variables and close database connection in your PHP code it save memory.
11. Reduce number of hits to database:
           Try to reduce the number of hits to the database. make queries aggregate so that you call the database less number of times.
12. Frequently used switch cases :
           Keep most frequently used switch case on the top.
13. Use method in derived classes:
            Method in derive class is faster than base class.
14. Use JSON :
          use JSON insated of XML while working with web services as therer are native PHP function like json_encode() and json_decode() which are very fast.
15. Use isset :
          Use isset where every possible insted of using count(), strlen(),sizeof() to check out greater then 0.