Building Blockchain using PHP

building a blockchain in php


Building Blockchain using PHP

We are going for building Blockchain in PHP programming Language, Blockchain in itself is a very simple concept, it’s a very simple data structure,  cryptocurrencies are complicated but the Blockchain is not, the reason they are complicated is the consensus algorithm, their mining mechanism and the everything that is around but Blockchain in itself is very easy to understand, what you need to know before you can actually understand what Blockchain is and how it works, you need to know what hashes are? and how they work?

So a hash is basically a digital signature of some kind of data, for example I can take a movie and I can hash it and get a digital signature, I can take an email and I can hash it and get a digital signature, I can take a word and I can hash it, you can take whatever data and hash it and you get the digital signature for that data.

How this hash actually works? Lets dive into it.

We will Build Blockchain in PHP, it will be very simple and if you know programming you can do that in another language and if you don’t know programming, I think you still will be able to understand roughly how this works so let’s talk about hashes.

As you can see we have two lists we have one list that contains A,B,C and we have a second list that also contains A, B and C so this is the data, this is basically the strings we’re trying to hash so let’s see what kind of  digital signature we will get from list 1 & 2.


You can see as we get these two hash strings or digital signatures, these are identical for these two lists.

If I now change anything in list 1 for example –

Now I run it again


You see that I get a completely different digital signature so if I just make a small amendment I just meddle with the data, I try to change anything in this list, I will get a completely different digital signature and that is the foundation of a Blockchain Technology because a Blockchain is a list of blocks basically a chain of blocks and each block holds the digital signature of the previous block and the digital signature of the next block is based upon the digital signature of the current block so they are always together the next signature will be based on the current signature and the current signature is based on the previous signature so if you change anything back in the past you will break all of the signatures they will look completely different.

Learn More: What is Blockchain?

Let’s actually get into our Blockchain coding so the first thing we need to do is talk about our Block class, our Block in this case will be very simple, it will only contain three things it will contain the list of transactions that happened in that block, it will contain the previous hash or the digital signature of the previous block and it will also contain the hash of the current of itself and this hash will be based on the transactions and the previous hash so if anyone would change anything in the previous block the digital signature of the current block will change and that changes the digital signature of the next blocks.

Create an empty PHP project using your favorite IDE, I am using XAMPP for my localhost environment.

Now create a new file block.php, and put the following code snippet in it.

Now create another file called index.php and use block.php class to create your blocks.



See that the current hash will be based on the previous hash, if you change any of the previous transaction your all digital signatures will effect, for example I changed 11 to 12 in the genesisTransaction it will output completely different like:


You can see a change in any of these transactions, in any of the data in the one block will propagate and change all of the digital signatures in the future chains and that is how Blockchain works, because if I change something everyone else will see that my digital signatures are completely different from everyone else’s so that means that I have cheated, so if I just tell everyone that hey Person A  gave me this many bitcoins back then and here is the Blockchain that confirms that everyone will take a look at it and say well I will know this blockchain doesn’t confirm that because we have our own versions of the Blockchain and the digital signatures are completely different.

I hope you understood the basic concepts of hashing of linking blocks together via their digital signatures. Implement this example in PHP and let me know your thoughts in the comments section below.

I am a Sr Android App, Web Developer & Blockchain Project Manager having 5 plus years of extensive hands-on experience in Object Oriented Programming, Database Design and Implementation using Agile & UML. Pro in Yii2 PHP Framework, Android App Development, MYSQL, EOS & Etherium Blockchain & Smart Contract development.

1 Comment

1 Comment

  1. Pingback: - DevProfessor

Leave a Reply

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

More in Blockchain

Recent Posts


To Top