Blockchain 6 min read

Build a Simple PHP Blockchain from Scratch – Step‑by‑Step Guide

This tutorial explains the fundamentals of blockchain by defining block and blockchain classes in PHP, detailing fields, hash generation with SHA‑256, constructing genesis and subsequent blocks, and providing complete example code with a test run that demonstrates how each block links to its predecessor.

21CTO
21CTO
21CTO
Build a Simple PHP Blockchain from Scratch – Step‑by‑Step Guide

Introduction

Blockchain is a distributed ledger system that links blocks of transaction records using cryptographic hashes. To make the concept concrete for beginners, we implement a simple blockchain in PHP.

Block Definition

<?php
class Block {
    public $prevHash;
    public $hash;
    public $timeStamp;
    public $data;
}

Block Fields

prevHash : hash of the previous block

hash : hash of the current block

timeStamp : creation timestamp

data : stored transaction data

Hash Calculation

<?php
class Block {
    ...
    public function setBlockHash() {
        $data = serialize($this);
        $this->hash = hash('sha256', $data);
    }
}

Block Constructor

<?php
class Block {
    ...
    public function __construct($prevHash, $data) {
        $this->prevHash = $prevHash;
        $this->timeStamp = time();
        $this->data = $data;
        $this->setBlockHash();
    }
    public function getBlockHash() {
        return $this->hash;
    }
}

Blockchain Class

<?php
include('block.php');
class Blockchain {
    public $blocks = [];
    public function __construct() {
        $this->blocks[] = new Block('', 'Genesis Block');
    }
    public function addBlock($data) {
        $prevBlock = $this->blocks[count($this->blocks)-1];
        $this->blocks[] = new Block($prevBlock->getBlockHash(), $data);
    }
}

Testing the Implementation

<?php
include('blockchain.php');
$bc = new Blockchain();
$bc->addBlock('This is block1');
$bc->addBlock('This is block2');
foreach ($bc->blocks as $block) {
    printf("PrevHash: %s
", $block->prevHash);
    printf("Hash: %s
", $block->hash);
    printf("Data: %s
", $block->data);
    printf("
");
}

The output shows each block’s previous hash, current hash, and stored data, confirming that the chain links correctly.

Conclusion

This example demonstrates a minimal PHP blockchain. It lacks features such as distributed consensus, persistent storage, and security hardening, which will be addressed in future tutorials.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPTutorialcryptography
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.