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