Hello,
I’m encountering the following error in my PHP game code I am working on currently:
Here is the part of my code that seems to be causing the issue:
I believe it’s related to how I'm trying to access $npcs[$npcId], but I’m not sure why it’s showing as null on line 53. The $npcs array is populated earlier, so I'm not sure why it would be null when I try to access it.
Any help would be greatly appreciated! I believe I am overthinking the solution.
Thanks!
I’m encountering the following error in my PHP game code I am working on currently:
Notice: Trying to access array offset on value of type null in /public_html/rpg/game.php on line 53
Here is the part of my code that seems to be causing the issue:
PHP:
// Handle POST requests (attacking NPC, etc.)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(file_get_contents("php://input"), true);
if ($data['action'] === 'attack') {
// Randomly select an NPC from the map
$npcId = array_rand($npcs); // Random NPC selection by ID
if (isset($npcs[$npcId])) {
$npc = $npcs[$npcId];
I believe it’s related to how I'm trying to access $npcs[$npcId], but I’m not sure why it’s showing as null on line 53. The $npcs array is populated earlier, so I'm not sure why it would be null when I try to access it.
Any help would be greatly appreciated! I believe I am overthinking the solution.
Thanks!