What are the pros and cons to using cron jobs, rather than just execute the code everytime a user views a webpage?
Pros and Cons about Using Cron Jobs
if the code you need to run takes 20 seconds to execute, the visitor doesnt have to wait.
the code may not need to be run each time
you may not want the code to be run each time
also, say for example you "cron script" opened a file and did some reading and writing to it, modifying the file. what happens if its in the middle of modifying the file, and then another visitor requests a page, causing that half complete file to be opened again by the other script? expect bad things to happen. of course that can be prevented w/ file locking, but the problem is not specifically w/ working on files, i just used it as an example. undersired race conditions could happen w/ many diff operations.
w/ cron, the script gets run only when you want it to, on a set schedule.
also, if your website doesnt get any visitors for 3 days, cron will still do its job. but your php imitation cron job wont.
Are there any negatives to cron jobs?
not really, as long as your using them only when its appropriate.
its just a tool in your arsenal. if you use it for the wrong reason, there could be negatives. but the same could be said of php itself.