PHP Interview Questions
1
What is the correct and the most two common way to start and finish a PHP block of code?The two most common ways to start and finish a PHP script are:
<?php [ — PHP code—- ] ?> and <? [— PHP code —] ?>
2
Is PHP a case-sensitive language?PHP can be considered as a partial case-sensitive language. The variable names are completely case-sensitive but function names are not. Also, user-defined functions are not case-sensitive but the rest of the language is case-sensitive.
For example, user-defined functions in PHP can be defined in lowercase but later referred to in uppercase, and it would still function normally.
3
What are the three classes of errors that can occur in PHP?The three basic classes of errors are notices (non-critical), warnings (serious errors) and fatal errors (critical errors).
4
Is it possible to submit a form with a dedicated button?It is possible to use the document.form.submit() function to submit the form. For example: <input type=button value=”SUBMIT” onClick=”document.form.submit()”>
5
What is the purpose of @ in PHP?In PHP, @ is used for suppressing error messages. If any runtime error occurs on the line which consists @ symbol at the beginning, then the error will be handled by PHP.
6
Explain the importance of Parser in PHP?A PHP parser is software that converts source code into the code that computer can understand. This means whatever set of instructions we give in the form of PHP code is converted into a machine-readable format by the parser.
You can parse PHP code with PHP using the token_get_all() function.
7
What was the old name of PHP?
The old name of PHP was Personal Home Page.
8
How is it possible to remove escape characters from a string?
The strip slashes function enables us to remove the escape characters before apostrophes in a string.
9
What are the popular frameworks in PHP?
- Laravel
- CodeIgniter
- Symfony
- CakePHP
- Yii
- Zend Framework
- Phalcon
- FuelPHP
- PHPixie
- Slim
10
What is the use of header() function in PHP?
The header() function is used to send a raw HTTP header to a client. It must be called before sending the actual output. For example, you can't print any HTML element before using this function.
11
What does PEAR stand for?PEAR means “PHP Extension and Application Repository”. It extends PHP and provides a higher level of programming for web developers.
12
What are the variable-naming rules you should follow in PHP?There are two main rules that you have to follow when naming a variable in PHP. They are as follows:
- Variables can only begin with letters or underscores.
- Special characters such as +, %, -, &, etc. cannot be used.
13
What is NULL in PHP?NULL is a special data type in PHP used to denote the presence of only one value, NULL. You cannot assign any other value to it.
NULL is not case sensitive in PHP and can be declared in two ways as shown below:
$var = NULL:
Or
$var = null;
Both of the above syntaxes work fine in PHP.
14
How are constants defined in PHP?Constants can be defined easily in PHP by making use of the define() function. This function is used to define and pull out the values of the constants easily.
Constants, as the name suggests, cannot be changed after definition. They do not require the PHP syntax of starting with the conventional $ sign.
15
What is the use of the constant() function in PHP?The constant() function is used to retrieve the values predefined in a constant variable. It is used especially when you do not know the name of the variable.
16
What does $GLOBALS mean?$GLOBALS is an associative array including references to all variables which are currently defined in the global scope of the script.
17
When do sessions end?Sessions automatically end when the PHP script finishes executing but can be manually ended using the session_write_close().
18
What is the correct and the two most common ways to start and finish a PHP block of code ?The PHP code always starts with <?php and ends with ?>. The block of PHP code are:
<?php [ --Content of PHP code-- ] ?>
19
How to make single and multi-line comments in PHP ?Comments are used to prevent the execution of the statement. It is ignored by the compiler. In PHP, there are two types of comments: single-line comments and multi-line comments
- Single-line comment: The comment is started with double forward-slash (//).
- Multi-line comment: The comments are enclosed within /* comment section */
20
What is the use of count() function in PHP ?The count() function in PHP is used to count the number of elements present in the array. The function might return 0 for the variable that has been set to an empty array. Also for the variable which is not set the function returns 0.
21
What is PHP?PHP is a server-side scripting language commonly used for web applications. PHP has many frameworks and cms for creating websites. Even a nontechnical person can create sites using its CMS. WordPress,osCommerce are the famous CMS of PHP. It is also an object-oriented programming language like java, C-sharp, etc. It is very easy for learning
22
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.
Sample code:
$str = "PHP"; $$str = " Programming"; //declaring variable variables echo "$str ${$str}"; //It will print "PHP programming" echo "$PHP"; //It will print "Programming"
23
What are the features of PHP?PHP has some unique features, which include:
- Open-source
- Platform Independent
- Case sensitive
- Flexibility
- Efficiency
- Third-party application support and security
- Interpreted
24
What is required to use the image function?To use the image function, we will need the GD library.
25
Mention the popular Content Management Systems (CMS) in PHP?Following are some popular Content Management Systems (CMS):
- WordPress
- Shopify
- Drupal
- Joomla
- Wix
- Magento
26
What’s the difference between include and require?If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
27
How does “$message” differ from “$$message” in PHP?In PHP, the “$message” has a fixed name and value. It’s a regular PHP variable. On the other hand, the “$$message” is a reference variable. It holds data about the variable in question. The value in the “$$message” changes when the value in the main variable changes.
28
How can you pass a variable by reference?To be able to pass a variable by reference, we use an ampersand in front of it, as follows $var1 = &$var2
29
How do indexed arrays differ from associative arrays in PHP?The differences between indexed and associative arrays in PHP are as follows:
- Indexed arrays have their keys or indexes. However, each key in an associative array has its value.
- PHP automatically assigns value to indexes in an indexed array. An index of an array starts from “0”. Associative arrays need the manual assignment of keys, and these keys can hold even strings.
30
Explain whether PHP is a case-sensitive programming language.PHP is case-sensitive but not fully. Variable names in PHP are case-sensitive. However, PHP function names aren’t case-sensitive. Programmers can define a function name using lowercase, and they can call these functions using uppercase. The other aspects of PHP are case-sensitive.
31
How can we automatically escape incoming data?We have to enable the Magic quotes entry in the configuration file of PHP.
32
What does the “require_once” expression do in PHP?PHP developers use constructs like “include” and “require” to include files. The “require_once” expression has some similarities with “require”. However, this expression checks whether the specified file is already included. It doesn’t include the file if that’s already included.
33
What is the actually used PHP version?Version 7.1 or 7.2 is the recommended version of PHP.
34
How to redirect a page using PHP?The browser obtains raw HTTP headers from the PHP header() function which is used to redirect it to some other location. After calling the header() function, the exit() function is used to prevent the rest of the code from getting parsed.
35
What is the difference between include() and require() functions?In case of a problem in loading a file, the require () causes a fatal error stops the script's execution. In case of the same problem, the include() issues a warning but the execution of the script continues.
36
What is the use of final keyword?The final keyword prefixes the definition of a method with itself and prevents the method from being overriden by a child class. If a class is defined as final, it means that the class cannot be extended.
37
How can we determine whether a variable is set?The boolean function isset determines if a variable is set and is not NULL.
38
How can I display text with a PHP script?Two methods are possible:
<!--?php echo "Method 1"; print "Method 2"; ?-->
39
How is the comparison of objects done in PHP?We use the operator '==' to test is two objects are instanced from the same class and have same attributes and equal values. We can test if two objects are referring to the same instance of the same class by the use of the identity operator '==='.
40
Is it possible to remove the HTML tags from data?The strip_tags() function enables us to clean a string from the HTML tags.
41
How to initiate a session in PHP?The use of the function session_start() lets us activating a session.
42
Will a comparison of an integer 12 and a string "13" work in PHP?"13" and 12 can be compared in PHP since it casts everything to the integer type.
43
How is a constant defined in a PHP script?The define() directive lets us defining a constant as follows:
define ("ACONSTANT", 123);
44
Which cryptographic extension provide generation and verification of digital signatures?The PHP-OpenSSL extension provides several cryptographic operations including generation and verification of digital signatures.
45
How can we connect to a MySQL database from a PHP script?To be able to connect to a MySQL database, we must use mysqli_connect() function as follows:
<!--?php $database = mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($database,"DATABASE_NAME"); ?-->
46
How be the result set of Mysql handled in PHP?The result set can be handled using mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row.
47
Is it possible to use COM component in PHP?Yes, it's possible to integrate (Distributed) Component Object Model components ((D)COM) in PHP scripts which is provided as a framework.
48
What do the initials of PHP stand for?PHP means PHP: Hypertext Processor.
49
What is the default session time in PHP?The default session time in php is until the closing of the browser
50
Which programming language does PHP resemble?PHP syntax resembles Perl and C.