Tricky Php questions where I failed doing a self test before the PHP Certification

Preparing for the exam in a few hours. There are many functions I never used used in the latest 6 years, and that’s all fine, since PHP is quite a bloated language. So I never had the need to use things as mysql_real_escape_shit :)

Or ucword ? WTF is that ? Anyways now learning the latest tidbits, and so far got 41 questions right from 70 which is a lame 5,5 and for sure won’t get approved if I rush on to get it done on time. So I will take it slowly.

#1 Which of the following is a valid way to pass the $callback parameter expected by array_walk()? 

1) An anonymous function
2) An array containing an instantiated object as the first element, and
the method name as the second element
3) A string containing the function name
This question is about callbacks – only these three items in the list are valid ways to specify a callback

I will make a short example of the #2:

<!–?php

class myobj{

   static function test($v){
     echo $v;
   }
}

$arr=array(1,2,3,4);

array_walk($arr,array(“myobj”,”test”));

 

// This is the right way to pass the object and the method that will be executed per array item.

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, ‘myCallbackMethod’));

more types in the php docs.

// Method is called statically so it should be declared that way or you get a warning.

#2 Right way to parse a webpage as XML?

DOMDocument::loadHTML(file_get_contents(‘http://yourpage.com&#8217;));

 

#3 A brief explanation about bitwise operators

$b= 4<< 2 + 2;

First of all, via operator precedence 2+2 is executed first resulting in:

$b= 4<< 4;

<< left shit, means that it's multiplied 2 ala n, so in this case: 4 *2*2*2*2 = 64

>> right shift would mean divide, so :  64 >> 4 = 4

#4 The standard PDO fetch by default

Does it return an:
  • Associative Array
  • Numeric Array
  • or Both ?

Leave your answer in the comments :)

Another questions about PDO query, ask about what it performs. Query in PDO does two things:
prepare() and execute(). Only that! Don’t be tempted to add a fetch, that you do afterwards, lazy programmer.

#5 The PCRE tricky question

$pattern= ‘/[a-z]{4} /’;

$string = ‘Mary was a little bitch’;

$matches = preg_match( $pattern, $string );

print_r($matches);
~

There is always some tricky question like this. So what returns this ?

A 1.    Not the matches, to get those $matches should be added in as a third parameter.  But in this way, preg_match just returns true if the match is found, which in this case would be ‘ttle ‘ since bitch won’t do it since it’s missing a space at the end. Bitchy, doesn’t it ?

#5b The PCRE tricky question in bitch mode

$pattern= ‘# \w*?[aeiou]{2}\w*? #’;

$string = “There’s a lion loose in the Maister park”;

$matches= array();

preg_match_all($pattern, $string, $matches);

First of all, the # delimiters are valid delimiters, the only requirement is that whatever you pick to match as the start should be matched at the end.  Note that as ” lion ” is matched, eats the space in “loose ” so loose will NOT match. And there you may be trapped.

There’s a lion loose in the Maister park
Array (
[0] => Array (
[0] => lion
[1] => Maister
)
)

#6 The HTTP status questions

Everybody and his mum, know about 404 not found.  But in this exam there come another statuses as:

  • 401  – Not authorised, put your user and password in this little box
  • 400 – Bad Request
  • 403 – Forbidden
  • 408 – Request Timeout

#7 The PHP magic methods

Remember the magic list:

  • __construct
  • __destruct
  • __call
  • __callStatic
  • __get
  • __set
  • __isset
  • __unset
  • __sleep
  • __wakeup
  • __toString
  • __invoke
  • __set_state
  • __clone       Some questions may ask what magic methods is instanced when you clone a class. Well it’s pretty obvious, don’t get lost there, is this one.

Analog B&W scan done today:
Image

One thought on “Tricky Php questions where I failed doing a self test before the PHP Certification

  1. fetch_style
    Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants, defaulting to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH).

Comments are closed.