Blog

PHP Blockchain

There is currently a lot of excitement around cryptocurrencies, with many making it rich and many more hoping to get rich quick. While speculating on cryptocurrencies is fun and (currently) profitable, cryptocurrencies are only part of the story of what makes this next technological innovation so ground-breaking.

At the heart of cryptocurrencies lies the concept of a blockchain, which, as the name implies, chains a number of blocks together, one after the other, with each block holding information whether transactions, smart contracts, data structures or anything else which needs to record something in an immutable way.

While revolutionary, the concept of the blockchain is quite simple; it is really just a linked list data structure. And it’s very easy to develop your own blockchain in pretty much any programming language you can think of.

There are already a number of tutorials on how to build blockchains in various languages and most cryptocurrency code is open sourced and available on Github, so simply browsing source is also a great way to learn how blockchains work. C, C++, Python and Java are the more popular languages for building blockchain technologies but there are also tutorials for Javascript, DotNet and others. Surprisingly, I was unable to turn up a PHP blockchain implementation so I thought I would develop a simple one myself with the aim of better understanding how the blockchain works. Using a blockchain tutorial for Javascript, I coded up a PHP-based blockchain and have made it publicly available on Github in the hope that others may also find it of interest.

Currently the implementation is very basic but it highlights the principles of a block chain; blocks can hold arbitrary data, are linked together via a blockchain and cannot be changed without corrupting the entire chain. I have also added basic mining to demonstrate the concept of proof-of-work.

Future developments on the PHP Blockchain roadmap might include:

  • Transactions
  • Wallets
  • Distributed architecture
  • Consensus

This is an open source project aimed at PHP programmers who wish to build a PHP-specific blockchain, so feel free to fork the code, make your own changes and contribute them back.

Some additional material for working with blockchains:

https://hackernoon.com/learn-blockchains-by-building-one-117428612f46
Python-focussed blockchain development with transaction and consensus coding examples.

https://brilliant.org/wiki/merkle-tree/
Information about merkle trees which form the basis of transaction management.

https://github.com/bitcoin/bitcoin/
The bitcoin code. While is C++ it is quite easy to follow the overall structure of a blockchain-based crypotcurrency.

Similar Posts

12 Comments

    1. Thanks for your interest. Yes, blockchain technology is an interesting topic and I am learning all the time.

      Next steps will be to introduce transactions into each block.

  1. This is fantastic. Thanks for the code. I have a question. I am still very new to blockchain. But where/how does P2P sharing of the blockchain happen? My understanding is what makes blockchain unique is there is no centralized location the chain is store and instead exact copies are shared P2P.

    Correct? How does this work?

    1. Thanks. A good question. I’m still working my way through the mechanisms of the blockchain. I have recently been working on UTXOs (unspent transaction outputs) which is a feature utilized in Bitcoin. I’m hoping I have a blog post explaining this concept in PHP soon. And, yes, you are correct; the distribution of nodes is one of the blockchain’s core strengths. Again, I’ll look at covering this in a future post, although I don’t have a lot of experience with developing P2P in PHP, so I’ll need some time to study up on the best way to approach this.

      Thanks for your support.

    1. Yes absolutely! That is something I’m working on at the moment. I’m looking to implement a UTXO as well. Thanks for the positive feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *