PHP Interview Questions – 2

PHP Interview Questions

This post is just an attempt to help you guys to know and understand how tricky a PHP interview can be, what type of questions can be asked to you and how effectively you can tackle those questions. I do not guarantee that these will be the questions you will be asked in the interview, it is just an attempt to guide you in your preparations.

So, lets start with the question & answer session.

Question-1. What are magic functions or methods in php?

Answer: There are various magic functions or methods in PHP for example – __construct(), __destruct(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), etc. You cannot have these magic methods or functions in your class until you want the function to work as a magic method.

Question-2. What are the differences between echo and print in PHP?

Answer: echo and print are largely the same in PHP. Both are used to output data to the screen even then there is some difference in the use of the twos –

a. “echo” does not return a value whereas “print” does return a value of 1 (this enables print to be used in expressions).

b. “echo” can accept multiple parameters (although such usage is rare) while print can only take a single argument.

Question-3. Write the code to fetch data from a Website using CURL.

Answer:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example_url_dot_com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

Question-4. How can we get the current session id?

Answer: You can get the current session id using session_id();

Question-5. How can we submit from without a submit button?

Answer: This question is generally asked to freshers or with 1-2 year experience. The answer to this question is “We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form.”


Question-6. How can we get the properties (size, type, width, height) of an image using php image functions?

Answer: We get the above requirement full-filled using the following image type function.
-Know the Image type use exif_imagetype () function
-Know the Image size use getimagesize () function
-Know the image width use imagesx () function
-Know the image height use imagesy() function

Question-7. How do you get the user’s ip address in PHP?

Answer: We can get the clients ip address using the following variable $_SERVER[‘REMOTE_ADDR’];

Related Post