To display an ad from within PHP code, make a call to the ad() function. You can optionally specify an ad group, the number of ads to display, and several other options. For example, to display one random ad from all ads that have not been assigned to any group you don't have to pass in any parameters: print ad();

To display two ads from all ads that have not been assigned to any group you would execute the following code:
print ad(0, 2);

The first parameter specifies the ad group to display ads from. By specifying 0, we are telling the ad module to display ads that are not assigned to any group. The second parameter specifies the number of ads to display at one time.

To randomly display an ad from a specific group of nids, for example with the node ID 69 or 76, you would pass in the following parameters:
print ad(NULL, 1, array('nids' => '69,76'));

When specifying specific nids, any specified ad group is ignored, so we leave the first parameter as NULL. The second parameter causes only one ad to be displayed at a time. And the third parameter is an array that tells the ad module to randomly display either ad 69 or ad 76.

To display and ad randomly selected from multiple groups you can simply specify multiple groups separated by commas. For example, to display 3 ads from groups 24, 56 and 98 you would pass in the following parameters:
print ad('24,56,98', 2);

You can also specify how to display a given ad. Current display methods are 'javascript' and 'raw'. When using the 'javascript' method, ads will randomly change even when the Drupal pagecache is enabled. When using the 'raw' method, ads will only change when the Drupal pagecache is flushed. To force one ad with a tid of 76 to display using JavaScript you would pass in the following parameters:
print ad(76, 1, array('method' => 'javascript'));

To force two ads with a tid of 101 or 102 to display using the Raw method you would pass in the following parameters:
print ad('101,102', 76, array('method' => 'raw'));