PHP Interview Questions

Level: Freshers

Q 21 - How we can retrieve the data in the result set of MySQL using PHP?

  • 1. mysql_fetch_row
  • 2. mysql_fetch_array
  • 3. mysql_fetch_object
  • 4. mysql_fetch_assoc

Q 22 - What is the use of explode() function ?

Syntax : array explode ( string $delimiter , string $string [, int $limit ] );
This function breaks a string into an array. Each of the array elements is a substring of string formed by splitting it on boundaries formed by the string delimiter.

Q 23 - What is the difference between explode() and split() functions?

Split function splits string into array by regular expression. Explode splits a string into array by string.

Q 24 - What is the use of mysql_real_escape_string() function?

It is used to escapes special characters in a string for use in an SQL statement

Q 25 - Write down the code for save an uploaded file in php.

if ($_FILES["file"]["error"] == 0)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}

Q 26 - How to create a text file in php?

$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" ); exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );

Q 27 - What are the encryption techniques in PHP

MD5 PHP implements the MD5 hash algorithm using the md5 function,
eg : $encrypted_text = md5 ($msg);

mcrypt_encrypt :- string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] );
Encrypts plaintext with given parameters

Q 28 - What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?

  • Mysql_fetch_array Fetch a result row as an associative array, a numeric array, or both.
  • mysql_fetch_object ( resource result ) Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows
  • mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

Q 29 - What are the different types of errors in PHP ?

Here are three basic types of runtime errors in PHP:

  • 1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although you can change this default behavior.
  • 2. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
  • 3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

Q 30 - What are the different errors in PHP?

In PHP, there are three types of runtime errors, they are:

Warnings:
These are important errors. Example: When we try to include () file which is not available. These errors are showed to the user by default but they will not result in ending the script.
Notices:
These errors are non-critical and trivial errors that come across while executing the script in PHP. Example: trying to gain access the variable which is not defined. These errors are not showed to the users by default even if the default behavior is changed.
Fatal errors:
These are critical errors. Example: instantiating an object of a class which does not exist or a non-existent function is called. These errors results in termination of the script immediately and default behavior of PHP is shown to them when they take place. Twelve different error types are used to represent these variations internally.

Q 31 - How to strip whitespace (or other characters) from the beginning and end of a string ?

The trim() function removes whitespaces or other predefined characters from both sides of a string.

Q 32 - What is the use of header() function in php ?

The header() function sends a raw HTTP header to a client browser.Remember that this function must be called before sending the actual out put.For example, You do not print any HTML element before using this function.

Q 33 - How to redirect a page in php?

The following code can be used for it, header("Location:index.php");

Q 34 - How stop the execution of a php scrip ?

exit() function is used to stop the execution of a page

Q 35 - How to set a page as a home page in a php based site ?

index.php is the default name of the home page in php based sites

Q 36 - How to find the length of a string?

strlen() function used to find the length of a string

Q 37 - what is the use of rand() in php?

It is used to generate random numbers.If called without the arguments it returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 6 and 12 (inclusive), for example, use rand(6, 12).This function does not generate cryptographically safe values, and should not be used for cryptographic uses. If you want a cryptographically secure value, consider using openssl_random_pseudo_bytes() instead.

Q 38 - what is the use of isset() in php?

This function is used to determine if a variable is set and is not NULL

Q 39 - What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?

mysql_fetch_assoc function Fetch a result row as an associative array, While mysql_fetch_array() fetches an associative array, a numeric array, or both

Q 40 - What is mean by an associative array?

Associative arrays are arrays that use string keys is called associative arrays.