Introduction to Event-Based Programming

Learn about event-based programming and click events.

Last chapter we were introduced to the DOMDocument Object Model and how it allows us to interact with the elements of a web page. Now, we’ll be looking at how events provide the link between user interactions and our program. That’s right—this section is where our code starts to become fully interactive and the fun really begins.

Event-based programming

Imagine boiling some water in a pan. The only way to tell if the water has boiled is to keep checking at regular intervals. This is annoying, because we have to keep stopping what we’re doing to check, and there’s also the chance that we might miss the point when the water boils and it will boil over, making a mess.

The better approach is to use a kettle that whistles when the water boils. This means we can go off and do something else, safe in the knowledge that we’ll be alerted as soon as the water has boiled.

Get hands-on with 1400+ tech skills courses.