The Protoss Photon Cannon is easily the most recognizable defense structure in the entire Starcraft universe. A po[censored] r building for the Protoss, these cannons are used by most, if not all, Protoss players due to their sheer versatility. Starcraft 2 Photon Cannon tactics may vary; these cannons are great at dealing with a number of different problems.
First off Photon Cannons are great because they have the ability to target both ground and air units. This is unique to the Protoss race as the other races do not possess such a defensive structure.
It is indeed not a question of how many anti-air structures are affordable or if that many anti-ground structures are constructed, but rather, how many defense structures are affordable in general. In the end Photon Canons take away a lot of the thinking when it comes to how a certain area should be defended.
Another advantage to building these cannons is their ability to detect hidden units. Much like the Observer unit, these defense structures can detect enemies that are cloaked, making them excellent defense for any type of invasion and making them also very effective guards against the Zerg, and their borrowing Infestors and Roaches.
Also, the nature of how Probes construct their buildings makes production of these structures easy and quick, allowing Protoss players to construct defensive positions quickly and easily. Also, these buildings are great for walling in enemies, they can block units from reaching certain areas and can therefore buy a lot of time, and whittle down opponents in the meantime.
The Achilles' heel of Photon Cannons is definitely the fact that they require a link to a Psionic Matrix at all time and will become nonoperational if the Pylon they are linked to is destro[censored] . This severing from the matrix is a severing from the Protoss power-structure back on their home-world of Aiur. Protoss do not build their structures, they warp them in, and power them through a matrix which transports power from their home-world to other sources.
Building these cannons in the early game is not always necessary unless you go for a forge fast expand. Relying solely on these structures as your only means of defense should still be avoided. Given the chance to outrage or byp[censored] your cannons, a witty adversary can find ways of disabling your defenses without much trouble.
Building Photon cannons mid game is a good move, as early into a match it would make more sense to purchase early game units and scout or har[censored] an enemy, rather then to start defending your location unless of course you do the forge fast expand. You should remember though that sitting behind walls won't win you the game on its own. These defense structures are still great at defending key areas however, and can prove to be a nuisance to your enemies.