PHP Interview Questions Basic

1. Which of the following variable name is valid or invalid?

i) $5555abc ii) $variable-name

Answer: Both varibale names are invalid.

2. What will be the output of the following statement
print gettype("5")?

Answer: The output of above statement will be string.

3. "PHP does not require explicit variable declaration". Justify true or false.

Answer: True. PHP does not require explicit variable declaration, PHP automatically converts the variable to correct data type, depending on its value as and when they are declared in the program.

4. "@ is used to execute query". Justify true or false.

Answer: False. @ is used to execute query coma, it is false. Because PHP supports @ symbol as error control operator. When @ is fixed to an expression in PHP, any error messages that might be generated by that expression will be ignored.

5. "Whole PHP script is case sensitive". Justify true or false.

Answer: False. The whole page is not case sensitive. The names of user defined functions and classes as well as built in constants and keywords are not case sensitive. Variables on the other hand, are case sensitive. Therefore as only some part of the PHP code is case sensitive, the given statement is false.

6. Find output of the following PHP code?

<?php
sa="2B points in space" + 1;
echo "sa";
?>
Answer: There is an error in the program since variable sa is not having a dollar $ sign.

7. What is the role of web server and web browser?

Answer: Web Server is the computer which is responsible for accepting clients requests and serving the http responses.

Web Browser is an application program that provides a way to look at and interact with all information on the world wide web.

8. How do you define constant PI with a value of 3.142 in PHP?

Answer: To create a constant we use define() function.
<?php
define("PI",3.142);
echo PI;
?>
Note: To create a constant, use the define() function.

Syntax

define(name, value, case-insensitive)

Parameters:

name: Specifies the name of the constant value: Specifies the value of the constant case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

9. What is type casting?

Answer: Typecasting means converting One data type into another by writing the desired data type in parentheses before the variable which is to be cast.
Note:
<php
$var1 = 11; // integer variable
$var2 = (bool) $var1; // boolean variable
?>
The cast allowed are:
(int),(integer) - cast to integer
(bool),(boolean) - cast to Boolean
(float),(double),(real) - cast to float
(string) - cast string
(binary) - cast to binary string
(array) - cast to array
(object) - cast to object
(under) - cast to null

10. List any four web browsers names.

Answer: The web browser names are
  1. Chrome
  2. Firefox
  3. Internet Explorer
  4. Opera

11. What is the difference between echo() and print() functions?

Answer: echo() allows multiple values to be printed on the screen at a time while print() only send a one value to the browser and returns true or false depending on whether the value is successfully displayed or not.

12. "Keywords are case sensitive in PHP". Justified true or false.

Answer: False. Keywords are not case sensitive in PHP.

13. What is the difference between == and ===?

Answer: == is an equality operator which returns true if both operands are equal otherwise false. === is an identical operator which returns true if both operands are equal and are of the same type otherwise false.

14. State two features of PHP.

Answer: 1) PHP can be used with all major operating systems like Linux Microsoft windows etc.
2) PHP supports wide range of databases. 3) PHP supports most of the web service like Apache, wamp server etc.

15. Which operator is used to compare data as well as data type?

Answer: Identical operator === is used to compare data as well as data type.

16. What are the special types present in PHP?

Answer: Resource and null.

17. State whether the following variables are valid or invalid.

1) $_abc 2) 8x

Answer: 1) is valid and 2 is invalid.

18. What is the resource?

Answer: The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP. A common example of using the resource data type is a database call.

19. State any two bitwise operators in PHP.

Answer: The bitwise operators in PHP are
  1. Bitwise AND (&)
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. left Shift (<<)
  5. Right Shift (>>)
  6. Bitwise Negation (~)