Thursday, 4 September 2014

PHP Interview Questions And Answers

Here we present some more challenging practice PHP interview questions and answers that were asked in a real interview for a PHP web developer position. These questions are really good to not just test your PHP skills, but also your general web development knowledge. We think that you will benefit a lot, and gain some good practice by going through these questions. The questions are for intermediate to somewhat advanced PHP software engineers, but even if you are just a beginner or fresher you should be able to understand the answers and explanations we give – but you may not be able to come up with the answers on your own. Here is the first part of the question – read it carefully to really understand it, and we give a simple, easy to understand explanation of everything in this question:
Write a PHP script to report the total download size of any URL. You may not use any 3rd-party code that performs the entire task described below.
No HTML interface is necessary for this exercise; you can write this as a command-line script that accepts the URL as an argument.
For a single-file resource such as an image or SWF, the script would simply report on the total size of the document.
For a complex resource such as an HTML document, the script would need to parse it to find references to embedded, included resources: javascript files, CSS files, iframes, etc.
The goal of this exercise is to output the following information for a given URL:
- total number of HTTP requests
- total download size for all requests

So, there are 2 primary goals that this question asks us to solve: For any URL, find the total number of HTTP requests generated by that URL, and also find the total download size for all requests. You may not understand what is meant by an HTTP request, but don’t worry we explain it all below.
We’ll have to break down this question into more manageable pieces since it is a lot to comprehend. So, we’ll go with the divide and conquer approach. Let’s start with the easier parts of the question first.

Accepting arguments in PHP scripts


The question says that “No HTML interface is necessary; you can write this as a command-line script that accepts the URL as an argument”.
So, let’s just say that we want to just write this as a command line script. The question is how do we retrieve arguments inside a PHP command-line script?
Well, if we plan on having the script called from the command line as “ourscript.php www.theurl.com”, where the URL is passed as an argument, then inside the PHP script we can grab the URL value by using the PHP variable “$argv[1];”. Inside our PHP script the code to retrieve the URL passed in as an argument would look like:
/*  If this script is invoked as ourscript.php www.theurl.com,
     then $argv[1] will hold the value www.theurl.com, and 
     that value will be stored in the $URL variable as well
*/

$URL = $argv[1];

That’s very simple code – now, let’s move on to other parts of the question.

How to connect to a URL in PHP?

It should also be clear that we will need to somehow be able to connect to a URL and view the contents of the page that the URL points to. What is the best way to do this? Well PHP provides a library called cURL that may already be included in your installation of PHP by default. cURL stands for client URL, and it allows you to connect to a URL and retrieve information from that page – like the HTML content of the page, the HTTP headers and their associated data, etc. You will see the use of cURL in our code below – don’t worry if you’ve never used cURL before, it’s fairly easy to understand!

0 comments:

Post a Comment