Tutorial Study Image

PHP Interview Questions

PHP is a widely-used, open-source scripting language. Scripts are executed on the server.PHP is used to develop dynamic and interactive websites.
 

Hypertext Preprocessor

Everyone writes the code differently with different variables, comments are written in the PHP code to understand the code that is written by the programmer. Comments can remind you of what you were thinking when you wrote the code.

We can add both single and multiple comments in PHP, to add a single comment in the PHP we need to include   “#” or “//”
In Front of the line, whichever line is written after these any of the 2 symbols will be considered as a single-line comment in PHP.
Example
# hai  php
//hello php

To add  multiple comments  we use  /*  */, whatever is written inside this symbol will be considered as comments 

Example:

/* Welcome to Learn eTutorils */
 

Creating websites, Desktop Applications, rest API's

 Hypertext Preprocessor

PHP is a recursive short form of “PHP hypertext preprocessor”. It is a server-side scripting language that helps developers to create dynamic web pages. PHP supports almost all of the databases like Mysql, Oracle, etc. It helps to act as a bridge between the front end of a website (HTML) and the Backend of the website (Database)

PHP is a loosely typed programming language because; in PHP we do not need to define a variable. PHP will automatically convert the variable to its proper datatype with respect to its value.

PHP syntax closely resembles the programming language C or Perl.

1.    Can able to do system functions like open, read, create files
2.    Can able to connect and manipulate databases
3.    Power to access and set cookies
4.    Helps in encrypting data
5.    Helps in restricting users on a website
6.    Act as a bridge between front and backend of the website
 

For adding a PHP script inside a HTML page, we need to use a markup tag
1.    <?php     Code script          ?>
2.    <?          Code snippet        ?>
 

It is the PHP configuration file that affects PHP functionality. This configuration file is read at each time when PHP is initialized.

Now the using verison of php is version 7.1 or 7.2

While comparing with PHP 4, PHP 5 has many additional features that include OOP, which means Object Oriented Programming features.

“Rasmus Lerdorf” found PHP also called as the father of PHP.

In old times php stands for Personal Home Page.

PHP uses the scripting engine called Zend Engine version 2.

Mostly almost all content management systems will be supported by PHP but most popular CMS is
1.    WordPress
2.    Joomla
3.    Magento
4.    Drupal
 

1.    CakePHP
2.    CodeIgniter
3.    Yii 2
4.    Symfony
5.    Zend Framework

While doing PHP parsing, the Engine needs a way to separate the PHP code from the rest of the code in that page, which is called Escaping to PHP.

White space is the space or tab or any invisible thing we write in between the code. PHP is insensitive to the whitespaces that means it has no issues with the number of whitespaces. 

We can say that PHP is partially Case sensitive.

A static website means the content of each page is hardcoded that will not change. There will not be any database in the static website.
Dynamic website: In a dynamic website, all pages or some page's content will not be hardcoded which will be loaded dynamically from a database. PHP will help to load the content from the database (backend) to the website (frontend).
 

•    Scalar type declarations
•    Spaceship operator
•    Declaring return types
•    Null operator (??)
•    Anonymous classes
•    Group use declarations
•    Generator delegation
 

From the command-line interface, use the below line of code which has the script name to run the PHP file.
PHP scriptname.php
 

From the command line use the program with a –a
php –a
 

By using the PHP script we can able to generate the HTML code. Also, we can able to pass the values from HTML to PHP code.

PHP is a server-side language where JS is a client-side language. They cannot work or interact together, but they can able to pass values to PHP, and also PHP can able to generate some Javascript codes.

PEAR stands for PHP Extension and Application Repository that helps programmers by providing more snippets and libraries. It is a framework and contains many reusable components in PHP.

In PHP we have a special tag for displaying the output that are “<?” opening tag and “?>” is closing tag.

PHP doesn’t support multiple inheritances, but it will support single inheritance.

For using the inheritance from a class in PHP we can use the keyword “extended”.

Echo is used to display the output including string or multiple strings in PHP. It is a basic PHP language construct that doesn’t needs braces.

Echo is used to display the output in PHP that does not return any value.

Print is used to display the output in the like string, or characters, or whitespaces in PHP. Like echo, print does not need the braces.

Both are used for displaying output in PHP, also both do not need the braces. 

The difference is print return 1 always where echo does not return any value so the echo is faster than print. Secondly, echo can display multiple strings where print can display only one.
 

We can declare a variable in php using 
$variableName=value; 
(Variable is a name that holds a value in some memory location to use that value inside the program) 
 

PHP has 8 data types that are
1.    Integers
2.    Doubles: Floating point numbers
3.    Booleans
4.    Null
5.    Strings
6.    Arrays
7.    Objects: an instance of a PHP class
8.    Resources: for external references
 

•    Have a default value
•    Denoted by a $ sign
•    No need to declare before assigning
•    Are Perl like
•    Automatically convert the type
•    Do not have intrinsic types
 

1.    Start with an underscore or a letter
2.    Cannot able to use special characters in the variable name
 

If we need to assign a zero value or null value to a variable, we have to use NULL. Null is a special type in PHP, which is case insensitive.
Example: $var = NULL;  or  $var = null;
 

We can define a constant using the 
1.    define() function
2.    Const Keyword
in PHP. After defining we can use the constant value just by calling the name. 
 

constant() function is used to print a constant value in PHP. By using the constant function it returns the value of the constant.

Syntax: constant (name) 
Note: constant can only have scalar values like string, number Boolean, etc.
 

It is like the pointer concept in C. $data stores the value stored in variable data, where $$data will store the reference to the variable data. Means a variable of variables so the $$data value can be changed dynamically where the $data value is constant.

Magic constants are pre-defined constants in PHP, but their value can be changed within the program so they are called magic constants. 
The syntax for magic constants starts with double underscore and end with the same __magic const__
 

PHP has nine magic constants, they are 
•    __LINE__
•    __FILE__
•    __DIR__
•    __FUNCTION__
•    __CLASS__
•    __TRAIT__
•    __METHOD__
•    __NAMESPACE__
•    CLASSNAME ::class
 

Magic constants are processed in the compile-time rather than in the run time. Also, they are not case-sensitive.

Data types in PHP are of 8 different types which is used to hold different types of data. This 8 data types are further classified into 3 types which are
1.    Scalar
?    Boolean
?    Integer
?    Float
?    String
2.  Compound
?    Array
?    Object
?    Special 
?    Null
?    Resource
 

In PHP we can make a line into comment by using 2 methods
1.    // comment
2.    # comment
 

__LINE__ is used to return the current line number of the file.

__FILE__ will return the absolute path (full path) with the file name of the file where the magic constant is executed.

__FUNCTION__ returns the function name where the magic constant was declared and executed. This magic constant is case-sensitive. This magic constant is added after the PHP version 4.3.0.

__CLASS__ It is almost the same as the function as this constant returns the class name where this class is declared. It is also case-sensitive.

__METHOD__  constant returns the name of the method where this constant is declared. It is also case-sensitive.

We have to include a GD library for using the image function.

Imagetypes() function which is defined in the GD library is used for getting the image type and the type of images supported by the GD library.

This function in PHP is used for knowing the image size in PHP.

For getting the image width and height, we can use the functions
1.    Imagesx()
2.    Imagesy()
Respectively. 
 

Let us think of a situation if the require() function cant able to access a file it gives an error that will stop the program execution. But if we use the function include(), it gives a warning and does not stop the program execution.

1.    For loop
2.    While loop
3.    Do-while loop
4.    Foreach loop

count() function is used to count the number of elements in an array or something else we can count. 

header() function is used in dynamic pages where we need to send the header to the HTML. The header must be sent before real output.

isset() function in PHP is used for checking the variable is defined or not. It checks the variable is not null.

Parameterized functions are functions, which can able to pass any number of parameters into that function in PHP.
The parameters that we pass into the function will act as variables inside that function.
 

As in C or another programming language, Break in PHP is used to break the “for loop” or “switch case”. 

Continue is used to stop and continue the loop execution by skipping the rest of the loop, and start the loop again.

Foreach loop in PHP is used in the concept of the array. It will help to loop through array elements. In every iteration, the value of the array field is assigned to its variable. 

syntax
foreach (array as value)
{
    code to be executed;
}

 

 

Both functions will help to load the PHP file, but the require_once() function will check the file is already included or not before loading it.

We have to use the function print_r() to print the information about the variable in PHP

A variable-length argument means we can pass any number of variables into the function. 

The array is a compound data type in PHP where we can store multiple values that can be accessed by an index variable. It can be defined as a collection of values of a data type.

In PHP, there are three types of arrays, they are
1.    Indexed Arrays
2.    Associative Arrays
3.    Multidimensional Arrays
 

Indexed arrays are called numeric arrays which are normal arrays that can be accessed by an index variable (numeric value) and access the elements of an array in the linear model.

This type of array is indexed by string value and it cannot be accessed by the linear increment method. Each key has a specific value.

Multidimensional arrays are arrays of arrays. This means arrays that have the index of many arrays stored in it.

1.    Array()
2.    Array_change_key_case()
3.    Array_chunk()
4.    Count()
5.    Sort()
6.    Array_reverse()
7.    Array_search()
8.    Array_intersect()
Etc
 

Indexed arrays are indexed by numerical values and incremented by one, starting from 0. 
Whereas in the associative array each element has a string index that cannot be accessed in a linear method.
 

We can set the execution time using the php.ini configuration file. For setting the time-unlimited use set_time_limit(0) at the beginning to set the unlimited execution time. 

If a parse error is detected at any line of code, the PHP parser will get a stop and end the execution of the PHP program at that line of code.

file_get_contents() is a PHP function that helps to read a file and store that in a string.

Using mysqli_connect() function we can connect to mysql database from a php code.
<?php 
$db= mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($db,"DATABASE_NAME");
 ?>
 

Mysql_pconnect is used to connect the database but the difference is the connection is persistent which does not close even the PHP script ends. It is not supported in PHP version 7.0 or above.

In PHP, the functions 
1.    mysqli_fetch_array
2.    mysqli_fetch_assoc
3.    mysqli_fetch_object 
4.    mysqli_fetch_row
 

Using the function mysqli_num_rows() will return the number of rows in the result.

Both these functions are collect data where the mysqli_fetch_object() return only the first matching record where the mysqli_fetch_array() return all the matching records.

For finding the length of a string we have to use the strlen() function.

1.    Strolower()
2.    Stroupper()
3.    Ucfirst()
4.    Lcfirst()
5.    Ucwords()
6.    Strrev()
7.    Strlen()

To concatenate two strings we have to use the dot (.) operator.

In PHP, we have to use the function strops() to find a string or character inside another string.

Php has two methods to accept values from users or from the form that are 
1.    GET
2.    POST
 

We have two methods to include a file in PHP that are
1.    Include
2.    Require
 

Both are for file inclusion, but in include if the file is not found, it sends only a warning. But in the require it sends a fatal error.

Cookies are set in PHP by using the function setcookie(). After the setting, we can access the cookie by $_cookie.

A session is a logical object that is created by a PHP engine that stores the data from a user temporarily across subsequent requests from that user. It makes the transfer easy.

<?php  
Session_register($ur_session_var);  
?>
 

session_start() is a PHP function that is used to create a session for a user. It resumes or starts a new session when that function is called.

session_destroy() function in PHP will destroy the current session of a user if the user logs out or time out.

We have the function is_numeric() in PHP to check the value in a variable is a number or not.

ctype_alnum is a function in PHP that is used to check the given variable value is alphanumeric or not.

Use the function empty() to check the given variable is empty or not in PHP.

We have the function unlink() for delete the file in PHP.

The function unset() is used to make a variable undefined.

The function getenv() will help to retrieve the values of environment variables in PHP.

By using the HTTP_USER_AGENT will help to retrieve the user browser and OS. Need to remember that the environment variable should be set before using it.

We have the function rand() in PHP that will generate random numbers within the limit set by the user.

By using the PHP_SELF variable the same script is called. It is used while submitting the form.

Function fopen() in PHP is using dot open a file and it has two arguments to be passed. Filename and mode.
fopen("test.txt", "r");