Php Interview Questions
Question-1. What is PHP ?
Answer- PHP is a recursive acronym for "PHP: Hypertext Pre-processor". PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
Question-2. What are the common usage of PHP ?
Answer- Common uses of PHP −
• PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close
them.
• PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data,
return data to the user.
• You add, delete, and modify elements within your database through PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• It can encrypt data.
Question-3. In how many ways you can embed PHP code in an HTML page ?
Answer- All PHP code must be included inside one of the three special mark-up tags ate are recognised by the PHP Parser.
<?php PHP code goes here ?>
<?PHP code goes here ?>
PHP code goes here
Most common tag is the <?php...?>
Question-4. What is the purpose of php.ini file ?
Answer- The PHP configuration file, php.ini, is the final and most immediate way to affect PHP's functionality. The php.ini file is read each time PHP is initialized. In other words, whenever httpd is restarted for the module version or with each script execution for the CGI version. If your change isn’t showing up, remember to stop and restart httpd. If it still isn’t showing up, use phpinfo() to check the path to php.ini.
Question-5. What is escaping to PHP ?
Answer- The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as 'escaping to PHP.'
Question-6. What do you mean by having PHP as whitespace insensitive ?
Answer- Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters). PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row. One whitespace character is the same as many such characters.
Question-7. Is PHP a case sensitive language ?
Answer- No, PHP is partially case sensitive.
Question-8. What are the characteristics of PHP variables ?
Answer- Here are the most important things to know about variables in PHP.
• All variables in PHP are denoted with a leading dollar sign ($).
• The value of a variable is the value of its most recent assignment.
• Variables are assigned with the = operator, with the variable on the left-hand side and the expression
to be evaluated on the right.
• Variables can, but do not need, to be declared before assignment.
• Variables in PHP do not have intrinsic types - a variable does not know in advance whether
it will be used to store a number or a string of characters.
• Variables used before they are assigned have default values.
• PHP does a good job of automatically converting types from one to another when necessary.
• PHP variables are Perl-like.
Question-9. What are the different types of PHP variables ?
Answer- PHP has a total of eight data types which we use to construct our variables −
• Integers − are whole numbers, without a decimal point, like 4195.
• Doubles − are floating-point numbers, like 3.14159 or 49.1.
• Booleans − have only two possible values either true or false.
• NULL − is a special type that only has one value: NULL.
• Strings − are sequences of characters, like 'PHP supports string operations.'
• Arrays − are named and indexed collections of other values.
• Objects − are instances of programmer-defined classes, which can package up both other kinds of
values and functions that are specific to the class.
• Resources − are special variables that hold references to resources external to PHP
(such as database connections).
Question-10. What are rules for naming a PHP variable ?
Answer- Rules for naming a variable are following −
• Variable names must begin with a letter or underscore character.
• A variable name can consist of numbers, letters, underscores but you cannot use characters like
+ , - , % , ( , ) . & , etc
Question-11. What are the rules for determine the "truth" of any value not already of the Boolean type ?
Answer- Here are the rules for determine the "truth" of any value not already of the Boolean type −
• If the value is a number, it is false if exactly equal to zero and true otherwise.
• If the value is a string, it is false if the string is empty (has zero characters) or is the string
"0", and is true otherwise.
• Values of type NULL are always false.
• If the value is an array, it is false if it contains no other values, and it is true otherwise.
For an object, containing a value means having a member variable that has been assigned a value.
• Valid resources are true (although some functions that return resources when they are successful
will return FALSE when unsuccessful).
• Don't use double as Booleans.
Question-12. What is NULL ?
Answer- NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −
$my_var = NULL;
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −
$my_var = null;
A variable that has been assigned NULL has the following properties:
It evaluates to FALSE in a Boolean context. It returns FALSE when tested with IsSet() function.
Question-13. How will you define a constant in PHP ?
Answer- To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $.
Question-14. What is the purpose of constant() function ?
Answer- As indicated by the name, this function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
Only scalar data (boolean, integer, float and string) can be contained in constants.
Question-15. What are the differences between PHP constants and variables ?
Answer-
• There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write
a dollar sign.
• Constants cannot be defined by simple assignment, they may only be defined using the define() function.
• Constants may be defined and accessed anywhere without regard to variable scoping rules.
• Once the Constants have been set, may not be redefined or undefined.
Question-16. What are PHP magic constants ?
Answer- PHP provides a large number of predefined constants to any script which it runs known as magic constants.
Question-17. What is the purpose of _LINE_ constant ?
Answer- _LINE_ − The current line number of the file.
Question-18. What is the purpose of _FILE_ constant ?
Answer- _FILE_ − The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, _FILE_ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
Question-19. What is the purpose of _FUNCTION_ constant ?
Answer- _FUNCTION_ − The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
Question-20. What is the purpose of _CLASS_ constant ?
Answer- _CLASS_ − The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
Question-21. What is the purpose of _METHOD_ constant ?
Answer- _METHOD_ − The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
Question-22. What is the purpose of break statement ?
Answer- break terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.
Question-23. What is the purpose of continue statement ?
Answer- continue causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Question-24. Explain the syntax for 'foreach' loop ?
Answer- The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.
foreach (array as value)
{
code to be executed;
}
Question-25. What is numeric array ?
Answer- Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
Question-26. What is associate array ?
Answer- Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
Question-27. What is Multidimensional array ?
Answer- Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.
Question-28. How will you concatenate two strings in PHP? ?
Answer- To concatenate two string variables together, use the dot (.) operator −
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
This will produce following result −
Hello World 1234
Question-29. How will you find the length of a string in PHP ?
Answer- The strlen() function is used to find the length of a string. Let's find the length of our string "Hello world!" −
echo strlen("Hello world!");
This will produce following result −
12
Question-30. How will you locate a string within a string in PHP ?
Answer- The strpos() function is used to search for a string or character within a string. If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string −
echo strpos("Hello world!","world");
This will produce following result −
6
Question-31. How will you get environment variables in PHP ?
Answer- PHP provides a function getenv() to access the value of all the environment variables.
Question-32. How will you get the browser's details using PHP ?
Answer- One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the user's browser and operating system.
Question-33. How will you generate random numbers using PHP ?
Answer- The PHP rand() function is used to generate a random number. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. This is achieved using the srand() function that specifies the seed number as its argument.
Question-34. What is the purpose $_PHP_SELF variable ?
Answer- The PHP default variable $_PHP_SELF is used for the PHP script name and when you click "submit" button then same PHP script will be called.
Question-35. How will you redirect a page using PHP ?
Answer- The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
Question-36. How can you display a file download dialog box using PHP ?
Answer- The HTTP header will be different from the actual header where we send Content-Type as text/html\n\n. In this case content type will be application/octet-stream and actual file name will be concatenated alongwith it. For example, if you want make a FileName file downloadable from a given link then its syntax will be as follows.
#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Actual File Content
open( FILE, "
Question-37. How will you get information sent via get method in PHP ?
Answer- The PHP provides $_GET associative array to access all the sent information using GET method.
Question-38. How will you get information sent via post method in PHP ?
Answer- The PHP provides $_POST associative array to access all the sent information using POST method.
Question-39. What is the purpose $_REQUEST variable ?
Answer- The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
Question-40. Which function will you use to create an array ?
Answer- array() − Creates an array.
Question-41. How can you sort an array ?
Answer- sort() − Sorts an array.
Question-42. What is the difference between single quoted string and double quoted string ?
Answer- Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
print "
";
$literally = "My $variable will print!\\n";
print($literally);
This will produce following result −
My $variable will not print!\n
My name will print
"; $literally = "My $variable will print!\\n"; print($literally);