• 26 Jul, 2025

10 Easy PHP Scripts for Beginners to Practice & Learn

10 Easy PHP Scripts for Beginners to Practice & Learn

Learn PHP fast with these 10 beginner-friendly scripts! From contact forms to login systems, master PHP basics like form handling, sessions, and file uploads—no database required.

If you're just stepping into the world of PHP, congratulations—you're diving into one of the most versatile and widely-used server-side scripting languages on the web. PHP powers everything from simple contact forms to massive platforms like WordPress and Facebook. But as with any language, the best way to learn is by building.

To help you get started, we've compiled 10 simple PHP scripts every beginner should try. These projects will help you practice real-world logic, form handling, session management, file operations, and more.


1. Basic Contact Form with Validation

A contact form is often the first thing developers build—and for good reason. It introduces core concepts like form handling and input sanitization.

  • Key Concepts: $_POST, HTML forms, filter_var(), htmlspecialchars()
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars(trim($_POST["name"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format";
    } else {
        echo "Thanks, $name!";
    }
}
?>

This teaches session handling and form comparisons without diving into database management—yet.

  • Key Concepts: $_SESSION, conditionals, header()
<?php
session_start();
$correct_user = "admin";
$correct_pass = "password";

if ($_POST["username"] === $correct_user && $_POST["password"] === $correct_pass) {
    $_SESSION["loggedin"] = true;
    header("Location: dashboard.php");
} else {
    echo "Invalid credentials";
}
?>

3. Hit Counter

Every site needs analytics. Start simple with a flat file hit counter.

  • Key Concepts: File read/write, fopen(), fwrite()
<?php
$file = 'counter.txt';
$hits = file_exists($file) ? (int)file_get_contents($file) : 0;
$hits++;
file_put_contents($file, $hits);
echo "Visitor count: $hits";
?>

4. Random Password Generator

Learn about strings and randomness with this simple script.

  • Key Concepts: str_shuffle(), custom functions
<?php
function generatePassword($length = 10) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
    return substr(str_shuffle($chars), 0, $length);
}
echo generatePassword(12);
?>

5. Simple File Upload Script

Uploading files is a core web app feature. This teaches file handling and server storage.

  • Key Concepts: $_FILES, move_uploaded_file(), file security
<?php
if ($_FILES["file"]["error"] === UPLOAD_ERR_OK) {
    $uploadDir = "uploads/";
    $uploadFile = $uploadDir . basename($_FILES["file"]["name"]);
    move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFile);
    echo "File uploaded!";
}
?>

6. Form with Sticky Input Values

Preserve form values after submission.

  • Key Concepts: Sticky input, form state
<?php
$name = $_POST["name"] ?? '';
?>
<form method="POST">
  <input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>">
  <input type="submit">
</form>

7. To-Do List (Using Sessions)

A to-do app is a great way to learn about arrays, loops, and session storage.

  • Key Concepts: Arrays in $_SESSION, dynamic output
<?php
session_start();
$_SESSION["tasks"] = $_SESSION["tasks"] ?? [];

if (!empty($_POST["task"])) {
    $_SESSION["tasks"][] = htmlspecialchars($_POST["task"]);
}

foreach ($_SESSION["tasks"] as $task) {
    echo "<li>$task</li>";
}
?>

8. Simple Email Sender

Learn how to send emails from your server.

  • Key Concepts: mail(), HTML headers
<?php
$to = "example@example.com";
$subject = "Test Mail";
$message = "<h1>This is a test</h1>";
$headers = "Content-type:text/html\r\n";
mail($to, $subject, $message, $headers);
?>

⚠️ Note: Many shared hosts disable mail(). Consider PHPMailer for production.

9. Age Calculator

Introduce date logic and form input handling.

  • Key Concepts: DateTime class, diff() method
<?php
$dob = new DateTime($_POST["dob"]);
$today = new DateTime();
$age = $today->diff($dob)->y;
echo "You are $age years old.";
?>

10. Basic Calculator

Great for mastering conditionals, math operations, and form structure.

  • Key Concepts: Arithmetic, switch statements
<?php
$num1 = $_POST['num1'] ?? 0;
$num2 = $_POST['num2'] ?? 0;
$operator = $_POST['operator'] ?? '+';

switch ($operator) {
    case '+': $result = $num1 + $num2; break;
    case '-': $result = $num1 - $num2; break;
    case '*': $result = $num1 * $num2; break;
    case '/': $result = $num2 != 0 ? $num1 / $num2 : 'Error'; break;
}
echo "Result: $result";
?>

Conclusion

These simple scripts may appear basic, but they lay the groundwork for more advanced applications. Once you're comfortable, try integrating databases (like MySQL), using frameworks (like Laravel), or securing your apps against attacks (XSS, CSRF, SQL injection). Mastery starts with understanding the building blocks.

Remember: Practice is essential. Code each script yourself, tweak it, and break it. That’s where the real learning happens.

Y2A Post

Discover the innovative work in AI-generated blogs, seamlessly blending technology with creativity. This unique approach not only offers fresh perspectives on various topics but also ensures that content is engaging and relevant.