PHP is compiled down to an intermediate bytecode that is then interpreted by the runtime engine. The PHP compiler’s job is to parse your PHP code and convert it into a form suitable for the runtime engine. Among its tasks:

  • Ignore comments
  • Resolve variables, function names, and so forth and create the symbol table
  • Construct the abstract syntax tree of your program
  • Write the bytecode

Depending on your PHP setup, this step is typically done just once, the first time the script is called. The compiler output is cached to speed up access on subsequent uses. If the script is modified, however, the compilation step is done again.

Here’s a list of good to know stuff related to PHP.

  1. How is array_search implemented internally ?
    it’s a linear search. Link
  2. List of Complexity of all PHP functions. Link
    Arrays in PHP are hashtables. Searching for a key in a hashtable is O(1). Searching for a value is O(n) — which you can’t beat on an unsorted set.
    So instead of using array_search do an array_flip and do an array_key_exists.
    This will be useful in case when we have to search a lot of times, as array_flip is-itself O(n).
  3. ini_get() -> Returns the value of the configuration option as a string on success, or an empty string for null values. Returns FALSE if the configuration option doesn’t exist.
  4. final keyword –
    PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. The methods or classes can not be modified by a child class. This prevents class inheritance, method-overriding and/or redefinition of methods.
    Note: Properties cannot be declared final, only classes and methods may be declared as final.
  5. static –
    Declares class methods or properties as a static value so that you have access to them without instantiating an object. These are shared between parent and child-classes. A class definition can not be static unlike final.
  6. const
    These create a constant value for a class. The constant values will get set and can NOT be changed by a method in either parent or child-class. Class constants are allocated per instance of the class. const is a type specifier in itself.In the context of a class, static variables are on the class scope (not the object) scope, but unlike a const, their values can be changed.Consts cannot be defined from an expression. const FOO = 4 * 3; doesn’t work, but define(‘CONST’, 4 * 3); does.

    const AWESOME = whatIsMyName(); // Invalid (Function call)
    const WEAKNESS = 4+5+6; // Invalid (Arithmetic)
    const FOO = BAR . OF . SOAP; // Invalid (Concatenation)

    class ClassName {
        static $my_var = 10;  /* defaults to public unless otherwise specified */
        const MY_CONST = 5;
    echo ClassName::$my_var;   // returns 10
    echo ClassName::MY_CONST;  // returns 5
    ClassName::$my_var = 20;   // now equals 20
    ClassName::MY_CONST = 20;  // error! won't work.

    Public, protected, and private are irrelevant in terms of consts (which are always public); they are only useful for class variables, including static variable.

    • public static variables can be accessed anywhere via ClassName::$variable.
    • protected static variables can be accessed by the defining class or extending classes via ClassName::$variable.
    • private static variables can be accessed only by the defining class via ClassName::$variable.
  7. define
    As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function. Difference.
  8. Constructor

    PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

    NoteParent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).


  9. array_key_exists is much faster. array_search must traverse the whole array, so it is O(n). array_key_exists is a hash table lookup, so it is O(1).
    isset() is faster than array_key_exists().  Graph Linkisset is faster because array_key_exists attempts to compute a hash even if the key does not exists. Link
  10. Why would one omit the close tag?
    If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
    Many older text editors always inject a trailing newline. And trailing whitespace led to “headers already sent” errors. The PHP interpreter actually circumvents this problem, and eats up a SINGLE trailing \r and \n newline after the ?> closing tag. Some unpracticed programmers however errornously added two or more trailing newlines or spaces, tabs after ?>. That’s why it’s considered good newbie guidance to omit the PHP close marker.
    Many PHP frameworks including SymfonyZend and Laravel (there is no mention of this in the coding guidelines but it follows the suit) and the PSR-2 standard (item 2.2) require omission of the closing tag. PHP manual itself (1,2), WordPressDrupal and many other PHP software I guess, advise to do so. If you simply make a habit of following the standard (and setup PHP-CS-Fixer for your code) you can forget the issue. Otherwise you will always need to keep the issue in your mind. SO SO
  11. Require is like include, but if the file included is not available a fatal error occurs and processing stops.
  12. isset() — Determine if a variable is set and is not NULL
    In other words, it returns true only when the variable is not null.empty() –  Determine whether a variable is empty
    In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

    From PHP Manual – is_null():
    is_null — Finds whether a variable is NULL
    In other words, it returns true only when the variable is null.
    is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.
    Screen Shot 2017-10-06 at 1.26.18 AM


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s