Php Interview Questions And Answers:
A list of top most asked Php interview questions and answers are given below
1.What is PHP?
PHP is a recursive acronym for “PHP: Hypertext Preprocessor”. 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.
2.What are the common usage of PHP?
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, modify elements within your database thru PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your website.
It can encrypt data.
3.In how many ways you can embed PHP code in an HTML page?
All PHP code must be included inside one of the three special markup tags ate are recognised by the PHP Parser.
4.Most common tag is the What is the purpose of php.ini file?
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.
6.What is escaping to PHP?
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.’
7.What do you mean by having PHP as whitespace insensitive?
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.
8.Is PHP a case sensitive language?
No, PHP is partially case sensitive.
9.What are the different types of PHP variables?
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).
10.What are rules for naming a PHP variable?
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
11.What are PHP magic constants?
PHP provides a large number of predefined constants to any script which it runs known as magic constants.
12.What is the purpose of _LINE_ constant?
_LINE_ − The current line number of the file.
13.What is the purpose of _FILE_ constant?
_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.
14.What is the purpose of _FUNCTION_ constant?
_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.
15.What is the purpose of _CLASS_ constant?
_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.
16.What is the purpose of _METHOD_ constant?
_METHOD_ − The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
17.What is the purpose of break statement?
break terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.
18.What is the purpose of continue statement?
continue causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
19.Explain the syntax for ‘foreach’ loop.
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.
20.What is the purpose of $GLOBALS variable in PHP?
$GLOBALS − Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.
21.What is the purpose of $_SERVER variable in PHP?
$_SERVER − This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these. See next section for a complete list of all the SERVER variables.
22.What is the purpose of $_COOKIE variable in PHP?
$_COOKIE − An associative array of variables passed to the current script via HTTP cookies.
23.What is the purpose of $_SESSION variable in PHP?
$_SESSION − An associative array containing session variables available to the current script.
24.What is the purpose of $_PHP_SELF variable in PHP?
$_PHP_SELF − A string containing PHP script file name in which it is called.
25.What is the purpose of $php_errormsg variable in PHP?
$php_errormsg − $php_errormsg is a variable containing the text of the last error message generated by PHP.
26.How ereg() function works?
ereg() − The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.
27.How eregi() function works?
eregi() − The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive.
28.How split() function works?
The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.
29.How preg_match() function works?
preg_match() – The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.
30.How preg_split() function works?
The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.