Phase and antigenic variation
Phase variation: the ability of a gene to switch ON and OFF in a reversible manner, without any permanent genetic change that locks it into one state or the other.
Antigenic variation: switching between variants of a single protein/phenotype.
most commonly involves changes to the protein-coding sequence of the gene. In some instances, though, it is possible for antigenic variation to occur via the mechanism of phase variation, with the ON/OFF switching of different genes producing multiple variants of a single protein/phenotype.
Many of the effects of phase and antigenic variation can be replicated through mutation/evolution over time, so we must be careful to define the processes separately.
A system can only be said to be genuinely controlled by phase variation or antigenic variation if the changes are more frequent than would be expected from traditional genetic mutation.
Phase variation
Some virulence traits, e.g. chaperone-usher fimbriae, are not able to be retracted from the cell surface once created. This prevents a problem that a bacterial pathogen must overcome, especially since many of these proteins are highly immunogenic.
Some bacteria employ methods of immune evasion such as capsule production or molecular mimicry. Some also vary the structure of their cell-surface proteins through antigenic variation. However, some proteins cannot be hidden from the immune system in this way.
To overcome this problem, bacteria employ a "bet-hedging" strategy where subsets of a given bacterial population either produce or do not produce the immunogenic proteins.
If the proteins are needed to obtain vital nutrients, then the cells producing them will grow and divide more readily. However, if the bacteria are under immune attack, then the cells in which the proteins are not produced will more easily survive and repopulate the host. Therefore, it is useful for the overall population to have a mixture of cells with the proteins switched "ON" and "OFF" - but with each individual cell retaining the ability to randomly switch between the two states.

Note how the mixture of phase ON/OFF cells and continual phase switching allows for a surviving population to persist, no matter the specific challenge.
Phase variation in type I fimbriae
A major example of phase variation in E.coli is the production of type I fimbriae, encoded by the fim operon.

fimB and fimE do not encode proteins associated with the fimbrial structure itself; these are the regulatory recombinases responsible for phase variation.
The fimAICDFGH structural operon lies downstream of a single promoter (near fimA) that produces a transcript containing all genetic instructions for the assembly of type I fimbriae. Upstream of the fimA promoter, the genes fimB and fimE are encoded as independent transcripts. The FimB and FimE proteins are site-specific recombinases that bind to an area surrounding the fimA promoter, known as the "fim switch" (fimS).
fimS is a 314 bp invertible element with 9 bp inverted repeats at each end. When the FimB or FimE recombinases bind to the inverted repeats, they interact to reverse the direction of the invertible element, changing whether or not the promoter is correctly orientated towards the fimA gene.
fimS orientation determines the expression of fimA due to promoter placement.
Inversion of the fim switch itself is further regulated by DNA-binding proteins IHF and Lrp, in response to the levels of branched-chain amino acids within the cell. Expression of the "switch ON" recombinase gene, fimB, is likewise down-regulated in response to the presence of sialic acid (liberated from the host during an immune response) and free N-acetylglucosamine (a bacterial cell wall component, i.e. an indicator of host immune attack). Despite these factors that influence whether or not the switch is predominantly ON or OFF within the population, the fimbriation state of any individual cell is still largely decided at random.
FimE: the "OFF" switch
The recombinase protein FimE preferably turns the system from ON to OFF at a very high rate. There are several reasons for this:
FimE preferentially binds to fimS when it is in the ON orientation.
fimE mRNA is degraded from the 3' end when the switch is OFF, so fewer FimE proteins can be produced.

The fimE transcript extends into the fim switch, so its 3' end sequence depends upon the switch orientation.
FimB: the "ON" switch
The recombinase protein FimB can switch the system in either direction, albeit more slowly. This effectively makes FimB the protein responsible for switching the system "ON". As such, it is heavily regulated. The region upstream of the fimB promoter is very large (at 1.4 kb, it's around 10x the average intergenic region in bacteria) and binds many regulatory proteins that control its expression.

Expression of the fimB gene is regulated by many other factors.
Coordinate control of gene expression
NanC and fimB are divergently transcribed, i.e. the genes appear on opposite strands of the DNA facing away from each other. This means that they share a regulatory region. We say they are coordinately controlled, which means they share at least some aspects of regulation. Some factors that repress nanC expression increase fimB expression and vice versa.
NanC is an enzyme involved in sialic acid metabolism, so the two proteins are not directly related, although since some of the regulators of nanC also regulate fimB, we've found that fimB expression responds to the presence of sialic acid.
This is interesting because sialic acid is released from host cells upon immune activation, so fimB is likely recognising this as a signal to lower expression in response to the host immune system.
Other factors controlling fimB include the bacterial cell wall component GlcNAc (N-acetylglucosamine), which we believe the system recognises as evidence of bacterial cell damage nearby, and the SlyA protein, which responds to starvation conditions, increasing fimB expression when the bacterium is in need of nutrients.
Antigenic Variation
Salmonella flagella proteins
In Salmonella, a reversible DNA region (similar to the fim switch) controls H2 flagellin production, leading to the ON/OFF switching (phase variation) of flj-type flagella.
However, the fljA gene in this operon encodes a repressor of the fliC promoter.
Hence, when flj operon is expressed, the fli operon (encoding H1 flagellin) is silenced and vice-versa.
So the Salmonella flagellar antigen is under antigenic variation as it switches between H1 and H2 variants - however the precise mechanism for this is phase variation.
Neisseria Opa proteins
The Neisseria gonorrhoeae Opa proteins are afimbrial adhesins that undergo antigenic variation by switching between different variants of the opa gene (each encoding different protein sequences).
They do this via a phase variation mechanism known as slipped-strand mispairing, where multiple repeat regions in the ORF cause accidental "slips" during DNA replication.
Because one or more of these repeats can therefore be excised from the strand during replication (see Figure 3), the entire ORF can move in- or out-of-frame and then produce either a functional or non-functional protein.
This happens randomly at multiple opa gene loci throughout the genome, so the Neisseria population ends up with a mix of different Opa proteins on its individual cells.
Neisseria type IV pili
Rather than using a phase variation mechanism, Neisseria meningitidis type IV pili undergo antigenic variation of the PilE protein (major pilin) via a process known as gene conversion.
In this case, the pilE gene is located near to a series of non-transcribed gene fragments known as pilS cassettes.
Due to the similar (but not identical) sequences of the pilS cassettes to the pilE coding region, homologous recombination can switch parts of these variants into the pilE ORF and hence create new variants of the PilE protein itself.

Antigenic variation by gene conversion in the pilE gene of N. meningitidis.