Technicality, but the Phantom didn't have a cannon.
The F-4E had a internal cannon in the nose.
During the initial design of the Phantom, several proposals had been considered for a cannon-armed version. In fact, the original F3H-E proposal was designed around a quartet of 20-mm cannon. However the philosophy of the day was that the air-to-air missile was the wave of the future and that the internal gun was an obsolete holdover from an bygone era. Consequently, all Phantoms to reach production had been armed exclusively with missiles.
However, the all-missile fighter had shown some serious drawbacks in the initial air-to-air battles over Vietnam. The earlier Sparrow, Falcon, and Sidewinder air-to-air missiles did not perform up to expectations. They were expensive, unreliable, and vulnerable to countermeasures. Many an enemy MiG was able to escape unscathed because a Phantom-launched missile malfunctioned and missed its target. The Phantoms could carry a podded cannon mounted on the centerline, but it was relatively inaccurate, caused excessive drag which reduced the performance of the Phantom carrying it, and took up a valuable ordinance/fuel station.
An initial F-4 variant with an internal M61 cannon had been proposed by McDonnell to the USAF in March of 1961, but had met with little enthusiasm. McDonnell began a new design study for a gun-armed Phantom in late 1964 and finally got the attention of the Air Force. The gun-armed F-4E was finally funded in June of 1965. It was destined to be produced in greater numbers than any other single Phantom variant.
The main difficulty in equipping the Phantom with an internal cannon was in finding a place to put it. The solution was found in using the sharper, longer nose of the F-4C reconnaissance version. The new nose was fitted with an AN/APG-30 radar set and an external pod was mounted underneath the nose that could carry a single six-barrel 20-mm General Electric M61A1 rotary cannon
The first YRF-4C (62-12200) was modified to test this new arrangement. A lead computing gunsight was cannibalized from an Air National Guard F-100D. Flight test instrumentation was carried in a centerline pod. Temporarily redesignated YF-4E, the modified aircraft first flew on August 7, 1965.
After 50 flights, the first YF-4E was re-engined with J79-GE-J1B engines (prototypes of the -10 and -17 series). The results with the YF-4E were sufficiently encouraging that two other YF-4Es were produced by modifying an F-4C (63-7445) and an F-4D (65-0713). These planes had the definitive nose-mounted cannon installation. The second YF-4E had the gun and no radar, but the third had both the gun and the radar. Both aircraft were powered by the J79-GE-J1B engines, but both were later re-engined with the definitive J79-GE-17 powerplant, which required new mounts and additional titanium sheeting in the engine bays to accommodate the higher temperatures.
The severe space constraints in the new nose meant that a new ammunition feed system had to be designed for the M61A1 cannon. In addition, the proximity of the gun to the radar set required that very effective vibration dampers and noise/blast eliminators had to be designed.
An initial batch of 96 F-4Es was ordered in August 1966 as part of an F-4D contract. The first production F-4E (serial number 66-0284) flew on June 30, 1967, R. D. Hunt and Wayne Wight being the crewmembers.