PHP Interview Questions I

1.What does <?= and ?> do in PHP?

  • Answer

    The output is displayed directly to the browser.

     <?="website".55?>  // outputs website55 

    TIPTIP: If you’re using short tags, be sure the “short_open_tag = on” is enabled in your php.ini

    NOTENOTE: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

2. What are the diferent type of errors in PHP?

  • Answer

    There are three basic types of runtime errors in PHP:

    1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although you can change this default behavior.

    NOTEExecution of the script is not terminated, but it is possible there is an error in your code.

    2. Warnings: These are more serious errors – for example, attempting to include() a file which does not exist.

    TIP By default, these errors are displayed to the user, but they do not result in script termination.

    3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. Internally, these variations are represented by twelve different error types.

    TIPThese errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

    Here is the complete list for all types of errors in PHP.

3. What are the differences between require / include, require_once / include_once?

  • Answer 1

    require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.But require() and include() will do it as many times they are asked to do.

  • Answer 2

    The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. The major difference between include() and require() is that in failure include() produces a warning message whereas require() produces a fatal errors.

  • Answer 3

    All three are used to an include file into the current page.
    If the file is not present, require(), calls a fatal error, while in include() does not.
    The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists.

  • Answer 4

    File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.

    NOTICENOTE: For faster execution, try to use absolute path when calling these functions.

4. What is meant by urlencode and urldecode?

  • Answer 1

    urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits.

    For example: urlencode(“10.00%”) will return “10%2E00%25″. URL encoded strings are safe to be used as part of URLs.

    urldecode() returns the URL decoded version of the given string

  • Answer 2

    string urlencode(str) – Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version:

    Alphanumeric characters are maintained as is.
    Space characters are converted to “+” characters.
    Other non-alphanumeric characters are converted “%” followed by two hex digits representing the converted character.

    string urldecode(str) – Returns the original string of the input URL encoded string.

    For example:

    $procent ="10.00%";
    $url = "http://example.org/exec.php?procent=".urlencode($procent);
    echo $url;
    

    You will get “http://example.com/exec.php?procent=10%2E00%25″.

5. What’s the difference between htmlentities() and htmlspecialchars()?

  • Answer

    htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.

6. What’s the difference between PHP4 and PHP5?

  • Answer

    There are several differences between PHP4 and PHP5.
    1. Unified Constructor and Destructor.
    2. Exception has been introduced.
    3. New error level named E_STRICT has been introduced.
    4. Now we can define full method definintions for a abstract class.
    5. Within a class we can define class constants.
    6. Public, private and proted method introduced

    A great article you may read here.

7. What’s the difference between PHP5 and PHP6?

  • Answer

    1. The operators continue and break will work with a constant parameter or without parameter.
    2. The ereg() function for regular expressions will be deleted. It will be replaced by preg_match() whose format is different but more generally used.
    3. The compatibility with older formats like GD 1 and Freetype 1 will no longer be supported.
    4. Magic_quotes, that was very decried as a source of security holes will no longer be supported.
    5. The same is true for register_global, safe_mode and register_long_arrays.

Posted by admin in PHP/mySQL on 15th Dec, 2009
tabs-top

1 Comment »

  1. webdagger

    Hey thanks a lot for sharing useful interview questions….. which will be very helpful while attending the interviews…..
    glad i found ur site…really a very helpful site…..
    by the way check out my collection of php interview questions from here: php interview questions

Leave a comment

SEO Blogs - BlogCatalog Blog Directory