Week 003 — The Bug That Taught Me PHP
One bug this week took longer to fix than it should have.
Week of October 14, 2024 debugging, learning, decision, ai
One bug this week took longer to fix than it should have.
Week of October 14, 2024 It left behind something more useful than a fix — an understanding of how PHP actually works.
Progress
Added UUIDs as item IDs and resized the database field to VARCHAR(36) to accommodate them. Added server-side image file type validation. Built the items list page — thumbnails, names, sorted alphabetically. Got delete working end-to-end with a confirmation step. Set up a VPN so development can happen away from the desk without losing access to the local environment.
The image thumbnail preview on the add-item page came from a Grok sample — working well enough out of the box, with some CSS cleanup needed afterward.
The image thumbnail preview on the add-item page came from a Grok sample — working well enough out of the box, with some CSS cleanup needed afterward.
The Hard Part
The delete functionality kept failing silently. The code looked correct, the logic was right, but nothing happened when delete was triggered. The eventual fix required looking at the Apache error log directly — tail /var/log/apache2/error.log — which finally revealed the actual problem.
The $log variable, used for logging throughout the codebase, was defined at the file level. Inside a function, PHP treats every variable as local unless explicitly told otherwise. The function couldn't see $log at all. The fix is one line inside the function: global $log;.
That's it. But PHP's default variable scoping behavior is genuinely different from what most languages do, and I hadn't properly internalized it yet. This was the lesson that made it stick.
Decision Made
Embedded each item's ID directly in its div element as an HTML attribute. This makes it trivially easy to identify which item triggered a delete or edit event without a secondary lookup, and lets DOM operations — like removing the div after a successful delete — happen without a full page reload. Clean enough to keep.
Learnings
Two things worth remembering from this week:
- When something is failing silently, check the error log. The answer is almost always there.
- PHP functions have local scope by default. Variables defined outside a function are not automatically visible inside it. Use
globalwhen you need them to be.
Next Week
Items can be created, listed, and deleted. Edit is the missing piece. Starting the refactor to turn the add-item page into a unified item page that handles all four operations from one place.
