DEV Community

Cover image for The Basics of PHP for Web Development
MediaGeneous
MediaGeneous

Posted on

The Basics of PHP for Web Development

The Basics of PHP for Web Development

PHP (Hypertext Preprocessor) is one of the most widely used server-side scripting languages for web development. It powers millions of websites, including major platforms like WordPress, Facebook (initially), and Wikipedia. If you're just starting with web development or looking to expand your backend skills, learning PHP is a great choice.

In this guide, we'll cover the fundamentals of PHP, its syntax, key features, and how you can use it to build dynamic websites. And if you're looking to grow your YouTube channel while learning web development, consider checking out MediaGeneous for expert strategies.


Why Learn PHP?

PHP has been around since 1994 and remains relevant due to its:

  • Ease of Learning: Simple syntax, especially for beginners.

  • Server-Side Execution: Runs on the server, generating dynamic HTML for the client.

  • Database Integration: Works seamlessly with MySQL, PostgreSQL, and other databases.

  • Large Community & Frameworks: Supported by frameworks like Laravel, Symfony, and CodeIgniter.

  • Cost-Effective: Open-source and widely supported by hosting providers.


Setting Up PHP

To start coding in PHP, you need:

  1. A Local Server: Install XAMPP (for Windows, Linux, macOS) or MAMP (for macOS).

  2. A Code Editor: Use VS Code, PHPStorm, or Sublime Text.

  3. A Web Browser: Chrome or Firefox for testing.

Once installed, create a file named index.php in the htdocs (XAMPP) or www (MAMP) folder.


Basic PHP Syntax

PHP scripts start with <?php and end with ?>.

php

Copy

Download






<?php
    echo "Hello, World!";
?>
  • echo outputs text.

  • Every statement ends with a semicolon (;).

Variables & Data Types

PHP variables start with $.

php

Copy

Download






<?php
    $name = "John Doe";  // String
    $age = 25;           // Integer
    $price = 9.99;       // Float
    $is_active = true;   // Boolean
?>

Conditional Statements

Use if, else, and elseif for logic.

php

Copy

Download






<?php
    $score = 85;
    
    if ($score >= 90) {
        echo "A Grade";
    } elseif ($score >= 80) {
        echo "B Grade";
    } else {
        echo "C Grade";
    }
?>

Loops

PHP supports for, while, and foreach loops.

php

Copy

Download






<?php
    // For Loop
    for ($i = 1; $i <= 5; $i++) {
        echo "Number: $i <br>";
    }

    // Foreach Loop (for arrays)
    $colors = ["Red", "Green", "Blue"];
    foreach ($colors as $color) {
        echo "$color <br>";
    }
?>

Working with Forms

PHP is commonly used to process form data.

HTML Form

html

Copy

Download

Run






<form method="POST" action="process.php">
    <input type="text" name="username" placeholder="Enter Name">
    <input type="submit" value="Submit">
</form>

PHP Form Handling (process.php)

php

Copy

Download






<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $username = $_POST["username"];
        echo "Welcome, $username!";
    }
?>
  • $_POST captures form data sent via POST method.

  • Always sanitize inputs to prevent SQL injection (use mysqli_real_escape_string() or PDO).


Database Connectivity with MySQL

PHP integrates well with MySQL.

Connecting to MySQL

php

Copy

Download






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

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

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

Executing Queries

php

Copy

Download






<?php
    // Insert Data
    $sql = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";
    if ($conn->query($sql) === TRUE) {
        echo "Record inserted";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    // Fetch Data
    $result = $conn->query("SELECT * FROM users");
    while ($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"] . "<br>";
    }

    $conn->close(); // Close connection
?>

For better security, use prepared statements or an ORM like Eloquent.


PHP Frameworks for Faster Development

Instead of writing raw PHP, consider using frameworks:

  • Laravel – Best for modern web apps.

  • Symfony – Enterprise-grade PHP framework.

  • CodeIgniter – Lightweight and fast.

Example in Laravel:

php

Copy

Download






// Routes/web.php
Route::get('/', function () {
    return view('welcome');
});

Final Thoughts

PHP remains a powerful tool for web development, especially for dynamic websites and server-side applications. Whether you're building a simple blog or a complex e-commerce site, PHP provides the flexibility and performance needed.

If you're documenting your PHP learning journey on YouTube and want to grow your audience, MediaGeneous offers great strategies to boost your channel.

Now that you know the basics, dive deeper into PHP by exploring PHP.net (official documentation) and building your first project!

Happy coding! πŸš€

Dev Diairies image

User Feedback & The Pivot That Saved The Project β†ͺ️

We’re following the journey of a dev team building on the Stellar Network as they go from hackathon idea to funded startup, testing their product in the real world and adapting as they go.

Watch full video πŸŽ₯

Top comments (0)

πŸ‘‹ Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's dayβ€”leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay