Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test PHP Skill
<?php
/*****************************************
* Created this test for job interviews. *
*****************************************/
/**
* Directions
*
* Read the direction in the Q and respond below A.
*
* All answers should be working code.
*/
//------------------------------------------------ Question 1
// Q: Find and replace (use the instructions within the string)
$str = "In this snippit we want to replace txet with text and text with txet.";
// A:
//------------------------------------------------ Question 2
// Q: Reverse the order of the first five directories of this path. Return a string.
$path = "/a/b/c/d/e/f/g/h/i";
// A:
//------------------------------------------------ Question 3
// Q: Refactor and clean up this piece of code. (REQ PHP 5.3+)
$a=10;$A=function(&$a){$B=function($b)use($a){return $a*$b;};$a=$B($a++);};++$a;$A($a);print $a;
// A:
//------------------------------------------------ Question 4
// Q: Turn array into object
$array = array(1,'a',array('a' => 'A', 'b' => array('c' => array(1,2,3,4,5,'c' => array(1,2,3,4,5,'c' => array(1,2,3,4,5,'c' => array(1,2,3,4,5)))))));
// A:
//------------------------------------------------ Question 5
// Q: Refactor, fix and optimize this code (Result should have no leading or trailing whitespace and all newline characters should display properly)
// Optional, use comments to explain your reasoning
$str = ' Some string here / some string there ';
if(strpos(' ', $str) != false) {
$str = trim($str);
}
$str = preg_replace('/\//', '\n', $str);
$words = split(' ', $str);
$str = '';
for($i = 0; $i < count($words); $i++) {
if(preg_match('/some/', $words[$i])) {
$str = trim($str);
$words[$i] = 'Some';
}
$str .= $words[$i] . ' ';
}
$str = trim($str);
// A:
//------------------------------------------------ Question 6
// Q: Write a simple class which can do the following.
$person = Person::make(array('first_name' => 'John', 'last_name' => 'Smith', 'date_of_birth' => '1984/04/06'));
print $person->getFullName(); // John Smith
print $person->getDateOfBirth(); // 1984/04/06
print $person->getDateOfBirth('F j, Y'); // April 6, 1984
$alpha = array('a','b','c','d','e','f','g','h');
$rand = array_rand($alpha, 3);
$randVar = $alpha[$rand[0]] . $alpha[$rand[1]] . $alpha[$rand[2]];
$person->{$randVar} = 'xyz';
print $person->{$randVar}; // xyz
// A:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.