PHP Programming Course Overview
PHP Programming Training Course
PHP Interview Questions and Answers
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.
<? PHP code goes here ?>
Most common tag is the
4) 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.
5) 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.'
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.
6) Is PHP a case sensitive language?
No, PHP is partially case sensitive.
7) What are the characteristics of PHP variables?
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.
8) 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).
9) 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.
10) What are the rules for determine the "truth" of any value not already of the Boolean type?
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.
11) What is NULL?
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.
12) How will you define a constant in PHP?
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 $.
13) Is PHP a strongly typed language?
No. PHP is a weakly typed or loosely typed language.
This means PHP does not require to declare data types of the variable when you declare any variable like the other standard programming languages C# or Java. When you store any string value in a variable, then the data type is the string and if you store a numeric value in that same variable then the data type is an Integer.
14) What is meant by variable variables in PHP?
When the value of a variable is used as the name of the other variables then it is called variable variables. $$ is used to declare variable variables in PHP.
15) What are the differences between echo and print?
Answer: Both echo and print method print the output in the browser but there is a difference between these two methods.
echo does not return any value after printing the output and it works faster than the print method. print method is slower than the echo because it returns the boolean value after printing the output.
16) How can you execute PHP script from the command line?
You have to use PHP command in the command line to execute a PHP script. If the PHP file name is test.php then the following command is used to run the script from the command line.
17) How can you declare the array in PHP?
You can declare three types of arrays in PHP. They are numeric, associative and multidimensional arrays.
18) What are the uses of explode() and implode() functions?
explode() function is used to split a string into an array and implode() function is used to make a string by combining the array elements.
19) Which function can be used to exit from the script after displaying the error message?
You can use exit() or die() function to exit from the current script after displaying the error message.
20) Which function is used in PHP to check the data type of any variable?
gettype() function is used to check the data type of any variable.
21) How can you increase the maximum execution time of a script in PHP?
You need to change the value of the max_execution_time directive in the php.ini file for increasing the maximum execution time.
22) What is meant by ‘passing the variable by value and reference' in PHP?
When the variable is passed as value then it is called pass variable by value.
Here, the main variable remains unchanged even when the passed variable changes.
23) Explain type casting and type juggling.
The way by which PHP can assign a particular data type for any variable is called typecasting. The required type of variable is mentioned in the parenthesis before the variable.
24) How can you make a connection with MySQL server using PHP?
You have to provide MySQL hostname, username, and password to make a connection with the MySQL server in mysqli_connect() method or declaring database object of the mysqli class.
25) How can you retrieve data from the MySQL database using PHP?
Many functions are available in PHP to retrieve the data from the MySQL database.
26) What are the differences between mysqli_connect and mysqli_pconnect?
mysqli_pconnect() function is used for making a persistent connection with the database that does not terminate when the script ends.
mysqli_connect() function searches any existing persistence connection first and if no persistence connection exists, then it will create a new database connection and terminate the connection at the end of the script.
27) Which function is used in PHP to count the total number of rows returned by any query?
mysqli_num_rows() function is used to count the total number of rows returned by the query.
28) How can you create a session in PHP?
session_start() function is used in PHP to create a session.
29) What is the use of imagetypes() method?
image types() function returns the list of supported images of the installed PHP version. You can use this function to check if a particular image extension is supported by PHP or not.
30) Which function you can use in PHP to open a file for reading or writing or for both?
You can use fopen() function to read or write or for doing both in PHP.
31) What is the difference between include() and require()?
Both include() and require() function are used for including PHP script from one file to another file. But there is a difference between these functions.
If any error occurs at the time of including a file using include() function, then it continues the execution of the script after showing an error message. require() function stops the execution of a script by displaying an error message if an error occurs.
32) Which function is used in PHP to delete a file?
unlink() function is used in PHP to delete any file.
33) What is the use of strip_tags() method?
strip_tags() function is used to retrieve the string from a text by omitting HTML, XML and PHP tags. This function has one mandatory parameter and one optional parameter. The optional parameter is used to accept particular tags.
34) How can you send an HTTP header to the client in PHP?
The header() function is used to send raw HTTP header to a client before any output is sent.
35) Which functions are used to count the total number of array elements in PHP?
count() and sizeof() functions can be used to count the total number of array elements in PHP.
36) What is the difference between substr() and strstr()?
substr() function returns a part of the string based on the starting point and length. Length parameter is optional for this function and if it is omitted then the remaining part of the string from the starting point will be returned.
strstr() function searches the first occurrence of a string inside another string. The third parameter of this function is optional and it is used to retrieve the part of the string that appears before the first occurrence of the searching string.
37) How can you declare a constant variable in PHP?
define() function is used to declare a constant variable in PHP. Constant variable declares without the $ symbol.
38) Which function is used in PHP to search a particular value in an array?
in_array() function is used to search a particular value in an array.
39) What is the use of the $_REQUEST variable?
The $_REQUEST variable is used to read the data from the submitted HTML form.
40) What is the difference between for and Foreach loop in PHP?
For loop is mainly used for iterating a pre-defined number of times and Foreach loop is used for reading array elements or MySQL result set where the number of iteration can be unknown.--------------------------------------------------------------------------------------------------------------