PHP Interview Questions (Set1)

1) What is the difference between this and self?
http://www.programmerinterview.com/index.php/php-questions/php-self-vs-this/

2) What is __toString used for ?
If an object needs to be used as a string (e.g.: echo $obj;). If you don’t use __toString then the statement will produce fatal error.

3)
$var1 = ‘Welcome to ‘; $var2 = ‘TechInterviews.com’;
What will work faster?

Code sample 1: –
$var 3 = $var1.$var2;
Or code sample 2:
$var3 = “$var1$var2”;

Both examples would provide the same result – $var3 equal to “Welcome to TechInterviews.com”. However, Code Sample 1 will work significantly faster. Try it out with large sets of data (or via concatenating small sets a million times or so), and you will see that concatenation works significantly faster than variable substitution.

4) Explain difference of explode(), implode(), extract()
extract makes variables from array elements.

$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array)
//$color is now blue; 

5) Explain array_push(), array_pop(), array_shift(), array_unshift()
array_push : put elements at end of array
array_pop : retrieves element from end of array
array_shift : retrieve element from beginning of array
array_unshift : adds element to beginning of array

6) Explain parse_str() and parse_url()

parse_str Parses str as if it were the query string passed via a URL and sets variables in the current scope.

$str = "first=value&arr[]=foo+bar&arr[]=baz";

// Recommended
parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

parse_url() parses a URL and returns an associative array containing any of the various components of the URL that are present.

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';

var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_USER));
var_dump(parse_url($url, PHP_URL_PASS));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PORT));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url($url, PHP_URL_FRAGMENT));

array(8) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(8) "hostname"
  ["port"]=>
  int(9090)
  ["user"]=>
  string(8) "username"
  ["pass"]=>
  string(8) "password"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(9) "arg=value"
  ["fragment"]=>
  string(6) "anchor"
}

7) What is the “final” keyword in PHP?
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.

8) Which php profiler do you use?
xdebug is a debugging and profiling tool for php. Xdebug’s built-in profiler allows you to find bottlenecks in your script and visualize those with an external tool such as KCacheGrind. When Xdebug is activated it will show a stack trace whenever PHP decides to show a notice, warning, error etc. The information that stack traces display, and the way how they are presented, can be configured to suit your needs.
Xdebug’s basic functions include the display of stack traces on error conditions, time tracking, maximum nesting level protection (Controls the protection mechanism for infinite recursion protection. The value of this setting is the maximum level of nested functions that are allowed before the script will be aborted.)

9) Different between const and define()
http://stackoverflow.com/questions/2447791/define-vs-const

Leave a Reply