Header Ads

  • Breaking Now

    Short questions and answers on PHP

    PHP acronym stands for Hypertext Preprocessor which is an open source scripting language which is suited for web development and embedded in HTML. Its syntax resembles with Perl and C. The recommended versions of PHP are 7.1 and 7.2. 
    A very basic PHP sample is given as below:
    Unlike any client side scripting language, PHP is executed at server side, generating HTML sent to client side. It offers many features and very simple to learn. 

    Here goes a list of Q&A on PHP:

    Q1: What can PHP do?
    A: PHP can be used for:
    a. Server-side scripting for which you may need the PHP parser (CGI or server module), a web server and a web browser.
    b. Command line scripting to run it without any server or browser.
    c. Writing desktop applications wherein some advanced PHP features in client-side applications can be used using PHP-GTK
    d. PHP is used on all major operating systems, e.g. Linux, many Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, macOS, RISC OS, and probably others
    e. One can output images, PDF files and even Flash movies (using libswf and Ming) generated on the fly, any text, such as XHTML and any other XML file.
    f.  It supports a wide range of databases. Using one of the database specific extensions (e.g., for mysql), or using an abstraction layer like PDO, or connect to any database supporting ODBC extension.
    g.  PHP supports talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and countless others. You can also open raw network sockets and interact using any other protocol.
    h. PHP has useful text processing features like Perl compatible regular expressions (PCRE), and many extensions and tools to parse and access XML documents.

    Q2: What’s the difference between the include() and require() functions?
    A: The difference between include and require is when a file being included cannot be found: include will emit a warning ( E_WARNING ) and the script will continue, whereas require will emit a fatal error ( E_COMPILE_ERROR ) and halt the script.

    Q3: How can we get the IP address of the client?
    A: In general "$_SERVER['REMOTE_ADDR'];" is used in PHP to determine the IP address of the client.

    Q4: What’s the difference between unset() and unlink()?
    A: In PHP, unlink() is a function used for file system handling, primarily used to delete files.
    unset() is a function for variable management in PHP. It will make a variable undefined/destroyed. unset() is used to null out the value of a given variable. It can be used to remove a single variable, multiple variables, or an element from an array.

    Q5: What are the main error types in PHP and how do they differ?
    A: Basically four types of errors in PHP, which are as follows:

    a. Parse Error (Syntax Error): Unclosed quotes, Missing or Extra parentheses, Unclosed braces, Missing semicolon

    b. Fatal Error: When asking PHP to do something which it is not designed to do e.g. try accessing undefined functions

    c, Warning Error: It won't stop execution of a PHP script e.g. warning errors are to include a missing file or using the incorrect number of parameters in a function

    d. Notice Error: Similar to warning errors, such as to include a missing file or using the incorrect number of parameters in a function
    Q6: How can you enable error reporting in PHP?
    A. There are two ways for viewing PHP errors.
    1. display errors directly on your website (viewable from any web browser)
    2. enable error logging to write the errors to a specified file (viewable inside a text file)

    Q7: What’s the difference between using mysql_ functions and PDO?
    A: mysql is deprecated function in PHP which provides an access to MySQL through a procedural way while PDO(PHP Data Object) is a general database abstraction layer with support for MySQL. PDO provides prepared statements and lot of flexibility while returning data.  

    Q8: Describe how inheritance works with PHP.
    A: PHP uses inheritance principle in its object model.When one class extends another, the subclass inherits all of the public and protected methods from the parent class. They will retain their original functionality unless a class overrides those methods in parent class. PHP supports single inheritance and multi-level inheritance only.

    Q9: Do you know what the PHP-FIG is? Describe it.
    A: FIG stands for Framework Interoperability Group. The purpose of same is to increase collaboration among different stakeholders of projects with commonalities. 

    Q10: What are different PHP frameworks and their preferences of use?
    A: The objective of PHP Frameworks is to provide a tool for building PHP based applications with agility. It helps in facilitating scalability and better control on maintenance. Some examples of PHP frameworks to help you build interoperable, agile applications are:
    Symfony, CakePHP, Phalcon, Aura, Laravel, Fat-Free, Kohana

    Q11: What are Traits?
    A: Traits is a mechanism for code reuse in single inheritance languages like PHP. It reduces limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

    Q12: Can the value of a constant change during the script’s execution?
    A: No, it cannot change during the execution of script.

    Q13: Can you extend a Final defined class?
    A: No. If a class is defined as Final, it cannot be extended. If a method is defined as final then it cannot be overridden in derived classes. 

    Q14: What are the __construct() and __destruct() methods in a PHP class?
    A: Constructor an Destructor, like any other OOP language, in PHP5 can be defined using _construct() and _destruct() methods explicitly. Classes which have a constructor method call this method on each newly-created object.The destructor method is called as soon as there are no other references to a particular object.

    Q15: How we can get the number of elements in an array?
    A: Use the PHP count() or sizeof() function

    Q16: What are the 3 scope levels available in PHP and how would you define them?
    A: private: accessible within the class itself, public:accessible outside of the class, protected: accessible in parent and child classes 

    Q17: What are getters and setters and why are they important?
    A: These are methods to declare and retrieve variable values. They play important role since they provide a central mechanism for a variable definition and retrieval.

    Q18: What does MVC stand for and what does each component do?
    A: Model View Controller: an application design pattern which separates data logic layer(model), presentation layer(view) and business logic layer(controller) from each other. It provides a segregation of responsibility for better code maintenance.  

    Q19: What are SQL Injections, how do you prevent them and what are the best practices?
    A: SQL Injections are nefarious ways to modify database queries on the fly in such a way that it will make a database vulnerable to external threats. In order to prevent SQL Injection, one can use prepared statements and parameterized queries. In PHP, PDO(independent of underlying database) and MySQLi(specifically for MySQL).

    Q20: Why would you use === instead of ==?
    A: The detailed answer of same can be obtained in PHP Manual.

    Q21: What are PSRs ? Why would you follow a PSR standard?
    A: PSR stands for PHP Standard Recommendations, a PHP specification published by the PHP Framework Interop Group(PHP-FIG) similar to Java Specification Request(JSR) in case of Java. The objectives of PSRs are:
    a. To serve the standardization of programming concepts in PHP
    b. To enable interoperability of components
    c. To provide a common technical basis for implementation of proven concepts for optimal programming and testing practices.

    Post Top Ad

    Post Bottom Ad