August 27th, 2007 - by cpradio

If you read ‘Yahoo Job Interview Questions: Part 1‘ because you were interested in the answers to their questions, then you will definitely be interested in Part 2; which answers questions 12 through 22.

So let’s get started!

Questions 12 – 22

  1. What is the difference between include, include_once? and require?

    All three allow the script to include another file, be it internal or external depending on if allow_url_fopen is enabled. However, they do have slight differences, which are denoted below.

    1. include()
      The include() function allows you to include a file multiple times within your application and if the file does not exist it will throw a Warning and continue on with your PHP script.
    2. include_once()
      include_once() is like include() except as the name suggests, it will only include the file once during the script execution.
    3. require()
      Like include(), you can request to require a file multiple times, however, if the file does not exist it will throw a Warning that will result in a Fatal Error stopping the PHP script execution.
  2. 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:

      1. header("Location:");
    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.
  3. 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 for more information.
    4. open_file()
      This is not a function in PHP, so it will fail with an error.
  4. 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().

  5. What does the following code do? Explain what’s going on there.

    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.

    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.

  6. 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’s strip_tags function. However, considering this is an interview question, I would write it like so:

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

  8. 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.

  9. 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 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.

  10. 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:

    1. echo "My string $variable";

    However, you can also use it like so:
    echo <<

  11. 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.

Updated: 08/27/2007
Changed # to % for Perl Hashes – Thanks to MrSpooky for pointing that out, can’t believe I forgot that!

15 Responses to “Yahoo Job Interview Questions: Part 2”

1 MrSpooky

Actually, hashes in perl start with a percent (%) sign

%hash = {};

# is what you use to comment lines out

Another difference is the indices in a PHP associative array use brackets like regular arrays, however perl uses curly braces:


2 cpradio

Thanks MrSpooky,

I have it corrected, once you stated the percent sign I was like “OMG! I didn’t screw that up…” But I did, and you caught it.

As for the associative array, that is true, but I just stuck to the question denoting the way the two languages distinguish between arrays and hashes, not necessarily their indexes, but it was nice of you to mention it.

3 Devthink

More about regular expressions

[…] one answers questions one through eleven and the second part wraps things up with the answers for questions twelve through twenty-two. […]

5 Jenn

16. Actually it does nothing because it’s riddled with errors…

17. I think the code you wrote is getting eaten by the browser. It doesn’t even really make sense and just throws a warning.

6 Fred

16 (as posted, maybe it contains a typo) will actually divide the numeric results of the regex since the result regex is not quote encapsulated. I think the fp value is something like 0.001622566. I would say this is a bug, and not the intended use of the function.

7 Fred

Also, for 17, I think your intent here might have been a regex like /\<(.*?)\>/. This will get the job done, but if you’re gonna use preg_replace, a better alternative would be /(<[^>]*>)/ (not sure if we need to backslash the tag wrappers).

Its just a more efficient match. The basic idea here is 1) match the opening delimiter, 2) match text which is not the closing delimiter, 3) match the closing delimiter.

8 cpradio

Sorry guys, my code did get rip to shreds. WordPress sucks when pasting code, I apologize.

I’ll try to get Mark to correct it for me when he gets a chance.


9 Golgotha

Ok, sorry, see if that’s better now…

Actually, I meant to mess things up, I was checking to see who was paying attention and besides it got some of you people to comment 🙂

Thanks Jenn and Fred for your feedback!

10 Jenn

You’re still missing quotes around the replacement string in #16. Yes, they’re missing in the original post on too.

There’s no way to say this without sounding b—-y, but you should really try actually running any code that you post… it saves people like me precious nagging time 😉

11 cpradio

Jenn, as much as I would like to say I didn’t I did. I wrote the whole thing on my PC in an editor and tested each line, pasted it into WordPress and clicked on Save. WordPress then removed a lot of the work I put in including breaking the code. I apologize for not catching all of them, but it is hard to really get it to work when the only one who can post such symbols and characters is the WordPress Admin.

I am going to assume the original article was missing them due to WordPress as well, but that is an assumption. Guess I won’t really know.

If you find any other mistakes I apologize in advance, but my original article on my PC did not have any of these…

12 Chandramohan

Thanks for Your Valuable Information…

Really it will the beginners like Me..

Thank You Once Again

13 Deepika Gupta


Its really great.


14 Jordan

Thanks a lot for this information. I’m going fo ra PHP/MySQL interview tommorow, and I feel confident I will do well thanks to this.


15 xHumanHealth

I really like these tips, thanks…

mulberry sale spyder womens jacket cheap new balance 574 mulberry outlet cheap new balance 574 arcteryx outlet mulberry sale spyder womens jacket mulberry sale spyder womens jacket mulberry outlet mulberry outlet new balance 574

Popular Articles

Top 10 Commentators

Subscribe to this feed! Subscribe by Email!

Random Bits Podcast

You need to download the Flash player from Adobe

Other Sites