Introduction — PHP Technical Interview Questions & Answers

Introduction

Introduction

  • PHP is a language for web pages that run on the server. The browser asks for result.php. The server runs PHP, talks to a database if needed, and sends HTML back. You do not write PHP for the browser to compile like Java. You write a .php file the server executes.
  • Smallest file: <?php echo 'Hello PHP'; ?> Save as hello.php under the web root (htdocs in XAMPP). Open http://localhost/hello.php. If you see Hello PHP, install is working. If you see the source, Apache/PHP is not running or you opened the file as file://.
  • Variables start with $. $marks = 72; echo $marks; Strings in quotes. Every statement ends with ; . Forms send data to PHP as $_GET or $_POST. That is how login and result pages work.
  • Trap: calling PHP a database. MySQL is the database. PHP is the language that talks to it. Trap: using the old mysql_ functions — use mysqli or PDO with prepared statements.
Browser
    │  GET /hello.php
    ▼
  PHP on the server
    │  echo
    ▼
  HTML back to browser

Technical Interview Tip: Server language. echo Hello. $marks. PHP is not the database.

Code Examples for Introduction

Introduction — working example

<?php
// Introduction
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
  if ($s >= 80) echo "high: $s\n";
}

Introduction: scores array, print avg, then high marks. Say the output.

How to Explain Introduction to an Interviewer

What is PHP?

Server-side language Runs .php, sends HTML/JSON Not a database

Smallest example?

<?php echo 'Hello PHP'; ?> Open via localhost, not file://