How can PHP be used to automatically generate affiliate links on a website?

To automatically generate affiliate links on a website using PHP, you can create a function that takes in a product ID or keyword and appends the affiliate tracking code to the URL before displaying it on the page. This way, whenever a user clicks on the generated link and makes a purchase, the affiliate will receive credit for the referral.

function generateAffiliateLink($productId) {
    $affiliateCode = 'your_affiliate_code_here';
    $productUrl = 'https://example.com/product/' . $productId;
    $affiliateLink = $productUrl . '?ref=' . $affiliateCode;
    
    return $affiliateLink;
}

// Example of generating an affiliate link for a product with ID 123
$productId = 123;
$affiliateLink = generateAffiliateLink($productId);
echo '<a href="' . $affiliateLink . '">Product Link</a>';