Advertisement
KingAesthetic

PHP Example 1

Sep 27th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2.  
  3. // function that prints a random number from 1 - 50.
  4. function printRandomNumber() {
  5.     srand(time());
  6.     $randomNum = rand(1, 50);
  7.     echo "Random number: " . $randomNum . PHP_EOL;
  8. }
  9.  
  10. // function that calculates the area of a rectangle.
  11. function calculateRectangleArea($length, $width) {
  12.     return $length * $width;
  13. }
  14.  
  15. // function that applies a speed boost.
  16. function applySpeedBoost(&$speed) {
  17.     $speed *= 1.1; // I increased the speed by 10%
  18. }
  19.  
  20. // function that simulates health regeneration.
  21. function regenerateHealth(&$health, $maxHealth) {
  22.     // Add 5 health points (up to maxHealth)
  23.     $health = ($health + 5 <= $maxHealth) ? $health + 5 : $maxHealth;
  24. }
  25.  
  26. // function that calculates experience points.
  27. function calculateExperience($level) {
  28.     // Simple formula (you can customize this)
  29.     return $level * 100;
  30. }
  31.  
  32. // define structure for inventory items.
  33. class Item {
  34.     public $name;
  35.     public $quantity;
  36.  
  37.     public function __construct($name, $quantity) {
  38.         $this->name = $name;
  39.         $this->quantity = $quantity;
  40.     }
  41. }
  42.  
  43. // function that adds an item to a player's inventory.
  44. function addItemToInventory(&$inventory, $itemName, $quantity) {
  45.     $inventory[] = new Item($itemName, $quantity);
  46. }
  47.  
  48. // define the structure for loot items.
  49. class LootItem {
  50.     public $name;
  51.     public $dropChance; // chance of (0.0 to 1.0)
  52.  
  53.     public function __construct($name, $dropChance) {
  54.         $this->name = $name;
  55.         $this->dropChance = $dropChance;
  56.     }
  57. }
  58.  
  59. // function that simulates loot drops.
  60. function simulateLootDrop() {
  61.     srand(time());
  62.     $randomNum = (float)rand() / (float)getrandmax(); // between 0 and 1
  63.  
  64.     $lootTable = [
  65.         new LootItem("Health Potion", 0.3),
  66.         new LootItem("Gold Coin", 0.6),
  67.         new LootItem("Rare Sword", 0.1)
  68.     ];
  69.  
  70.     foreach ($lootTable as $item) {
  71.         if ($randomNum < $item->dropChance) {
  72.             echo "Loot dropped: " . $item->name . PHP_EOL;
  73.             break;
  74.         }
  75.     }
  76. }
  77.  
  78. printRandomNumber();
  79.  
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement