remember 1st ionization energy increases from left to right and bottom to top (essentially diagonally to Helium) on the periodic table.
both atoms are in the same row. so in this case you could think that the number of core electrons between the nucleus of each atom and its valence electrons are the same, but the difference lies in the number of nuclear protons. therefore the attractive pull of P's 15 protons is higher than that of Mg's 12 and would make it more difficult to strip P of the first electron.
in this specific comparison you can relate 1st ionization energies to the difference of their effective nuclear charge seeing as they're in the same row (but this method won't work in every case).