PHP 2 Flashcards
How do you obtain the IP address of a client?
$_SERVER[‘REMOTE_ADDR’];
Please note this is note safe as it can be faked by the client. It may also not be the ip of the client, rather the proxy
How do you determine the type of web browser that the user is using?
$_SERVER[‘HTTP_USER_AGENT’];
What constant in PHP stores the current file name?
__FILE__
What is one way of getting the current file name?
$current_file = basename($_SERVER[‘PHP_SELF’]);
What global variable (not constant) holds the name of the current file?
$_SERVER[‘PHP_SELF’]
In a regex expression, what does \b match?
Word boundaries. Very useful for picking the first letter in a sentence for example:
/(\b[a-z])/i
What would one way be to retrieve a web page and print the source out with line numbers?
Use ‘file’ to obtain an array of the lines, then print them out passing each line through htmlspecialchars
What is the difference between sort and usort?
usort uses a user defined comparison function
If you unset an element in an array, are the integer keys reordered?
No - it is common to use unset to remove an element out of an array, but elements after it will continue their numbering scheme. Use sort to fix this.
By default json_decode returns data in objects, how do you make it return the data in an associative array?
Set the second parameter to true.
Why use array_walk_recursive over array_walk?
In some cases an array will contain another array (i.e. nested json) - array_walk will fail in this case.
You can sort an array by key ksort - how would you sort the same array in reverse?
krsort
You can sort an array by value using asort, how would you sort the same array in reverse?
arsort
You’ve been asked to add up all the elements in an array, do you write a function or use a function in built in php
Use an inbuilt function, namely array_sum
What function can be used to make an array of arrays?
array_map - if you pass the callback function as null, it will create an array of arrays from a given set of arrays.
What does is_scalar test for?
Whether a value is scalar or not - a scalar variable is an atomic variable, whereas arrays, objects and resources are not scalars
How would you obtain an array of numbers that fit within a range. I.e. from 0-16, but in steps of 4?
range(0,16,4);
How could you create a random list of numbers in a range, without calling rand()?
$a = range(1, 10);
shuffle($a);
$a is now an array containing a random order of numbers
How would you take a string and convert it into an array of characters so they can be traversed?
$array_char = str_split($str);
How could you use substr to check each character in a string
for ($x = 0; $x < strlen($str); $x++) { if (substr($text, $x, 1) == $search_char) { // Do stuff } }
How would you tell curl to inform a server that you can recieve compressed data to your application?
$header[] = “Accept-Encoding: compress, gzip”;
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $header);
Why is it important that a webbot emulate a standard browser?
Firstly, it reduces the risk of sites from detecting the presence of a web bot - but in cases where you want compressed files, most servers check for valid web clients before sending compressed data.
What test can you do to determine if the incoming file is compressed?
if (stristr($http_header, “zip”))
$compressed = TRUE;
(This assumes you’ve extracted the header into $http_header)
What function could you use to decompress a file in PHP?
gzuncompress($filename);