in the second season of the show there was an episode called summer belongs to you where dr. doofenshmirtz works with perry the platypus to rescue his daughter vanessa doofenshmirtz. in the third season there was an episode called agent doof where dr. doofenshmirtz tries to become a secret agent working for the good guys and accidentally causes more problems than he did when he was evil so he decides to go back to being evil. in the season three episode, where’s perry, dr. doofenshmirtz builds a machine called the “ultimate evil-inator” to turn perry’s wikt:boss major monogram evil but accidentally hits monogram’s assistant carl making him turn evil. so doof ends up working with monogram and perry to stop carl because carl is a more dangerous villain than doof was so dr. doofenshmirtz builds a machine called the re-good inator and turns carl good again. in the movie phineas and ferb: across the second dimension, dr. doofenshmirtz goes to a parallel universe where a more evil version of himself already won and doof ends up stopping the other him from taking over the tri-state area from the main universe. in the season 4, episode mission marvel, doofenshmirtz accidentally steals the powers of the superheroes from marvel comics and teams up with the marvel supervillains but the villains betray him so doofenshmirtz helps stop them. in the season 4 episode minor monogram, doofenshmirtz’s sidekick betrays dr. doofenshmirtz and tries to take over the world so doof helps stop him. in the season four episode, “primal perry” dr. doofenshmirtz hires a hunter from australia named liam to trap perry the platypus but ends up working with perry to stop him after liam tries to kill them both. in the episode doof 101, it’s revealed that after summer ended dr. doofenshmirtz was given a choice between jail and community service and ended up as a high school science teacher. and the episode act your age set in the future reveals that dr. doofenshmirtz didn’t commit any crimes since then. in the episode phineas and ferb save summer, dr. doofenshmirtz helps the good guys stop a mad scientist named rodney from putting the world in a new ice age but says he’s still evil. in the horror episode night of the living pharmacists, he accidentally starts a zombie pharmacist apocalypse and helps to stop it. in the series finale last day of summer, he builds a machine called the do-over inator so that he can start the same day over any number of times and accidentally nearly destroys the universe so he tries to save it from himself and his daughter vanessa convinces him to turn good.