| 0 comments ]

13.
What function would you use to redirect the browser to a new page?
1. redir()
This is not a function in PHP, so it will fail with an error.
2. header()
This is the correct function, it allows you to write header data to direct the page to a new location. For example:
PLAIN TEXT
PHP:
1.
header("Location: http://www.search-this.com/");

3. location()
This is not a function in PHP, so it will fail with an error.
4. redirect()
This is not a function in PHP, so it will fail with an error.
14.
What function can you use to open a file for reading and writing?
1. fget()
This is not a function in PHP, so it will fail with an error.
2. file_open()
This is not a function in PHP, so it will fail with an error.
3. fopen()
This is the correct function, it allows you to open a file for reading and/or writing. In fact, you have a lot of options, check out php.net for more information.
4. open_file()
This is not a function in PHP, so it will fail with an error.
15.
What's the difference between mysql_fetch_row() and mysql_fetch_array()?

mysql_fetch_row() returns all of the columns in an array using a 0 based index. The first row would have the index of 0, the second would be 1, and so on. Now another MySQL function in PHP is mysql_fetch_assoc(), which is an associative array. Its' indexes are the column names. For example, if my query was returning 'first_name', 'last_name', 'email', my indexes in the array would be 'first_name', 'last_name', and 'email'. mysql_fetch_array() provides the output of mysql_fetch_assoc and mysql_fetch_row().
16.
What does the following code do? Explain what's going on there.
PLAIN TEXT
PHP:
1.
$date='08/26/2003';
2.
print ereg_replace("([0-9]+)/([0-9]+)/([0-9]+)","\\2/\\1/\\3",$date);

This code is reformatting the date from MM/DD/YYYY to DD/MM/YYYY. A good friend got me hooked on writing regular expressions like below, so it could be commented much better, granted this is a bit excessive for such a simple regular expression.
PLAIN TEXT
PHP:
1.
// Match 0-9 one or more times then a forward slash
2.
$regExpression = "([0-9]+)/";
3.
// Match 0-9 one or more times then another forward slash
4.
$regExpression .= "([0-9]+)/";
5.
// Match 0-9 one or more times yet again.
6.
$regExpression .= "([0-9]+)";

Now the \\2/\\1/\\3 denotes the parentheses matches. The first parenthesis matches the month, the second the day, and the third the year.
17.
Given a line of text $string, how would you write a regular expression to strip all the HTML tags from it?

First of all why would you write a regular expression when a PHP function already exists? See php.net's strip_tags function. However, considering this is an interview question, I would write it like so:
PLAIN TEXT
PHP:
1.
$stringOfText = "

This is a test
";
2.
$expression = "/<(.*?)>(.*?)<\/(.*?)>/";
3.
echo preg_replace($expression, "\\2", $stringOfText);
4.
5.
// It was suggested (by Fred) that /(<[^>]*>)/ would work too.
6.
$expression = "/(<[^>]*>)/";
7.
echo preg_replace($expression, "", $stringOfText);

18.
What's the difference between the way PHP and Perl distinguish between arrays and hashes?

This is why I tell everyone to, "pick the language for the job!" If you only write code in a single language how will you ever answer this question? The question is quite simple. In Perl, you are required to use the @ sign to start all array variable names, for example, @myArray. In PHP, you just continue to use the $ (dollar sign), for example, $myArray.

Now for hashes in Perl you must start the variable name with the % (percent sign), as in, %myHash. Whereas, in PHP you still use the $ (dollar sign), as in, $myHash.
19.
How can you get round the stateless nature of HTTP using PHP?

The top two options that are used are sessions and cookies. To access a session, you will need to have session_start() at the top of each page, and then you will use the $_SESSION hash to access and store your session variables. For cookies, you only have to remember one rule. You must use the set_cookie function before any output is started in your PHP script. From then on you can use the $_COOKIE has to access your cookie variables and values.

There are other methods, but they are not as fool proof and most often than not depend on the IP address of the visitor, which is a very dangerous thing to do.
20.
What does the GD library do?

This is probably one of my favorite libraries, as it is built into PHP as of version 4.3.0 (I am very happy with myself, I didn't have to look up the version of PHP this was introduced on php.net). This library allows you to manipulate and display images of various extensions. More often than not, it is used to create thumbnail images. An alternative to GD is ImageMagick, however, unlike GD, this does not come built in to PHP and must be installed on the server by an Administrator.
21.
Name a few ways to output (print) a block of HTML code in PHP?

Well you can use any of the output statments in PHP, such as, print, echo, and printf. Most individuals use the echo statement as in:
PLAIN TEXT
PHP:
1.
echo "My string $variable";

However, you can also use it like so:
PLAIN TEXT
PHP:
1.
echo
2.
This text is written to the screen as output and this $variable is parsed too. If you wanted you can have HTML tags in here as well. The END; remarks must be on a line of its own, and can't contain any extra white space.
3.
END;

22.
Is PHP better than Perl? - Discuss.

Come on, let's not start a flame over such a trivial question. As I have stated many times before,

"Pick the language for the job, do not fit the job into a particular language."

Perl in my opinion is great for command line utilities, yes it can be used for the web as well, but its' real power can be really demonstrated through the command line. Likewise, PHP can be used on the command line too, but I personally feel it's more powerful on the web. It has a lot more functions built with the web in mind, whereas, Perl seems to have the console in mind.

Personally, I love both languages. I used Perl a lot in college and I used PHP and Java a lot in college. Unfortunately, my job requires me to use C#, but I spend countless hours at home working in PHP, Perl, Ruby (currently learning), and Java to keep my skills up to date. Many have asked me what happened to C and C++ and do they still fit into my applications from time to time. The answer is primarily 'No'. Lately all of my development work has been for the web and though C and C++ could be written for the web, they are hardly the language to use for such tasks. Pick the language for the job. If I needed a console application that was meant to show off the performance differences between a quick sort, bubble sort, and a merge sort, give me C/C++! If you want a Photo Gallery, give me PHP or C# (though I personally find .NET languages better for quick GUI applications than web).

I would like to take this time to challenge other companies to post their interview questions or feel free to email them to me at search-this [at] cpradio [dot] org. I will be glad to read through them, and write an article about them revealing their answers for everyone to learn.

Changed # to % for Perl Hashes - Thanks to MrSpooky for pointing that out, can't believe I forgot that!

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.