PHP Interview Questions & Answers

  • Home
  • Interview_Question

Interview Questions & Answers

Select Series

How to include a file to a php page?

We can include a file using "include() " or "require()" function with file path as its parameter.

What is use of in_array() function in php ?

in_array used to checks if a value exists in an array

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.

How to find the length of a string?

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

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.

What is PHP?

PHP is a recursive acronym for "PHP: Hypertext Preprocessor". PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.

What are the common usage of PHP?

Common uses of PHP − PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them. PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user. You add, delete, modify elements within your database thru PHP. Access cookies variables and set cookies. Using PHP, you can restrict users to access some pages of your website. It can encrypt data.

What is the purpose of _FILE_ constant?

_FILE_ − The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, _FILE_ always contains an absolute path whereas in older versions it contained relative path under some circumstances.

How will you get the browser's details using PHP?

One of the environemnt variables set by PHP is HTTP_USER_AGENT which identifies the user's browser and operating system.

How can you display a file download dialog box using PHP?

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, "

How will you concatenate two strings?

To concatenate two string variables together, use the dot (.) operator. This will produce following result − Hello World 1234

How will you get cookies using PHP?

PHP provides many ways to access cookies. Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables.

How will you access the size of the uploaded file in PHP?

Using $_FILES['file']['size'] − it provides the size in bytes of the uploaded file.

How will you access the error code associated with file upload in PHP?

Using $_FILES['file']['error'] − it provides the error code associated with this file upload.

What is the purpose of $GLOBALS variable in PHP?

$GLOBALS − Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.

What is the purpose of $_SERVER variable in PHP?

$_SERVER − This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these. See next section for a complete list of all the SERVER variables.

What is the purpose of $php_errormsg variable in PHP?

$php_errormsg − $php_errormsg is a variable containing the text of the last error message generated by PHP.

How split() function works?

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

How will you retrieve formated string of trace in PHP when error occured?

Using getTraceAsString() method of Exception class which returns formated string of trace.

What is the purpose of getdate() function?

The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().