PHP interview questions and answers Set – 2



In this tutorial we are providing new set of some frequently asked PHP Interview Questions which will help you to get success in interview session easily. Read previous set PHP interview questions and answers.

  1. What should we do to be able to export data into an Excel file?

    The most common and used way is to get data into a format supported by Excel. For example, it is possible to write xyz.csv file, to choose for example comma as separator between fields and then to open the file with Excel.

  2. What is the function file_get_contents() useful for?

    file_get_contents() lets reading a file and storing it in a string variable.

  3. Write a program using while loop
    $x = 1;
    
    while($x <= 5) {
        echo "The number is: $x";
        $x++;
    }
    
  4. 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 sub-string of string formed by splitting it on boundaries formed by the string delimiter.

  5. What is the difference between explode() and str_split() functions?

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

  6. 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 "uploaded in : " . "upload/" . $_FILES["file"]["name"];
    }
    
  7. How to create a text file in php?
    $filename = "/home/user/testing/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 );
    
  8. 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.

  9. 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.

  10. How to redirect a page in php?

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

  11. How can we access the data sent through the URL with the GET method?

    In order to access the data sent via the GET method, we you use $_GET array like this:www.someurl.com?var=value
    $variable = $_GET[“var”]; this will now contain “value”

  12. How can we access the data sent through the URL with the POST method?

    To access the data sent this way, you use the $_POST array.Imagine you have a form field called ‘var’ on the form, when the user clicks submit to the post form, you can then access the value like this:$_POST[“var”];

  13. How can we check the value of a given variable is a number?

    It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.

  14. How can we check the value of a given variable is alphanumeric?

    It is possible to use the dedicated function, ctype_alnum to check whether it is an alphanumeric value or not.

  15. How do I check if a given variable is empty?

    If we want to check whether a variable has a value or not, it is possible to use the empty() function.

  16. What does the unlink() function means?

    The unlink() function is dedicated for file system handling. It simply deletes the file given as entry.

  17. What does the unset() function means?

    The unset() function is dedicated for variable management. It will make a variable undefined.

  18. How do I escape data before storing it into the database?

    addslashes function enables us to escape data before storage into the database.

  19. How is it possible to remove escape characters from a string?

    The stripslashes function enables us to remove the escape characters before apostrophes in a string.

  20. How can we automatically escape incoming data?

    We have to enable the Magic quotes entry in the configuration file of PHP.

  21. What does the function get_magic_quotes_gpc() means?

    The function get_magic_quotes_gpc() tells us whether the magic quotes is switched on or no.

  22. Is it possible to remove the HTML tags from data?

    The strip_tags() function enables us to clean a string from the HTML tags.

  23. How stop the execution of a php scrip ?

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

  24. 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.

  25. How to find the length of a string?

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

  26. 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.

  27. What is the use of isset() in php?

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

  28. What is the difference between $var and $$var?

    They are both variables. But $var is a variable with a fixed name. $$var is a variable who’s name is stored in $var. For example, if $var contains “message”, $$var is the same as $message.

  29. What are the encryption techniques in PHP?

    MD5 PHP implements the MD5 hash algorithm using the md5 function,

    $encrypted_text = md5 ($msg);
    
  30. What is x+ mode in fopen() used for?

    Read/Write. Creates a new file. Returns FALSE and an error if file already exists.

  31. How to find the position of the first occurrence of a substring in a string?

    strpos() is used to find the position of the first occurrence of a substring in a string.

  32. 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.

Read PHP MySQL Interview Questions and Answers Set – 3