PHP Interview Questions and Answers For Experience
Q #1) Which functions are used to remove whitespaces from the string?
Answer:
There are three functions in PHP to remove the whitespaces from the string.
trim() – It removes whitespaces from the left and right side of the string.
ltrim() – It removes whitespaces from the from the left side of the string.
rtrim() – It removes whitespaces from the from the right side of the string.
Sample code:
1
$str = ” Tutorials for your help”;
2
$val1 = trim($str);
3
$val2 = ltrim($str);
4
$val3 = rtrim($str);
Q #2) What is a persistence cookie?
Answer:
A cookie file that is stored permanently in the browser is called a persistence cookie. It is not secure and is mainly used for tracking a visitor for long times.
This type of cookie can be declared as follows,
setccookie (“cookie_name”, “cookie_value”, strtotime(“+2 years”);
Q #3) How can a cross-site scripting attack be prevented by PHP?
Answer:
Htmlentities() function of PHP can be used for preventing cross-site scripting attack.
Q #4) Which PHP global variable is used for uploading a file?
Answer:
$_FILE[] array contains all the information of an uploaded file.
The use of various indexes of this array is mentioned below:
$_FILES[$fieldName][‘name’] – Keeps the original file name.
$_FILES[$fieldName][‘type’] – Keeps the file type of an uploaded file.
$_FILES[$fieldName][‘size’] – Stores the file size in bytes.
$_FILES[$fieldName][‘tmp_name’] – Keeps the temporary file name which is used to store the file in the server.
$_FILES[$fieldName][‘error’] – Contains error code related to the error that appears during the upload.
Q#5) What is meant by public, private, protected, static and final scopes?
Answer:
Public– Variables, classes, and methods which are declared public can be accessed from anywhere.
Private– Variables, classes and methods which are declared private can be accessed by the parent class only.
Protected– Variables, classes, and methods which are declared protected can be accessed by the parent and child classes only.
Static– The variable which is declared static can keep the value after losing the scope.
Final– This scope prevents the child class to declare the same item again.
Q #6) How can image properties be retrieved in PHP?
Answer:
getimagesize() – It is used to get the image size.
exif_imagetype() – It is used to get the image type.
imagesx() – It is used to get the image width.
imagesy() – It is used to get the image height.
Q #7) What is the difference between abstract class and interface?
Answer:
Abstract classes are used for closely related objects and interfaces are used for unrelated objects.
PHP class can implement multiple interfaces but can’t inherit multiple abstract classes.
Common behavior can be implemented in the abstract class but not an interface.
Q #8) What is garbage collection?
Answer:
It is an automated feature of PHP.
When it runs, it removes all sessions data which are not accessed for a long time. It runs on /tmp directory which is the default session directory.
PHP directives which are used for garbage collection include:
session.gc_maxlifetime (default value, 1440)
session.gc_probability (default value, 1)
session.gc_divisor (default value, 100)
Q #9) Which library is used in PHP to do various types of Image work?
Answer:
Using GD library, various types of image work can be done in PHP. Image work includes rotating image, cropping an image, creating image thumbnail etc.
Q #10) What is URL rewriting?
Answer:
Appending the session ID in every local URL of the requested page for keeping the session information is called URL rewriting.
The disadvantages of this methods are, it doesn’t allow persistence between the sessions and, the user can easily copy and paste the URL and send to another user.
Q #11) What is PDO?
Answer:
The full form of PDO is PHP Data Objects.
It is a lightweight PHP extension that uses consistence interface for accessing the database. Using PDO, a developer can easily switch from one database server to the other. But it does not support all the advanced features of the new MySQL server.