PHP Interview Questions

1. What is PHP?

The acronym of PHP is: “Hypertext Preprocessor”. PHP is a type of scripting language widely used as a backend language in web development for making different types of web pages (either static or dynamic). It is a server-side language that manages front-end files like HTML, CSS, java-script and the database. 

2. How is data fetch from the database in PHP?

Generally, in PHP, we use the function “mysql_fetch_assoc()” to fetch data from the database. PHP also provides another function in the array, i.e., “mysql_fetch_array()”.

3. What are the different rules for naming PHP variables?

Variables names are case-sensitive, so lower-case and upper-case both will consider as different. The rules for naming variables are: –

  • A variable can contain alpha-numeric characters and underscores.
  • A variable name starts with a ‘$’ symbol followed by its variable name.
  • A variable name should begin with either underscore or character only.
  • Blank spaces are not allowed.
  • A variable cannot start with a number.

4. What is the ‘GET’ and ‘POST’ methods in PHP?

GET and POST are the two most usual HTTP methods. The client’s browser can send data to the webserver. In PHP, these methods are used to retrieve information from forms, such as data input. To send data to the server, GET and POST METHODS are used. These methods are used for handling the data in forms. Where every method has minimal variation in the way they work. 

  • GET: It is the method to request data as URL parameters from the specified resource. Here the request is sent via the visible URL string. So, it can carry only the text data.
  • POST: It submits data that is to be processed to a specific resource. Here the data which is sent is not visible in the URL. It can carry text data as well as binary data. Hence there is no limitation in size.

5. How to connect database in PHP?

There are three ways to connect the MySQL database using PHP:

1. MySQLi 

2. PHP Data Objects (PDO).

3. MySQL

Here MySQL() is outmoded due to security issues such as SQL injection and all. But the other two are actively used.

6. What is the difference between the ‘GET’ and ‘POST’ Method?

GETPOST
-> Data is sent in a header.-> Data is sent in a body.
-> Limited amount of Data can be sent.-> Large amount of data can be sent.
-> It is more efficient.-> It is less efficient.
-> It is less secure but faster.->It is more secure but slower.

7. How to run PHP programs?

PHP programs are run as a Web Server module or a common line interface. The various Web Servers on which PHP programs are run like (WAMP- for windows) and (XAMP- for windows and Linux), and the data server like MySQL is to be installed. PHP programs can be written in any text editor such as Notepad++, Notepad, Dreamweaver, etc. These programs are to be saved with .php extension inside htdocs folder. PHP programs can run on various web browsers such as – Chrome, Internet explorer, Firefox etc.

8. How to upload an image at PHP?

Firstly a new directory called upload.PHP file is to be created, then the HTTP method ‘POST’ is called and attribute encrypt should be multi-part or form-data in HTML. After that, in the PHP file, image details are codded according to any web page requirements.

9. What is the purpose of the _CLASS_AND_METHOD_ constant?

  • To declare a class constant, the ‘const’ keyword is used. The purpose of the class constant is when constant data is to be declared or initialized.
  • In Method constant, there are two methods, i.e., GET and POST, which is used to request or submit any kind of data. There are also some limited and reserved names of the method, which begins with two underscores called Magic Methods.

10. What is overloading and overriding in PHP?

  •  Overloading:  Overloading is the ability to create multiple functions of the same name with different implementations, which is used to create properties and methods dynamically using the magic method function _call(). There are two types of overloading: property overloading and method overloading. Here more than one function can have a different number of arguments but the same method signature.
  • Overriding: In the overriding function, both parent and child classes should have some same function name arguments. Overriding is a method of updating the inherited method from parent class to child class. In this case, you will see how the parent class method’s behaviour changed when the child class override the parent class method.

11. How to install PHP?

Wamp server and XAMP are available packages and help install PHP, MySQL, and Apache Spark. To install PHP on windows, the following steps are to be followed:

  1. Go to the latest PHP package from the PHP website.
  2. Once the PHP zip file is downloaded, create a PHP folder in a c: drive.
  3. Then extract all the contents from the zip file to the folder.
  4. Rename ‘php.ini-development’ to ‘php.ini’ and make a copy of it.
  5. Change the path of the environment variable.
  6. Once we made a copy, we need to open ‘php.ini’ with a text editor’s help (Text editor can be a notepad, a notepad++ or Dream weaver) to make some changes.
  7. Enable all the essential extensions which are used while performing the majority of functions.
  8. Once the extensions are enabled, then open a control panel and search for ‘variable’.
  9. After that, click on ‘environment variables’, selecting ‘path’ from system variables.
  10.  After selecting the path, click on ‘edit’, then add a path, click on ‘new’ and then add ‘C:PHP7’.
  11. Once the path is added installation process gets completed.

12. How to create API in PHP?

API (Application Programming Interface) here, we will create a simple API (REST API) in PHP. REST API stands for Representational State Transfer, which defines constraints for developing and consuming web services through HTTP protocol. REST API is widely used in web applications and mobile applications compared to SOAP API as it provides output data in multiple formats such as JavaScript object notation (JSON), Command separated value (CSV), Extensible markup language (XML) and many others. To follow REST API, follow the following steps:

  1. Create database and table.
  2. Create a database connection.
  3. Create a REST API.

13. Name some of the functions in PHP?

A function is a block of code written in a program to perform some specific task. In PHP, there are over 700 built-in functions that perform different task. These built-in functions are the functions that are already present in the PHP installation package, which make PHP a very efficient and productive scripting language. These built-in functions are further categorized as numeric functions, string functions, data functions and user-defined functions. Here is some name of the functions: 

Cos(), sin(), tan(), is_number(), number_format(), sqrt(), srtr len(), str replace() etc.,

14. What is the PHP magic functions/ methods?

In PHP, we can define some special functions that will be called automatically. Such functions require no function call to execute the code inside their function. With these unique features, they can be referred to as ‘Magic functions or methods. For using these magic methods in the PHP project, one should know about the points listed below: 

  1. PHP magic matters are limited to some list of PHPs supported Keywords like construct destruct etc.
  2. These names are reserved; hence any function should not define on the names of the magic functions.
  3. It starts with a double underscore (__).
  4. PHP magic method must be defined inside the class.

 The following are some names of the magic functions:

  • __isset ()
  • __unset ()
  • __call ()
  • __clone ()
  • __invoke ()

15. How to redirect another page in PHP?

Redirection from one page to another in PHP can be achieved by using the following two ways:

  1. Using the header function: Header() is an inbuilt function used to send the raw HTTP header to the client using the header function.
  2. Using JavaScript via PHP: The window.location object in JavaScript is used to get the current page address (URL) and redirect the browser from one page to another.

16. What is a session in PHP?

A session is a way to store data in the form of variables to be used across multiple pages. PHP session is used to store and pass the information from one page to another temporarily. This technique is widely used in shopping websites where we need to store the items and pass the cart information. Php provides a unique id for each browser to recognize the user and avoid the conflict between multiple browsers. 

17. What is the difference between run time exception and compile time exception?

Compile time exceptionRun time exception
-> Compile time exception is also known as a checked exception-> Run time exception is also known as unchecked exception
-> They are propagated using throw keywords.-> They are propagated automatically.
->For errors, the programmer cannot directlyprevent from occurring.->For errors, the programmer can directly prevent them from occurring.
-> It occurs when the chances of failure are too high.-> It occurs mostly due to a programmer’s mistake.

18. How to send mail in PHP?

  • Using a mail() function in PHP, one can send a mail. First of all, create a PHP mail script file and access it using the domain name place the file in the public_html directory.
  • Now, to access the h panel, click on the file manager, click on the new file, and open it by double-clicking on it.
  • After that, use the mail() function. Now, one can send the mail by accessing the domain/name of the file with its extension from the browser.

19. How to run a PHP file in XAMPP?

Here are the steps to run PHP files in Xampp:

  1. Write a program in any text editor and save it with the .php extension (file.php).
  2. Start MySQL and Apache servers.
  3. Copy the save file (file.php) to htdocs, or you can also create a folder inside htdocs folder and save your codes.
  4. Now to run code, open local host/file.php then it gets executed.

20. How to get the current date in PHP?

To get the current date, we can use PHP inbuilt function. They are date() function or DateTime() class. Conversion of the time stamp stored in computer memory in a human-readable format is done using the date() function. The DataTime() class functions allow you to get the date and time.

21. How to check the PHP version? 

The easiest way to find out the PHP version used for that specific website is to use the phpinfo() function that prints various information about the PHP server, including its version.

22. How to get data from JSON array in PHP?

For handling the JSON array, PHP has some inbuilt functions. The most commonly used two functions are:

  1. Json_encode ():  The json_encode() function is used to encode a value to JSON format.
  2. Json_decode (): The json_decode() function is used to decode a json object into php object or an associative array. The json_decode() function return a default object. This function has a second parameter and when it is set true json objects are decoded into associative arrays.

23. How to convert an array into a string in PHP?

There are two ways to convert an array into a string in PHP.

  1. Using implode() function: These functions can convert array elements into a string. This function returns the string. The separate parameter in implode() function is optional.
  2. Using json() function: In PHP objects can be formed can be converted into json string by using json_encode ().

24. What is function overriding?

Overriding function occurs between the two classes, i.e., superclass and a subclass. Here inheritance is involved; hence child method hides that of the parent class method. The return value type and the parameter are the same. Access modifier should be the same or increased the scope of the access modifier. An excess modifier if the method contains the final keyboard in a parent class, then we cannot override, and if the method contains a static keyboard, then the child cannot override parent class methods but hide.

25. How to get a URL in PHP?

PHP provides a superglobal variable $_SERVER. It is a superglobal variable to get the URL of the current page, which means it is always available in all scope. There are many of these superglobals, but we commonly used $_SERVER superglobal. Many properties are accessible with an associative style index using $_SERVER superglobal variable. Some of the values we can access include:

  1. HTTP_USER_AGENT
  2. HTTP_HOST
  3. HTTP_ACCEPT_ENCODING
  4. HTTP_ACCEPT
  • With this variable, we will have to be two separate indices to get each part of the current page’s URL. The first part will be the host, localhost, and the second part will be the page name, home.
  • The first index we will use is HTTP_HOST- Is the current web address host. The second part is REQUEST_URI which will give us the part of the URL after the host.

26. How to insert data in MySQL using PHP?

  •  There are two methods that can be used to insert data in MySQL using PHP are MySQL and PDO. Here are the steps to insert data into a table:
  1. To connect the MySQL database by creating a new instance of PDO object.
  2. Construct a MySQL insert statement.
  3. Call exec () method of the PDO object.

27. How to install PHP in windows 10?

The following steps are to be performed to install PHP in windows 10:

  1. Go to link windows.php.net/download/
  2. Download the thread safe zip file.
  3. Go to C: Drive and create a new file and name it to suppose (PHP7).
  4. Open the zip file and drag all files into the new file we created recently (PHP7).
  5. Go to new folder (PHP7)-> Go to PHP.ini-development -> copy it and paste it in the same folder.
  6. Rename the copied file as PHP.ini, then right-click to it and open it with your text editor (notepad or notepad++)
  7. Go to the control panel, edit the system environment variable, and add the path C:\PHP7.

28. How to store form data into a database using PHP?

Below are the steps to store form data into a database using PHP:

  1. Start Xampp / Wamp server.
  2. Open local host/PHPMyAdmin in a web browser.
  3. Create database.
  4. Write HTML and PHP code in the text editor.
  5. Now submit data through HTML forms.
  6. Data will automatically get inserted into the database.

29. How to retrieve data from the database in PHP using MySQL?

To retrieve the data from the database, we can use two files:

  1. Database.php: to connect the database.
  2. Retrieve.php: to retrieve data from the database.

30. What are cookies in PHP?

A cookie is a small file with a maximum size of 4kb that the web server stores on the client computer. All page requests that follow return the cookie name and value once a cookie has been set. It has been issued that a cookie can only be read from the domain. Whenever the client sends a request to the server, a cookie is embedded with the request. The same way cookie can be received at the server-side means cookie created, sent, and received at the server end. A cookie created by the user can only be visible to them; other users cannot see its value.

31. How to connect Html form to MySQL database using PHP?

Follow the below-given steps to connect the Html form to the database using PHP:

  1. Create Html form.
  2. Grab data from Html forms and store it into PHP variables.
  3. Make a database connection.
  4. Run the query, and the data gets successfully inserted into the database.

32. What is namespace in PHP?

PHP namespaces are the way of encapsulating or summarizing items to reuse the same names without any kind of conflict. A namespace is a code block that is hierarchically labelled, holding a regular PHP code. A namespace is a qualifier that allows for the betterment of the organization by grouping classes that work together to perform a task. A namespace must be declared at the top of the file.

33. What is curl in PHP?

The Curl stands for ‘client for URL’. A curl is a command-line tool in PHP library that helps send files and download data over HTTP and FTP. The curl project is further classified into two parts: libcurl and curl. Curl works by sending a request to a website and this process includes the following four parts:

  1. Initialization
  2. Setting the option
  3. Execution with curl_exec ()
  4. Releasing the curl handle

34. How to get the last inserted id in PHP?

For retrieving last inserted data in php we can use PDO method as well as MySQL method.

Syntax in PDO: :lastInserted ID

Syntax in MySQL: $insert_id

35. What is isset() in PHP?

The isset() is an in-built function of PHP which helps to determine that the variable is set or not. It returns a Boolean value. It checks the variable is declared and not null. This function returns true if the variable is not null and false if the variable is null.

36. How to display an image from a database in PHP?

Steps are to be followed to display image from database in PHP:

  1. Create database in local host phpMyAdmin.
  2. Create table insert images and its path.
  3. Insert database connectivity code.
  4. Use select statement with the name of table.
  5. Pass query and complete code.
  6. Run output.

37. What are the common uses of PHP?

  • PHP can handle different forms that are gathering data from the file, save data to file, through email you can send data, return data to the user.
  • One can add, delete, and modify elements within the database through PHP.
  • It can encrypt data.
  • Through PHP one can restrict the user to access some pages of the website.
  • PHP is used for creating a dynamic website.
  • To Interact with database and web server PHP used.
  • Set cookies and access cookies variable.

38. What is PEAR in PHP?

PEAR stands for PHP extension an application repository. It is a structure and distribution system for reusable PHP components. It extends PHP and gives a higher level of programming for all web developers. PEAR is further divided into three different classes and those three classes are PEAR core components, PEAR packages and PECL packages.

39. What is the difference between ‘static website’ and ‘dynamic website’?

STATIC WEBSITEDYNAMIC WEBSITE
-> Content is fixed and each page is coded in plain HTML and CSS.-> Content is dynamic with changes according to users preference.
-> Updates can be tedious and prone to errors.-> Design updates can be much simpler.
-> Static web pages never use databases.-> Dynamic website uses databases to generate dynamic content through queries.
-> static web pages are easy to develop.-> Dynamic web page development requires programming skills.

40. Is PHP a sensitive language?

PHP is neither a case sensitive language nor an insensitive language. But the variable names in PHP are case sensitive, but function names are not case sensitive.

41. What are the characteristics of PHP variables?

The following are the characteristics of PHP variables:

  1. It starts with ‘$’ dollar sign.
  2. Its name must contain characters and underscores only.
  3. It cannot starts with number.
  4. Variables names are case sensitive.

42. What are different types of PHP variables?

PHP has many types of variables they are listed below:

  1. Integer: It includes whole numbers without a decimal point.
  2. Doubles: It includes all floating values.
  3. Booleans: It has only possible values that is true and false.
  4. Null: It is a special type that has only one value that is null.
  5. Strings: It includes a sequence of characters.
  6. Objects: Objects are instances of programmer defining classes that can package up both put other kinds of values and functions specific to the class.
  7. Resources: Resources are special variables that hold references to resources external to PHP.

43. What are the rules for determining the ‘truth’ of a value which is not already of the Boolean type?

The rules to determine the ‘truth’ of a value which is not already of the Boolean type they are: 

  1. If its value is equal to zero or anything, it is true, or if its values is a number, it is false.
  2. If the string is equal to zero or anything, it is true, or if its value is a string, it is false.
  3. Null type values are always false.
  4. Don’t use double as Booleans.
  5. Valid resources are true.
  6. If the array contains the value, it is false; if the array contains no value or anything else, it is true.

44. What is Null?

NULL is a special data type in PHP that contains only one value which is Null. is a variable that has no value assigned to it, then the variable of that data type is null. A variable can be empty by setting the value null to the variable. A variable that has been assigned the null value consist of the following properties:

  • It evaluates false to the Boolean context.
  • It returns false when it is tested with the isset() function.

45. What is the purpose of the constant() function?

The name constant in the constant() function indicates that this function will return a constant value. This function is used to retrieve the value of the constant when the name is unknown.

46. What is the purpose of the break and continue statement?

  •  Break: Break statement is used to play the loops or switch conditional statements and transfer the control to the next statement.
  • Continue: Continue statement is used to stop breaking the loop and transfer the control again to the condition.

47. How can you compare objects in PHP?

To compare objects in PHP, we use the “==” operator as it compares two objects if they both are from the same class and tests their attributes and equal to values we use the “===” operator to identify the instances that are referencing the same two objects of the same classes.

48. How can PHP and JavaScript interact?

PHP is a server-side language, and JavaScript is a client-side language so, both languages cannot interact directly. But JavaScript can send some variables back to PHP using URL, and PHP can generate some JavaScript code to be executed by the browser.

49. What are constructor and destructor in PHP?

PHP provides a special method to create and initialize a class object in a single step called constructor used to construct the object and destroy that object destructor method. Destructor is automatically called at the end of execution.

50. What are include() and require() function?

  • Include(): Include() function is to transfer data from one file to another file. It continues the execution and provides a warning If any error occurs.
  • Require(): Require() function easy to use to transfer the data of one PHP file to another file. It stops the execution if any error occurs with the fatal error on the screen.

51. Explain the syntax for ‘foreach loop’ with an example?

Foreach loop is used in an array to the value of the current array element in every pass it is assigned to $value, and the pointer is transferred to the next one.

Syntax:

foreach(array as value)
{
Code
}

Example:

$f_name = array (“smit”, “john”, “shrey”);
Foreach ($f_name as $value)
{
Echo “$value”;
}
?>

52. What is the difference between the ‘single quoted’ string and the “double quoted” string? 

  • Single quotes and double quotes are treated differently in PHP.
  • Parsed items are there within double quotes so, any string with double quotes, variable names are replaced with their values.
  • Items within single quotes are treated literally, so variable names within single quotes would be print out as names, not values.

53. What is the difference between ‘echo’ and ‘print’ in PHP?

ECHOPRINT
-> Echo is not written in parenthesis.-> Print can be written with parenthesis.
-> It can produce one or more than one strings.->It can produce one string at one time and returns 1.
-> Echo is faster than print.-> Print is slower than an echo.
-> Echo can pass multiple arguments.-> Print cannot pass multiple arguments.

To learn more about PHP in detail, check out PHP Tutorial to sharpen your XML skills.

Wondering where to learn the highly coveted in-demand skills for free? Check out the courses on Great Learning Academy.

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × three =