DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Important PHP Interview Questions and Answers

Important PHP Interview Questions and Answers

If you're in the running for a position that requires knowledge of PHP, read on for great questions to quiz yourself on your PHP prowess!

Sudheer M user avatar by
Sudheer M
·
Oct. 26, 18 · Opinion
Like (2)
Save
Tweet
Share
8.22K Views

Join the DZone community and get the full member experience.

Join For Free

First Round: Basic PHP Interview Questions and Answers

Q1. Describe PHP

Ans: PHP is a server-side scripting language whose most common use is in web applications. PHP comes with various frameworks and CMSs that help create websites. Being object-oriented, PHP resembles languages such as Java and C#, which makes it very easy to learn and implement. A few of the popular applications built on PHP are WordPress and osCommerce.

Q2. Show What Use “echo” has in PHP

Ans: The main purpose of echo in PHP is to help print data in the webpage. For example, the following code prints the text for this item from the webpage: <?php echo 'Branded shirts'; ?>

Q3. What Is the Way in Which a File Is Included in a PHP Page?

Ans: Including a file to a PHP page is simple. All that we need to do is use the include() or require() function with the file path as its parameter.

Q4. How Does include Differ From require?

Ans: The main difference between include and require relates to file execution. What happens when require() does not find the file is that there will be a fatal error which stops the script from being executed. Whereas, when include() does not find a file, it issues a warning, but will not stop the execution, which can continue.

Q5. Describe the Difference Between require_once(), require(), and include().

Ans: The main difference between these is that while require() includes and assesses a particular file, require_once() does the same, but only if it has not been included before on the same page. Because of this, ideally, it is recommended to use require_once() when you want to include a file in which there are many functions. This is one way of ensuring that file is not included multiple times and to also avoid getting the “function re-declared” error.

Q6. What Are the Basic Differences Between GET and POST Methods?

Ans: These are the basic difference between GET and POST methods:

  1. In the GET method, it is possible to send only 1024 bytes, but with the POST method, we can transfer a larger amount of data.

  2. The GET method is relatively less secure than the POST method.

Q7. How Do You Declare an Array in PHP?

Ans: var $arr = array('brinjal', 'cucumber', 'carrot'); 

Q8. What Purpose Does ‘print’ Have in PHP?

Ans: Ironically, the ‘print’ function is not a real function in PHP. Rather, it is a language construct, which means it can be used without parentheses with its argument list.

Example: 

  • print('Personality Development'); 

  • print 'management test'; 

Q9. What Is the Purpose of in_array() function in PHP?

Ans: The in_array is meant for checking if an array contains a value.

Q10. Explain the Use of the count() Function in PHP

Ans: The use of count() is twofold: 1. To count all elements in an array; 2. To count something in an object.

Q11. In What Ways Do Include and Require Differ From Each Other?

Ans: These functions differ from each other mainly in the way they handle failures. When require() does not find the file, it will result in a fatal error which will halt the execution of the script. On the other hand, if include() does not find a file, it will send out a warning, but continues with the execution.

Q12. How Do You Differentiate Between Session and Cookie?

Ans: We can explain the differences between sessions and cookies in the following ways:

  1. While sessions are stored on the server, cookies are stored on the user's computers in text file format.

  2. While cookies cannot hold multiple variables, a session can.

  3. An expiration can be set for a cookie, because of which the session only remains active as long as the browser is open. Since data is stored in the server in session, it does not allow access to users. 

  4. Cookies are used for tracking user activities, while sessions are mainly used for login/logout.

Q13. How Do You Set Cookies in PHP?

Ans: Setcookie("sample", "ram", time()+3600); 

Q14. How Is a Cookie Value Retrieved?

Ans: echo $_COOKIE["user"]; 

Q15. How Is a Session Created? How Is a Value Set in Session? How Do You Remove Data From a Session?

Ans: 

  • Create a session: session_start();  

  • Set the value of a session: $_SESSION['USER_ID']=1;  

  • Remove data from a session: unset($_SESSION['USER_ID'];  

Q 16. What Is the Use of the explode() Function?

Ans: Syntax: array explode (string $delimiter, string $string [, int $limit ]);  

What this function does is that it breaks a string into an array. Each of the array elements is a substring of strings that is formed by splitting it on boundaries formed by the string delimiter.

Q17. Differentiate Between explode() and str_split() Functions

Ans: While the str_split function splits a string into an array using regular expressions; explode splits a string into an array.

Q18. Describe the Uses of the mysql_real_escape_string() Function?

Ans: The mysql_real_escape_string() function is used to escape special characters in a string for use in an SQL statement.

Q19. What Use Does the header() Function Have in PHP?  

Ans: The purpose of the header() function is to send a raw HTTP header to a client browser. Bear in mind that this function must be called before sending the actual output. For example, make sure you do not print any HTML element before using this function.

Q20. How Is a Page Redirected in PHP?

Ans: It can be done using the following code: header("Location:index.php");  

Q21. How Do You Stop the Execution of a PHP Script?

Ans: The execution of a PHP script can be stopped using the exit() function.

Q22. In a PHP-Based Site, How Is a Page Set Up as a Home Page?

Ans: In all PHP-based sites, index.php is the default name of the home page.

Q23. How Do You Find the Length of a String?

Ans: The length of a string can be found using the  strlen() function.

Q24. Describe the Use of rand() in PHP

Ans: rand() can be used to generate random numbers. If called without the arguments it returns a pseudo-random integer between 0 and getrandmax(). Let us say you want a random number between 5 and 15 (inclusive). In this case, you need to use rand(5, 15). Note that rand() cannot be used to generate cryptographically safe values, so using it for cryptography should be avoided. However, if a cryptographically secure value is what you are looking for, you could think of using openssl_random_pseudo_bytes() .

Q25. Describe the Use of isset() in PHP

Ans: The isset() function is used in PHP to determine if a variable is set and is not NULL.

Q26. How Do mysql_fetch_array() and mysql_fetch_assoc() Differ From Each Other?

Ans: While mysql_fetch_assoc function obtains a result row as an associative array; the mysql_fetch_array() fetches either an associative array, a numeric array, or both.

Q27. What Is an Associative Array?

Ans: Arrays that use string keys are called associative arrays.

Q28. For What Purpose is an “action” Attribute in an HTML Form Used?

Ans: The purpose of the action attribute is to determine where to send the form data in the form submission.

Q29. What Use Does the "enctype" Attribute Have in HTML Forms?

Ans: This attribute helps to understand the manner in which the form data should be encoded when submitting it to the server. enctype needs to be set as multipart/form-data when a form is being used for uploading files.

Q30. What Is a Constant?

Ans: Using the define() directive, like define ("MYCONSTANT",150)

Q31. Describe the Use of “ksort” in PHP

Ans: ksort is used to sort an array by key in reverse order.

Q32. What Is SQL Injection?

Ans: SQL injection is a malicious code injection technique that identifies and exploits SQL vulnerabilities in web applications.

Q33. Why Do You Need to Use the x+ Mode in fopen()?

Ans: It is used for the following: Read/Write. It creates a new file and returns FALSE and an error if the file already exists.

Q34. What Is the Way by Which the Position of the First Occurrence of a Substring in a String is Found?

Ans: The position of the first occurrence of a substring in a string is found using strpos().

PHP Session (web analytics) Data structure Strings Data Types Interview (journalism)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • Load Balancing Pattern
  • The Quest for REST
  • Better Performance and Security by Monitoring Logs, Metrics, and More

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: