Top Php interview questions and their answer for fresher

Updated on ... 02nd May 2023

This PHP tutorial will really helpful for fresher to crack their interview who is stating interview and who have no work experience in PHP.

Question List:

  1. What is PHP?
  2. Who is the father of PHP?
  3. What is the difference between static and dynamic websites?
  4. How many different ways of fetching the result set of MySQL in PHP?
  5. How many types of arrays are in PHP?
  6. How to wrap all matched elements with another element with a class using jquery?
  7. What is the difference between GET and POST methods in PHP?
  8. What is the difference between PHP functions and methods?
  9. How to create a star triangle pattern using PHP loop?
  10. What does the enctype attribute in the form tag?
  11. What are some common security vulnerabilities in PHP, and how can you prevent them?



1. What is PHP 

PHP stands for PHP Hypertext Preprocessor (earlier it was called  Personal Home Page) is a widely used open-source server-side scripting language and free for web development and mobile APIs for managing dynamic content or information, session tracking, databases, etc.

By default, most of the server supports PHP so it contributes to cost-effectiveness. It supports many types of databases for example MySQL, ODBC, Solid, Oracle, Sybase, Solid PostgreSQL, etc PHP file extension is .php, 

PHP  was introduced in 1994 by Rasmus Lerdorf, so Rasmus Lerdorf is the father of PHP.

Very Basic Example of PHP


                                                    

                                                    

                                                    <?php

echo "Hello world";

?>

                                                    

                                                

2. Who is the father of PHP?

Rasmus Lerdorf is the father of PHP, who introduced PHP in 1994

3. What is the difference between static and dynamic websites?

Static Websites are written in HTML, JavaScript, CSS, etc. it is straightforward, and their content can't be changed dynamically after running the script.

In a static website, all things are predefined. when the server receives any request for a web page, the server sends the response to the users of the content which is predefined statically and every user receives and views the same content every time.

Dynamic websites are built in server-side language and technology like PHP, CGI, AJAX, ASP.NET, etc. Dynamic websites allow the Content displayed dynamically and it may be different for different users,

It takes more time to load because when the server receives any HTTP request then the program executes the code and produces HTML  and then sends the relevant response to the user.


How many different ways of fetching the result set of MySQL in PHP?

There are 4 different ways of fetching the result set of MySQL in PHP. They are mentioned below:

  1. mysqli_fetch_array(): It returns the result set, as an associative array, a numeric array, or both.
  2. mysqli_fetch_assoc(): It returns the result set, as an associative array only.
  3. mysqli_fetch_object(): It returns the result set, as an object.
  4. mysqli_fetch_row(): It returns the result row as an enumerated array


How many types of arrays are in PHP?

There are three types of arrays in PHP:

  1. Indexed array:  Indexed array is an array that is assigned by an index number automatically (Array index always starts at 0). 
  2. Associative array:  PHP associative arrays are arrays that use named keys that you assign to them.
  3. Multidimensional array: It is also known as an array of arrays, it can be a two-dimensional or three-dimensional, or n-dimensional array. 


How to wrap all matched elements with another element with a class using jquery?



                                                    

                                                    

                                                    $(document).ready(function() {
    $('table').wrap('<div class="table-responsive"></div>');
});

                                                    

                                                

You Should Also Read

Read more

What is the difference between GET and POST methods in PHP?

In PHP, the GET and POST methods are used to send data from a client to a server. Here are the differences between the two:

  • Data transmission: GET sends data via the URL, while POST sends data in the body of the HTTP request. With GET, the data is visible in the URL, while with POST, the data is not visible in the URL and is therefore more secure.
  • Data length: GET has a limit on the amount of data that can be sent (usually around 2048 characters), while POST has no such limit. Therefore, POST is suitable for sending large amounts of data, such as file uploads.
  • Caching: GET requests can be cached by the browser and intermediate servers, while POST requests are not cached. This means that if you make the same GET request multiple times, you may get a cached response, while with POST, you will always get a fresh response.
  • Security: POST is generally considered more secure than GET, as the data is not visible in the URL and is therefore less susceptible to interception or modification by third parties.

In general, GET requests are used when you want to retrieve data from the server, while POST requests are used when you want to send data to the server, such as when submitting a form or uploading a file. However, the choice between GET and POST ultimately depends on the specific requirements of your application.

What is the difference between PHP functions and methods?

In PHP, functions, and methods are both used to encapsulate code and perform specific tasks, but they have some fundamental differences.

  • A function is a standalone block of code that performs a specific task. Functions in PHP can be defined either within a class or outside of it. Functions can be called from anywhere in the code, and they can receive arguments and return a value.
  • A method, on the other hand, is a function that is defined inside a class and is designed to operate on the object's properties or data. In other words, a method is a function that is associated with a particular object. When you call a method, you are calling a function that is specific to the object's class. Methods can access and manipulate the object's data and can return a value just like functions.
  • Here are some of the key differences between functions and methods in PHP:
  • Scope: Functions can be defined outside of a class or inside a class, but they are not associated with any particular object. Methods, on the other hand, are defined inside a class and operate on object data.
  • Accessing data: Methods can access and modify object data, while functions cannot. Functions can only operate on data that is passed to them as arguments.
  • Visibility: Functions can be defined as public or private, while methods can be defined as public, private, or protected. Private methods can only be accessed by other methods within the same class, while protected methods can be accessed by other methods within the same class or within derived classes.

How to create a star triangle pattern using PHP loop?

Here's an example of how to print a star triangle using PHP:


                                                    

                                                    

                                                    <?php
//syntax:
do {
  code to be executed;
} while (condition is true);

//Example
$x = 1;

do {
  echo  $x ."<br>";
  $x++;
} while ($x <= 10);
?>

                                                    

                                                

The output of the above code will be:

What does the enctype attribute in the form tag?

The enctype attribute in an HTML form tag specifies how form data is encoded and sent when the form is submitted. By default, the enctype is "application/x-www-form-urlencoded", meaning that spaces and special characters are replaced with codes.

However, if you want to submit binary data such as images, audio, or video, you need to set the enctype attribute to "multipart/form-data". This encoding type is used for forms that have file uploads, and it allows the form data to be transmitted as a stream of binary data, along with information about the file being uploaded.

Example of how to specify the enctype attribute in an HTML form tag:


                                                    

                                                    

                                                    <form action="/submit-form" method="post" enctype="multipart/form-data">
  <!-- Form fields here -->
</form>

                                                    

                                                

What are some common security vulnerabilities in PHP, and how can you prevent them?

Developers need to be aware of several common security vulnerabilities in PHP and take steps to prevent them.

  1. SQL Injection: SQL injection occurs when a user submits malicious SQL code through a web form or URL parameter, which can then execute unauthorized database queries. To prevent SQL injection, use prepared statements or parameterized queries, which allow developers to separate SQL code from user input.
  2. Cross-site Scripting (XSS): This vulnerability occurs when attackers inject malicious scripts into web pages that are then executed by unsuspecting users. To prevent XSS, use input validation and output encoding to sanitize user input and escape special characters.
  3. Cross-site Request Forgery (CSRF): This vulnerability occurs when an attacker tricks a user into performing an action on a website without their knowledge or consent. To prevent CSRF, use CSRF tokens to validate requests and ensure that actions can only be performed by authorized users.
  4. File Upload Vulnerabilities: This vulnerability occurs when attackers upload malicious files to a server, which can then be executed or used to execute attacks. To prevent file upload vulnerabilities, restrict the types of files that can be uploaded, use file type validation and content scanning, and store uploaded files outside of the web root directory.
  5. Remote Code Execution (RCE): This vulnerability occurs when attackers are able to execute malicious code on a web server, which can then be used to gain unauthorized access or execute other attacks. To prevent RCE, keep software up to date, limit file permissions, and sanitize user input.

Overall, the best way to prevent security vulnerabilities in PHP is to follow secure coding practices and use security tools and techniques such as input validation, output encoding, prepared statements, parameterized queries, CSRF tokens, and file upload restrictions. It is also important to stay up to date with the latest security threats and vulnerabilities and to implement security best practices throughout the entire software development lifecycle.

Related Post

Leave a comment