PHP

PHP Basics

PHP Data

Control Flow

PHP Functions

PHP Arrays

PHP Forms

PHP Advanced

Cookies & Sessions

Filters & JSON

PHP Exceptions

PHP OOP

MySQL with PHP

PHP and XML

AJAX with PHP

PHP

PHP Basics

PHP Data

Control Flow

PHP Functions

PHP Arrays

PHP Forms

PHP Advanced

Cookies & Sessions

Filters & JSON

PHP Exceptions

PHP OOP

MySQL with PHP

PHP and XML

AJAX with PHP

PHP Introduction


🔹 What is PHP?

PHP stands for Hypertext Preprocessor. It is a server-side scripting language used primarily for web development, but it can also be used as a general-purpose programming language.

PHP code runs on the server, and the result is sent to the browser as plain HTML.


🔹 Why Learn PHP?

  • Easy to learn and use

  • Runs on all major platforms (Windows, Linux, macOS)

  • Compatible with all major databases (MySQL, PostgreSQL, etc.)

  • Used in popular platforms like WordPress, Drupal, Magento

  • Open-source and well-supported by the community


🔹 What Can PHP Do?

✅ Generate dynamic page content
✅ Handle form data
✅ Create, open, read, write, delete files on the server
✅ Send and receive cookies
✅ Connect to databases
✅ Manage user sessions
✅ Encrypt data


🔹 PHP Syntax Example

<?php
echo "Hello, World!";
?>
🔸 Explanation:
 
  • PHP code is enclosed in <?php ... ?> tags

  • echo is used to output text

  • Statements end with a semicolon ;


🔹 How PHP Works

  1. Client requests a PHP file (e.g., index.php)

  2. Web server (e.g., Apache) processes the PHP code

  3. The server sends the output (usually HTML) to the client’s browser


🔹 PHP File Structure

  • File extension: .php

  • Can contain HTML + PHP code

Example:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>
<?php
echo "Hello from PHP!";
?>

</body>
</html>
 

Practice Questions

Q1. Write a PHP program to display your name.

Example Output:
My name is John.


Q2. Write a PHP program to add two numbers and display the result.

Example Output:
Sum: 15


Q3. Write a PHP program that uses variables to store a name and concatenate it with a welcome message.

Example Output:
Welcome, Alice


Q4. Write a PHP program to check if a person is eligible to vote using an if-else condition.

Condition: Age should be 18 or above.
Example Output:
Adult


Q5. Write a PHP program to store colors in an array and display them one by one using a foreach loop.

Example Output:

Red Green Blue



PHP Introduction Quiz

Q1: What does PHP stand for?

A. Personal Home Page
B. PHP: Hypertext Preprocessor
C. Private Hosting Page
D. Page Hosting Processor

Q2: Which tag is used to start PHP code?

A. <php>
B. <?php ... ?>
C. <script>
D. {php}

Q3: Which function is used to output in PHP?

A. echo()
B. print()
C. Both A and B
D. out()

Q4: Which of the following is used to get data from a form?

A. $_FORM
B. $_INPUT
C. $_POST / $_GET
D. $_DATA

Q5: Which of the following variables is valid in PHP?

A. $1name
B. $_name
C. @name
D. name$

Go Back Top