Advanced PHP Practice Interview Questions And Answers Part 3

This is the last portion of our PHP practice interview questions and answers. Here is part 3 of the PHP practice interview question:


Given the previous two portions of this question, can you name some of the drawbacks or disadvantages of the solution you provided?

And, here are parts 1 and 2 of the original question for your convenience:


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


The code should also be able to handle the URL in the src attribute of an iframe.

There are a lot of potential problems with the solution that we gave for this interview question. And, some of those problems are unavoidable given that this is an interview question, and you don’t have a few weeks to give a perfect answer. Try to see if you can think of any potential problems on your own.

No Javascript was executed to find additional resources

One disadvantage is the fact that our PHP code does not try to execute any Javascript in order to find additional resources. What exactly does that mean? Well, many websites have some Javascript code that, when executed, will request and display specific resources on their page – like gif’s, jpg’s, swf’s, or whatever else the Javascript want’s to display (it’s really up to whoever wrote the Javascript code).

One specific example of Javascript requesting and displaying resources are websites (like this one) which use Google Adsense to put Google’s ads on their website. In order to do this, a Javascript script is provided by Google. And, some Javascript variables are also passed to the script to tell the script what size the ad should be.

That Javascript is then executed by the browser and the correct ad is generated – it could be a SWF file (which is a Flash format), a jpg, a gif, or whatever file type Google determines is appropriate to display at the moment for whoever is viewing the webpage.

But, we can’t possibly be expected to run this Javascript in order to see what kind of resource is generated – doing this could be tricky. And, there’s no guarantee that trying to execute this Javascript would even be successful. This means that this will be one less resource that will be counted – and even more if there are multiple ads on a page (like there are on this page). So, that’s one drawback of the PHP code we came up with to answer this interview question. And, this means that less HTTP requests are counted than the true number of HTTP requests on pages that use Javascript to request additional resources.

Checking for duplicate resources

One thing we admittedly do not do in our implementation is check for duplicate resources – like images/CSS/Javascript files being referenced more than once in the HTML. Even though the files are referenced multiple times in the HTML file, they still result in only one HTTP request because browsers are smart enough to only make the request once. But, we are counting each one of those as a separate request – this should be fairly simple to fix, but it is an issue with our code which could result is double-counting of HTTP requests.

Checking for resources inside CSS documents

Another thing that we did not do is check for resources named inside external CSS documents – like “background-image”. That means our implementation will not count those resources, which would lead to a total number of HTTP requests that is lower than the actual number.

Checking for browser specific code

Another thing we did not do in our implementation is check for browser specific code – as in the HTML tags that look like this: “<!–[if IE]>”. This could also potentially affect the HTTP request count and total download size that our implementation reports, because certain files could conditionally be included depending on what browser the user is using. A common usage of this is to use a different stylesheet for older Internet Explorer browsers. This would mean that our implementation would double count – both the stylesheet for Internet Explorer and the stylesheet for non-Internet Explorer browsers.

Browser cache

One thing that may be challenging is accounting for the resources which are already cached by the browser. If a resource is cached, then it means that the browser will not generate a new HTTP request for that resource because the browser will just use the version of the resource that it has saved in it’s cache. This means that since we are not even taking browser caching into account in our implementation (something that would probably be very difficult), in situations where cached resources are used we would definitely be over-estimating the number of HTTP requests because we just count every single resource in the HTML as an HTTP request.

Final thoughts on Advanced PHP interview question and answer part 3

Now, those are just some of the potential drawbacks of our answer to the PHP interview question. Remember, you can see the final solution we came up with right here: PHP Interview Question Part 2. Writing something that is very accurate given all the complexities would be very challenging and time consuming – and would certainly not be expected in a PHP interview question like this. Most likely, what interviewers are looking for with a question like this is that you have some essential PHP skills, and a good foundation of knowledge for how the web works.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

One thought on “Advanced PHP Interview Questions And Answers Part 3”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.