Most Essential And Frequently Asked Interview Questions And Answer
A session is a global variable which is used to store users/sensitive information on the server. Each session register a unique session id which is use to fetch stored value. Session store data as an associative array. Once a session variable is created then it is used across multiple page. By default, session variables last until the user closes the browser or leave the site otherwise most of the server terminate the session variable if you are not active more than 24 minutes on the site.View Detials
mysqli_fetch_assoc() function fetch a result row as an associative array where as mysqli_fetch_array() function fetch a result row as an associative array, a numeric array, or both.View Detials
mysqli_fetch_object() fetch a result row as an object where as mysqli_fetch_array() fetch a result row as an associative array, a numeric array, or both.View Detials
There is a function session_id() in php which is use to get and/or set the current session id.
string session_id ([ string $id ] )
for getting session id in php we have to use session_id() function without parameter.
if we want to set session id then we need to pass a parameter inside session_id() function. For this purpose we needs to be called session_id() function before session_start()
implode() function is use to convert an array into a string where as explode() function is used to convert an string into an arrayView Detials
There are 4 ways:
Duplicate value can remove from an array by using array_unique function. If two or more array values are the same, the first appearance will be kept and the other will be removed. It takes an input array and returns a new array without duplicate values.
Both php.ini and .htaccess file is configuration file.
It is a configuration file for php setting. It is a special file by which you can make changes in PHP settings. php.ini file provide many setting such as changing of the default max_execution_time = 30, error_reporting = E_ALL & ~E_NOTICE, file_uploads = [on/off] etc.
It is configuration file for server setting. With the help of .htaccess file you can make changes in server settings. It is supported by several web servers, including the popular Apache software which is used by most commercial web hosting providers. These files were first used to control user access on per-directory basis. It is used to control a couple of things such as error handling, enabling ssl, ip blocking and ip whitelisting, url redirect and url rewriting etc.
Bellow code is use for the same
$date1 = date(‘Y-m-d’); $date2 = ‘2006-08-15’; $days = (strtotime($date1) – strtotime($date2)) / (60 * 60 * 24); echo $days;
For increaseing the speed of a query we have to do couple of things
Predefined classes in PHP are:
strstr() and stristr() both are use to find first occurence of a string, but the differences are strstr( ) is case sensitive where as stristr( ) is case insensitive.View Detials
Bellow code is use for the same
$sql = "SELECT * FROM table1"; $result = mysql_query($sql, $db_link); $num_rows = mysql_num_rows($result); echo $num_rows;
Cookie is an array of data stored by the browser on client location and sent to the server with every request. It store limited amount of data about 4kb(i.e.: 4096 bytes). It is less secure than session because it present in client's computer.
Session is an array of data stored on the server and associated with a given user (usually via a cookie containing an id code). It store unlimited amount of data. It is more secure because of stored on the server.View Detials
By changing the value of max_execution_time in our php.ini file
max_execution_time = 30; // in second
By default, session variables last until the user closes the browser or leave the site otherwise most of the server terminate the session variable if you are not active more than1440 sec or 24 minutes on the site.
File's existence can be confirmed using file_exist() function which takes file name as an argument.
The default maximum size of a file that can be uploaded using PHP is 2MB.
upload_max_filesize = 2M;
and we can change the maximum size by changing the value of upload_max_filesize in our php.ini file.
upload_max_filesize = 5M;
There are two ways to find number of elements in an array:
1. sizeof($arr), This function is an alias of count()
There are several ways to concatenate two strings in PHP.
Use the concatenation operator [.] and [.=]
There are two string operators which is use for concatenation.
[.] : Concatenation operator | Used to combine two string values to create a new string.
[.=] : Concatenation assignment operator | Appends the argument on the right side to the argument on the left side.
<?php $arr=array("0"=>"Even","1"=>"Odd"); $check=13; echo "Your number is: ".$arr[$check%2]; ?>
The three different way in PHP to connect with MySQL is:
Note: We should use MySQLI because MySQLI is imporoved version of MySQL and have more function then MySQL.
Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.
$name and $$name both are variables but with a single difference that is $name is just a normal php variable where as $$name is known as reference variable. It allow you to use $name variable's value as a variable for getting $$name variable value.View Detials
In PHP all functions start with __ names are magical functions. Magical functions always lives in a PHP class.The definition of magical function are defined by programmer itself.
Here are list of magic functions available in PHP
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo().
PHP mainly support two type of inheritance:
The unlink() and unset() function in php have same functionality (i.e.: delete) but the difference is in the type of data on which they are applied to perform such delete functionalities.
unlink() used to delete files from directory where as unset() is used to unset or delete the variable.View Detials
<?php $background_color = ($num == 0 ? 'blue' : ($num > 0 ? 'green' : 'red')); ?>
Here we are assigning different strings to the $background_color variable based on a numeric value ($num).
The two operators known as comparison operator. "==" (i.e. equal) and === (i.e. identical) both are use to check values are same or not. The difference between the two is that '==' should be used to check if the values of the two operands are equal or not whereas, '===' checks the values as well as the type of operands.View Detials
List is a language construct and is similar to an array. It is used to assign a list of variables in one operation. If you are using PHP 5, then the list values start from a right parameter, and if you are using PHP 7, then the list starts with left parameter.View Detials
By using php date() function we can find the current date and time.
<?php echo date(“d-m-Y”); //This will print today date. echo date (“d-m-Y h:i:s”) // This will print today’s date with current time. ?>
This operator is known as scope resolution operator. And this operator is use to access the static members of class. To access static members of class we do not need to create object/instance of class.View Detials
PHP echo and print both are used to display the output in PHP. Both can be used with or without parenthese. The differences are small
The "session.gc_maxlifetime" is use to modify the default timout of session.
server should keep session data for AT LEAST 1 hour by using the below code. It increase session default timeout by 1 hour.
<?php //3600 is use for 1 hour ini_set('session.gc_maxlifetime', 3600); ?>
By using exit() function we can stop the execution of php script.
<?php $a = 10; $b = 20; $c = $a+$b; exit(); echo $c; // In the above example execution will stop after addition it will not print variable c. ?>
Both require() and require_once() is use to include a file but the difference is required_once() function checks if the file already included or not where require() function does not check.
(same for include_once() and include())
There are two ways to enable error reporting in your PHP scripts:
1. You can add the following function in the the script:
2. You can add the following option in your website's php.ini file:
error_reporting = E_ALL
PHP header() function is use to redirect a page to another page. It supplies raw HTTP headers to the browser and redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
header(location: www.studyzone4u.com); // This will redirect to www.studyzone4u.com.
We can use PHP inbuilt function error_reporting(0) to avoid all errors in PHP file.
<?php error_reporting(0); ?>
Superglobal are out of scope limitation. These variables can be accessed from any function, class or any file without doing anything special such as declaring any global variable etc. These variables store information in an array. Commonly they are used to store and get information from one page to another in an application.
Below is the list of superglobal variables available in PHP:
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
Scopes define visibility of a variables. In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be used.
There are three scopes available in php
The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.
(same for require_once() and include_once())
By using gettype() function we can check the datatype of a variable.
we can also use var_dump() function for the same.View Detials
Most viewed posts
9 months ago 1071 views
Add number of days to a date in php
5 months ago 625 views
9 months ago 529 views
What is the use of header() function in php?
9 months ago 252 views
how to remove index.php in codeigniter
3 months ago 232 views
How to remove HTML tags form a string in PHP?
9 months ago 227 views
How to select dropdown item based on value
1 day ago 22 views
Get full url in php
2 weeks ago 50 views
Automatically change the html element size using css.
3 weeks ago 49 views
How to use php array with sql IN operator?
3 weeks ago 62 views
Shutdown a window system through RUN or Command Prompt
3 weeks ago 39 views
What do you mean by foreign key?
1 month ago 41 views
Today spacial posts