There are frequently asked PHP questions interview for fresher, candidates go through these question and test your knowledge.
1) What is PHP?
PHP stands for Hypertext Preprocessor. It is an open source server-side scripting language which is widely used for web development. It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.
2) What is PEAR in PHP?
PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries. It also provides a command line interface to install “packages” automatically.
3) Who is known as the father of PHP?
4) What was the old name of PHP?
Personal Home Page.
5) Explain the difference b/w static and dynamic websites?
In static websites, content can’t be changed after running the script. You can’t change anything in the site. It is predefined.
However, In dynamic websites, contents of the script can be changed at the runtime. Its content is regenerated every time a user visit or reload. Google, Yahoo, and every search engine is the example of the dynamic website.
6) What is the name of scripting engine in PHP?
The scripting engine that powers PHP is called Zend Engine 2.
7) Which programming language does PHP resemble?
PHP has borrowed its syntax from Perl and C.
8) What is “echo” in PHP?
PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, use of parentheses is required.
void echo ( string $arg1 [, string $… ] )
9) What is “print” in PHP?
PHP print output a string. It is a language construct not a function. So the use of parentheses is not required with the argument list. Unlike echo, it always returns 1.
int print ( string $arg)
10) What is the difference between “echo” and “print” in PHP?
Echo can output one or more string but print can only output one string and always returns 1.
Echo is faster than print because it does not return any value.
11) How a variable is declared in PHP?
PHP variable is a name of the memory location that holds data. It is a temporary storage.
12) What is the difference between $message and $$message?
$message stores variable data while $$message is used to store a variable of variables.
$message stores fixed data whereas the data stored in $$message may be changed dynamically.
13) What are the ways to define a constant in PHP?
PHP constants are name or identifier that can’t be changed during execution of the script. PHP constants are defined in two ways:
Using define() function
Using const() function
14) What are magic constants in PHP?
PHP magic constants are predefined constants which change on the basis of their use. They start with a double underscore (__) and end with a double underscore (__).
15) What are the different loops in PHP?
For, while, do-while and for each.
16) What is the use of count() function in PHP?
The PHP count() function is used to count total elements in the array, or something an object.
17) What is the use of header() function in PHP?
The header() function is used to send a raw HTTP header to a client. It must be called before sending the actual output. For example, you can’t print any HTML element before using this function.
18) What does isset() function?
The isset() function checks if the variable is defined and not null.
19) Explain PHP parameterized functions.
PHP parameterized functions are functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name. Output depends upon dynamic values passed as parameters into a function.
20) Explain PHP variable length argument function
PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in the function. To do this, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.
21) Explain PHP variable length argument function.
PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments.
22) What is the array in PHP?
Array is used to store multiple values in a single value. In PHP, it orders maps of pairs of keys and values. It stores the collection of a data type.
23) How to get the length of string?
The strlen() function is used to get the length of string.
24) What are the methods to submit form in PHP?
There are two methods GET and POST.
25) How can you submit a form without a submit button?
26) Differentiate between require and include?
Require and include both are used to include a file, but if a file is not found include sends warning whereas require sends Fatal error.
27) Explain setcookie() function in PHP?
PHP setcookie() function is used to set cookie with HTTP response. Once a cookie is set, you can access it by $_COOKIE superglobal variable.
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path
[, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
28) How can you retrieve a cookie value?
echo $_COOKIE [“user”];
29) What is the method to register a variable into a session?
30) What is $_SESSION in PHP?
PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
31) What is PHP session_start() and session_destroy() function?
PHP session_start() function is used to start the session. It starts a new or resumes the existing session. It returns the existing session if a session is created already. If a session is not available, it creates and returns new sessions.
32) What is the difference between session and cookie?
The main difference between session and cookie is that cookies are stored on user’s computer in the text file format while sessions are stored on the server side.
Cookies can’t hold multiple variables on the other hand Session can hold multiple variables.
You can manually set an expiry for a cookie, while session only remains active as long as the browser is open.
33) Write syntax to open a file in PHP?
PHP fopen() function is used to open file or URL and returns resource. It accepts two arguments: $filename and $mode.
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
34) How to write in a file in PHP?
PHP fwrite() and fputs() functions are used to write data into file. To write data into a file, you need to use w, r+, w+, x, x+, c or c+ mode.
35) How to delete a file in PHP?
The unlink() function is used to delete a file in PHP.
bool unlink (string $filename)
36) What is the method to execute a PHP script from the command line?
You should just run the PHP command line interface (CLI) and specify the file name of the script to be executed as follows.
37) How to upload a file in PHP?
The move_uploaded_file() function is used to upload file in PHP.
bool move_uploaded_file ( string $filename , string $destination )
38) How to download a file in PHP?
The readfile() function is used to download a file in PHP.
int readfile ( string $filename )
39) How can you send email in PHP?
The mail() function is used to send email in PHP.
40) How do you connect MySQL database with PHP?
There are two methods to connect MySQL database with PHP. Procedural and object-oriented style.
41) How to create a connection in PHP?
The mysqli_connect() function is used to create a connection in PHP.
resource mysqli_connect (server, username, password)
42) How to stop the execution of PHP script?
The exit() function is used to stop the execution of PHP script.
43) What are the encryption functions in PHP?
CRYPT() and MD5()
44) What is htaccess in PHP?
The .htaccess is a configuration file on Apache server. You can change configuration settings using directives in Apache configuration files like .htaccess and httpd.conf.
45) Explain PHP explode() function.
The PHP explode() function breaks a string into an array.
46) Explain PHP split() function.
The PHP split() function splits a string into an array by regular expression.
47) How can we get IP address of a client in PHP?