Question-31. How will you get environment variables in PHP ?

Answer- PHP provides a function getenv() to access the value of all the environment variables.

Question-32. How will you get the browser’s details using PHP ?

Answer- One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the user’s browser and operating system.

Question-33. How will you generate random numbers using PHP ?

Answer- The PHP rand() function is used to generate a random number. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. This is achieved using the srand() function that specifies the seed number as its argument.

Question-34. What is the purpose $_PHP_SELF variable ?

Answer- The PHP default variable $_PHP_SELF is used for the PHP script name and when you click “submit” button then same PHP script will be called.

Question-35. How will you redirect a page using PHP ?

Answer- The PHP header() function supplies raw HTTP headers to the browser and can be used to 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.

Question-36. How can you display a file download dialog box using PHP ?

Answer- The HTTP header will be different from the actual header where we send Content-Type as text/html\n\n. In this case content type will be application/octet-stream and actual file name will be concatenated alongwith it. For example, if you want make a FileName file downloadable from a given link then its syntax will be as follows.

#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";
# Actual File Content
open( FILE, "<FileName" );
while(read(FILE, $buffer, 100) )
{
   print("$buffer");
}

 

Question-37. How will you get information sent via get method in PHP ?

Answer- The PHP provides $_GET associative array to access all the sent information using GET method.

Question-38. How will you get information sent via post method in PHP ?

Answer- The PHP provides $_POST associative array to access all the sent information using POST method.

Question-39. What is the purpose $_REQUEST variable ?

Answer- The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Question-40. Which function will you use to create an array ?

Answer- array() − Creates an array.