3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Block { public $nonce; public function __construct($index, $timestamp, $data, $previousHash = null) { $this->index = $index; $this->timestamp = $timestamp; $this->data = $data; $this->previousHash = $previousHash; $this->hash = $this->calculateHash(); $this->nonce = 0; } public function calculateHash() { return hash("sha256", $this->index.$this->previousHash.$this->timestamp.((string)$this->data).$this->nonce); } } class BlockChain { /** * Instantiates a new Blockchain. */ public function __construct() { $this->chain = [$this->createGenesisBlock()]; $this->difficulty = 4; } /** * Creates the genesis block. */ private function createGenesisBlock() { return new Block(0, strtotime("2017-01-01"), "Genesis Block"); } /** * Gets the last block of the chain. */ public function getLastBlock() { return $this->chain[count($this->chain)-1]; } /** * Pushes a new block onto the chain. */ public function push($block) { $block->previousHash = $this->getLastBlock()->hash; $this->mine($block); array_push($this->chain, $block); } /** * Mines a block. */ public function mine($block) { while (substr($block->hash, 0, $this->difficulty) !== str_repeat("0", $this->difficulty)) { $block->nonce++; $block->hash = $block->calculateHash(); } echo "Block mined: ".$block->hash."\n"; } /** * Validates the blockchain's integrity. True if the blockchain is valid, false otherwise. */ public function isValid() { for ($i = 1; $i < count($this->chain); $i++) { $currentBlock = $this->chain[$i]; $previousBlock = $this->chain[$i-1]; if ($currentBlock->hash != $currentBlock->calculateHash()) { return false; } if ($currentBlock->previousHash != $previousBlock->hash) { return false; } } return true; } } /* Set up a simple chain and mine two blocks. */ $testCoin = new BlockChain(); echo "mining block 1...\n"; $testCoin->push(new Block(1, strtotime("now"), "amount: 4")); echo "mining block 2...\n"; $testCoin->push(new Block(2, strtotime("now"), "amount: 10")); echo json_encode($testCoin, JSON_PRETTY_PRINT);
Output for git.master, git.master_jit
Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Deprecated: Creation of dynamic property BlockChain::$chain is deprecated in /in/iqUn3 on line 29 Deprecated: Creation of dynamic property BlockChain::$difficulty is deprecated in /in/iqUn3 on line 30 mining block 1... Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Block mined: 00008d618a2eac6e1624c7703245cf78750107f58039003bfd844515a1540d91 mining block 2... Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Block mined: 0000e2fb4e39130b839f5da622df7d76f70c8ef9d34d4012b7c078cabf07ab3e { "chain": [ { "nonce": 0, "index": 0, "timestamp": 1483225200, "data": "Genesis Block", "previousHash": null, "hash": "34054c41850bdf3f6f62dd60ae824dd35b98c872d420ed0dde942d0e54cd94d6" }, { "nonce": 185284, "index": 1, "timestamp": 1673950980, "data": "amount: 4", "previousHash": "34054c41850bdf3f6f62dd60ae824dd35b98c872d420ed0dde942d0e54cd94d6", "hash": "00008d618a2eac6e1624c7703245cf78750107f58039003bfd844515a1540d91" }, { "nonce": 99256, "index": 2, "timestamp": 1673950980, "data": "amount: 10", "previousHash": "00008d618a2eac6e1624c7703245cf78750107f58039003bfd844515a1540d91", "hash": "0000e2fb4e39130b839f5da622df7d76f70c8ef9d34d4012b7c078cabf07ab3e" } ], "difficulty": 4 }
Output for rfc.property-hooks
Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Deprecated: Creation of dynamic property BlockChain::$chain is deprecated in /in/iqUn3 on line 29 Deprecated: Creation of dynamic property BlockChain::$difficulty is deprecated in /in/iqUn3 on line 30 mining block 1... Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Block mined: 00000c6902da7a388f98997e655826203ce6f352bd55217a21e1e53dd01f5474 mining block 2... Deprecated: Creation of dynamic property Block::$index is deprecated in /in/iqUn3 on line 8 Deprecated: Creation of dynamic property Block::$timestamp is deprecated in /in/iqUn3 on line 9 Deprecated: Creation of dynamic property Block::$data is deprecated in /in/iqUn3 on line 10 Deprecated: Creation of dynamic property Block::$previousHash is deprecated in /in/iqUn3 on line 11 Deprecated: Creation of dynamic property Block::$hash is deprecated in /in/iqUn3 on line 12 Block mined: 0000321bdd6e23d84069d67260f2795c838006e992533c4bb9bbdab2d62515b8 { "chain": [ { "nonce": 0, "index": 0, "timestamp": 1483225200, "data": "Genesis Block", "previousHash": null, "hash": "34054c41850bdf3f6f62dd60ae824dd35b98c872d420ed0dde942d0e54cd94d6" }, { "nonce": 160545, "index": 1, "timestamp": 1673951076, "data": "amount: 4", "previousHash": "34054c41850bdf3f6f62dd60ae824dd35b98c872d420ed0dde942d0e54cd94d6", "hash": "00000c6902da7a388f98997e655826203ce6f352bd55217a21e1e53dd01f5474" }, { "nonce": 87547, "index": 2, "timestamp": 1673951076, "data": "amount: 10", "previousHash": "00000c6902da7a388f98997e655826203ce6f352bd55217a21e1e53dd01f5474", "hash": "0000321bdd6e23d84069d67260f2795c838006e992533c4bb9bbdab2d62515b8" } ], "difficulty": 4 }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
34.02 ms | 420 KiB | 5 Q