PHP interview questions

PHP quiz questions

  • 1.

    What is the difference between $var and $$var?

    Answer:

    $$var sets the value of $var as a variable.

                $day='monday';
                $$day='first day of week';
                echo $monday; //outputs 'first day of week'
                

    http://us2.php.net/manual/en/language.variables.variable.php

    View
  • 2.

    Are objects in PHP 5 passed by value or reference?

    Answer:

    This is basically a trick questions. To understand how they are passed you need to understand how objects are instantiated. Study the below link:

    http://ca2.php.net/manual/en/language.oop5.references.php

    View
  • 3.

    What are some magic methods in PHP, how might you use them?

    Answer:

    Magic methods are basically triggers that occur when particular events happen in your coding. __GET, __SET are magic methods that are called (behind the seen) when you get or set a class property.

    http://php.net/manual/en/language.oop5.magic.php

    View
  • 4.

    Does PHP support multiple inheritance?

    Answer:

    No. You should understand what multiple inheritance is.

    http://en.wikipedia.org/wiki/Multiple_inheritance

    View
  • 5.

    What is the meaning of a final class and a final method?

    Answer:

    Final keywords indicates that the class or method cannot be extended.

    http://php.net/manual/en/language.oop5.final.php

    View
  • 6.

    What does "&" mean in '&$var' ?

    Answer:

    '&' indicates a reference

    http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html

    View
  • 7.

    What does ob_start do?

    Answer:

    Makes it so PHP does not output anything. Companies ask this because many large frameworks wrap a bunch of code in ob_start() and ob_get_clean(). So understanding how that function works is pretty important.

    http://myphpsource.blogspot.com/2010/01/obstart-save-php-output-to-string-php.html

    View
  • 8.

    What is the difference between single-quoted and double-quoted strings in PHP?

    Answer:

    PHP strings can be specified not just in two ways, but in four ways.

    1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
    2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
    3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
    4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'No parsing is done in nowdoc.

    Speed:
    I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

    View
  • 9.

    What are some PHP Design patterns you have worked with?

    Answer:

    Design patterns are simply commonly used techniques within your code, they often are implemented in different ways so they can be a bit tricky to grasp without writing them yourself. If you are unfamiliar with them I would start by learning the Singleton Pattern and the Strategy Pattern.

    http://www.ibm.com/developerworks/library/os-php-designptrns/

    View
  • 10.

    What is the Scope Resolution Operator?

    Answer:

    "::" double colons is the scope operator it is used to call methods of a class that has not been instantiated. You should also understand static methods and how they differ from regular methods.

    http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php 
    http://php.net/manual/en/language.oop5.static.php

    View
  • 11.

    What is the value of "$day" in the below code?

    Answer:

    $wed= 1;    
    $day = ($wed==1) ? 'today' : 'tommorrow';
    // $day is now set to 'today'
    

    Companies often ask about the ternary operator (?). which is simply a shorthand for if else statements.

    http://davidwalsh.name/php-shorthand-if-else-ternary-operators

    View
  • 12.

    How do you load classes in PHP?

    Answer:

    They are trying to gauge your understanding of how class auto loading works. Review the "autoload" and "spl_autoload_register" function (note:you should use the later). The autoload function basically triggers a function when a class is instantiated, you can put whatever logic you like in it but generally you want to include the class file based on some sort of naming convention.

    http://www.php.net/manual/en/function.spl-autoload-register.php

    View
  • 13.

    What is Polymorphism?

    Answer:

    Don't get scared by the big word. It's simply the idea that one object can can take on many forms. So in PHP OOP one class "cars" may have two classes that extend it, for example a "Honda" class and a "BMW" class.

    http://stackoverflow.com/questions/210460/try-to-describe-polymorphism-as-easy-as-you-can

    View
  • 14.

    In a PHP class what are the three visibility keywords of a property or method?

    Answer:

    public, private and protected. The default is public. 
    Public -> Any class may instantiate the class and call the method or property. 
    Protected -> Only the class itself or inherited (children) classes may call a method or property.
    Private -> Only the class itself may call a method or property.

    http://php.net/manual/en/language.oop5.visibility.php

    View
  • 15.

    What is the difference between $_GET and $_POST

    This is a great question because an interviewer can tell how deeply you understand HTTP and the request/response. If you don't have good understanding of HTTP protocol, google around and get a grasp on it. 
    Good answer 
    Explain the HTTP protocol and how every request contains a method, generally(GET,POST,PUT,DELETE) and what each method signifies. 
    Bad answer 
    $_GET stores variables passed in url as query strings. $_POST stores variables past from using method = $_POST 

    http://djce.org.uk/dumprequest

    View
  • 16.

    What are some of the big changes PHP has gone through in the past few years?

    Answer:

    There are a number, but the big ones people are looking for are: 
    a. PHP 5.0 realised the object model (AKA OOP).
    b. 5.1 added PDO - for accessing databases.
    c. 5.3 - added namespace support and late static bindings.

    http://en.wikipedia.org/wiki/PHP#Release_history

    View
  • 17.

    Explain how a PHP session works?

    Answer:

    A PHP session cookie is set in the clients browser, on every request the client sends that cookie to the server. PHP then uses that cookie to select the corresponding session information. By default PHP session_start() will store session data in files, you can also store sessions in a database.

    View
  • 18.

    What is MVC?

    Answer:

    Most programmers know this, but interviewers will likely look for a deep understanding of MVC, and some explanation or examples on how/why/ when you used it. 

    MVC - Model, View, Controller - is simply a way of organizing your code into 3 separate layers each with there own job. 

    Model - Usually contains data access code and all of you business logic code. 
    View - Contains markup/design code, generally html,xml, json. 
    Controller - Usually contains very little code, just whatever is needed to call the Model code and render the View code.

    http://www.symfony-project.org/book/1_0/02-exploring-symfony-s-code

    View
  • 19.

    In PHP what is the difference between a Class and an Interface?

    Answer:

    Interfaces do not contain business logic, only method signatures that define a template that any classes implementing the interface must contain. Lets take an auto mobile for example. If we were to create and interface for a car we would want to define a few methods like drive, stop, turn left , turn right. This mean that any vehicle that is a car (aka implements the interface car) must have methods for these things, If they do not PHP will throw an error. So if your car is an BMW , Honda or Ford it must be able to stop. How it stops is up to each car (or PHP class) but it must be able to stop. Technically we can decide not to use an interface for cars, but then some types of cars are not forced to have a "stop" method.

    View
  • 20.

    What is Object Oriented Programming?

    Answer:

    This is usually a pretty open ended question. You should understand classes (objects are instantiated classes), abstract classes, interfaces, methods, properties,inheritance, multiple inheritance as well as why OOP is helpful as compared to procedural programming.

    http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/

    View

© 2017 QuizBucket.org