PHP Functions Flashcards
Task: Replace a backslash with a forward slash
$string = str_replace(‘\’, ‘/’, $string);
Task: Determine if a given filename exists in the specified location
if (file_exists($filename)) { // do stuff }
Task: Use a function to filter a URL to make sure it’s safe
$url = filter_var($this->url, FILTER_SANITIZE_URL);
Task: Write a function to remove the whitespace off the ends of a string.
$str = trim($string);
Task: Given a string with colons as a separator, split the string up into components into an array.
$array = explode(“:”, $input);
Task: Determine if a string exists in an array of srings
if (in_array($string, $array)) { // Do stuff }
Task: Get all of the methods names from a class
$methods = get_class_methods($obj);
or
$methods = get_class_methods(‘className’);
Task: Convert all strings in an array to lowercase
$yourArray = array_map(‘strtolower’, $yourArray);
Task: Sort an array that has keys, without damaging the keys
bool uasort ( array &$array , callable $value_compare_func )
NOTE: $value_compare_func should do a test between the two values and return -1 or 1 (not much point in returning 0)
Task: Sort an array - keys don’t matter
bool usort ( array &$array , callable $value_compare_func );
NOTE: $value_compare_func should test between two values and return -1 or 1 (not much point in returning 0)
Task: Make the first letter in a string uppercase
string ucfirst ( string $str );
Task: Format a decimal number into thousands for nice display
string number_format ( float $number [, int $decimals = 0 ] );
OR (note: only 1,2 or 4 parameters allowed)
string number_format ( float $number , int $decimals = 0 , string $dec_point = “.” , string $thousands_sep = “,” )
Task: Create a string from an array, that is separated by a ‘glue’ string
string implode ( string $glue , array $pieces );
glue could be ‘,’ to create a csv
Task: Make the first characters of a string lowercase.
string lcfirst ( string $str )
Task: Compare two strings for equality
int strcmp ( string $str1 , string $str2 )
Returns 0
Task: Parse a url, and return an associative array with it’s components
mixed parse_url ( string $url [, int $component = -1 ] );
Task: Take a string and parse it like a URL, outputing the variable / value combinations into new variables
void parse_str ( string $str [, array &$arr ] )
NOTE: It does not return a value - it will write the values into new variables
Question: What is the issue with using parse_str?
If you don’t pass an array as the second argument, values are written to the current context, and could overwrite values. Using an array means they are written to the array
Task: Which function would you use to compare two strings and ensure it takes into account the current locale?
int strcoll ( string $str1 , string $str2 )
NOTE: Not binary safe
Question: Which function is just an alias of implode?
join
Task: You need to cut whitespace or other characters from the end of a string.
string rtrim ( string $str [, string $character_mask ] )
Question: What function is an alias of rtrim?
chop
Task: You have to parse a comma seperated string into an array?
array str_getcsv ( string $input [, string $delimiter = “,” [, string $enclosure = ‘”’ [, string $escape = “\” ]]] )
Question: What is the case insensitive version of str_replace?
str_ireplace
Task: read a single line of a file and write the contents into an array
array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = “,” [, string $enclosure = ‘”’ [, string $escape = “\” ]]]] )
Question: What needs to be done before fgetcsv can be used?
A file must be opened using fopen (it requires a file handle)
Task: Convert data to a JSON format
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
Question: What does the JSON_NUMERIC_CHECK bitmask do?
Checks for a string being a number, if so will encode it as a number (so not as a string)
Question: What does the JSON_FORCE_OBJECT bitmask do?
It encodes the array as an object
Question: What does JSON_PRETTY_PRINT do?
It formats the output of json_encode in an easy to read manner (useful really only for debugging since it adds overhead)
Task: Use a function to decode a string of JSON
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
Question: What parameter do you set on json_decode to return an associative array instead of an object?
The second parameter should be set to true
Question: If a document is 3 levels deep, and you specify a max of 2 levels of recursion - what is returned, the first two levels or null?
null
Task: Use a function to determine what the last error was from a json decode or encode
int json_last_error ( void )
Task: The datasource is not under your control and you want to make sure the output of json_encode is UTF-8 compliant (or you will raise a JSON_ERROR_UTF8 when decoding) What function do you use to ensure UTF-8 encoding?
string utf8_encode ( string $data )’
utf8_encode(json_encode($payload));
Task: Use a built in php function to configure an error handler function.
mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )