PHP Programs for Beginners: Simple to Advanced Examples

Rishabh parmarRishabh parmar
5 min read

If you’re new to web development, chances are you’ve already come across PHP. It’s one of the most widely used scripting languages for building dynamic websites and applications. PHP powers millions of websites, including big names like Facebook and WordPress. But to get comfortable with PHP, the best way is to learn through practice.

In this blog, we’ll walk through PHP programs for beginners, starting with the basics and moving toward slightly advanced examples. By the end, you’ll have a clear idea of how to use PHP to write simple scripts, interact with forms, connect with databases, and even handle user sessions.


Why Learn PHP?

Before diving into examples, let’s quickly understand why PHP is so popular:

  • Beginner-friendly – Easy to learn with simple syntax.

  • Server-side execution – Makes web pages interactive and dynamic.

  • Database support – Works seamlessly with MySQL, PostgreSQL, and others.

  • Open-source and free – Supported by a vast community.

  • Cross-platform – Runs on Windows, Linux, and macOS.

These benefits make PHP an excellent choice for beginners and developers alike.


Basic PHP Programs

Let’s start with some simple programs that every beginner should practice.

1. Hello World Program

The most common starting point in any programming language is printing "Hello World."

<?php
echo "Hello, World!";
?>

Explanation:

  • <?php ... ?> denotes the PHP code block.

  • echo is used to display output.


2. Simple Arithmetic Operations

PHP can handle arithmetic easily.

<?php
$a = 10;
$b = 5;

echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b);
?>

This program performs basic operations and displays results on the webpage.


3. Using Variables and Strings

<?php
$name = "Alice";
$age = 22;

echo "Hello, my name is $name and I am $age years old.";
?>

Explanation: PHP allows you to insert variables directly into strings using double quotes.


Intermediate PHP Programs

Once you’re comfortable with basics, try programs that involve loops, conditions, and functions.

4. Factorial of a Number

<?php
$num = 5;
$fact = 1;

for ($i = 1; $i <= $num; $i++) {
    $fact *= $i;
}

echo "Factorial of $num is $fact.";
?>

Logic: Multiply numbers from 1 to $num to calculate factorial.


5. Check Even or Odd

<?php
$number = 11;

if ($number % 2 == 0) {
    echo "$number is Even";
} else {
    echo "$number is Odd";
}
?>

This program demonstrates the use of conditionals in PHP.


6. Palindrome Check

<?php
$str = "level";
$rev = strrev($str);

if ($str == $rev) {
    echo "$str is a Palindrome.";
} else {
    echo "$str is not a Palindrome.";
}
?>

A palindrome is a string that reads the same forward and backward.


Advanced PHP Programs

Once you’ve mastered basic syntax and logic, it’s time to explore slightly advanced programs that show the real power of PHP in web development.

7. Handling HTML Form Input

<!-- form.html -->
<form method="post" action="welcome.php">
  Name: <input type="text" name="name">
  <input type="submit">
</form>
<!-- welcome.php -->
<?php
$name = $_POST['name'];
echo "Welcome, $name!";
?>

This program takes user input through a form and displays it on another page.


8. Connect to a MySQL Database

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>

Here, we establish a connection with a MySQL database. This forms the foundation of building dynamic websites.


9. Insert Data into Database

<?php
$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

This program adds data into a database table using SQL queries.


10. User Login Session

<?php
session_start();

$_SESSION['username'] = "Alice";
echo "Session started for " . $_SESSION['username'];
?>

Sessions allow PHP to store user information across multiple web pages, which is essential for login systems.


Tips for Practicing PHP Programs

  • Start small with basic print statements and variables.

  • Practice common programming problems like factorial, Fibonacci, and palindrome.

  • Gradually move to form handling and database operations.

  • Experiment with real-world projects like login forms, contact pages, or simple blogs.

  • Use an online compiler or a local server setup (XAMPP/WAMP) to test programs.


Conclusion

Learning PHP doesn’t have to be intimidating. By practicing simple scripts and gradually moving to advanced applications, you’ll develop the skills to build dynamic and interactive websites.

These PHP programs for beginners cover everything from printing text to handling user sessions and connecting with databases. Whether you are a student, hobbyist, or aspiring web developer, hands-on practice is the best way to strengthen your understanding of PHP.

And remember: consistency is key. The more you practice, the more confident you’ll become. Soon, you’ll move beyond basic exercises and start building full-fledged projects with ease.

Whether you’re exploring PHP for fun or preparing for a career in web development, these examples will set a strong foundation. With time, you’ll master not just these basics but also frameworks like Laravel and CodeIgniter, which take PHP to the next level.

0
Subscribe to my newsletter

Read articles from Rishabh parmar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishabh parmar
Rishabh parmar