PHP Interview Questions – Introduction

1. (Asked in Infosys) What is PHP?

Answer:
PHP stands for Hypertext Preprocessor. It is an open-source, server-side scripting language used mainly for developing dynamic websites and web applications.


2. (Asked in TCS) What are the main features of PHP?

Answer:
Key features include:

  • Easy to learn and use.
  • Open-source and cross-platform.
  • Supports databases like MySQL, PostgreSQL.
  • Provides built-in functions for handling forms, files, and sessions.
  • Offers excellent community support and extensive documentation.

3. (Asked in Cognizant) Explain server-side scripting and how PHP fits into it.

Answer:
Server-side scripting refers to scripts executed on a web server rather than the clientโ€™s browser. PHP scripts run on the server, process requests, interact with databases, and generate dynamic content, which is then sent back to the browser.


4. (Asked in Amazon) Is PHP a case-sensitive language? Give examples.

Answer:
PHP is partially case-sensitive. Variable names are case-sensitive, but function names are not.
Example:

$name = "John";
echo $NAME; // Error, variables are case-sensitive

function greet() { echo "Hello"; }
GREET(); // Works, functions are case-insensitive

5. (Asked in Wipro) Which type of websites can be built using PHP?

Answer:
PHP is versatile and can be used to build:

  • Dynamic and interactive websites
  • E-commerce platforms
  • Content Management Systems (e.g., WordPress, Joomla)
  • Social media sites
  • Blogs and forums

6. (Asked in Accenture) How can you embed PHP code within HTML? Give an example.

Answer:
PHP code is embedded within HTML using the <?php ?> tags.
Example:

<html>
<body>
  <?php echo "Hello, World!"; ?>
</body>
</html>

7. (Asked in Google) What are PHP variables, and how do you declare them?

Answer:
Variables store data that can be used later in the program. PHP variables start with a dollar sign $.
Example:

$age = 25;
$name = "Alex";

8. (Asked in IBM) What is the use of the echo statement in PHP?

Answer:
The echo statement outputs one or more strings to the browser. It is commonly used for displaying text, variables, or HTML content.
Example:

echo "Welcome to PHP!";
echo $name;

9. (Asked in Capgemini) Explain the difference between single and double quotes in PHP.

Answer:

  • Single quotes (') treat the content as a literal string without interpreting variables or special characters.
  • Double quotes (") interpret variables and special characters inside the string.
    Example:
$name = "John";
echo 'Hello $name'; // Output: Hello $name
echo "Hello $name"; // Output: Hello John

10. (Asked in Facebook) Can PHP run without a web server? Explain briefly.

Answer:
Yes. PHP scripts can be executed directly from the command line without a web server. This feature is useful for automated tasks, testing, or creating cron jobs.
Example:

php script.php