📜  tl to usd - PHP (1)

📅  最后修改于: 2023-12-03 15:05:36.328000             🧑  作者: Mango

TL to USD - PHP

This script allows you to convert Turkish Lira (TL) to US Dollar (USD) using PHP. This could be useful for websites or applications that serve a Turkish audience and need to display prices in both TL and USD.

Prerequisites

You will need PHP installed on your server to use this script.

Usage

To use the TL to USD conversion function, simply include the tl_to_usd.php file in your PHP script and call the tl_to_usd() function, passing the amount in TL as a parameter.

Example
include('tl_to_usd.php');

$tl_amount = 100;
$usd_amount = tl_to_usd($tl_amount);

echo $tl_amount . ' TL is equal to ' . $usd_amount . ' USD';

This would output:

100 TL is equal to 11.89 USD
Code

Here's the code for the tl_to_usd() function:

function tl_to_usd($tl_amount) {
  // Get the exchange rate from an API (replace with your own)
  $exchange_rate = file_get_contents('https://api.exchangeratesapi.io/latest?symbols=USD&base=TRY');
  
  // Convert the exchange rate JSON to an array
  $exchange_rate = json_decode($exchange_rate, true);
  
  // Get the USD exchange rate
  $usd_rate = $exchange_rate['rates']['USD'];
  
  // Calculate the USD amount
  $usd_amount = $tl_amount / $usd_rate;
  
  // Round to two decimal places
  $usd_amount = round($usd_amount, 2);
  
  // Return the USD amount
  return $usd_amount;
}
Notes

This script uses the Exchange Rates API to get the latest exchange rate, but you can replace this with any other API or method that provides exchange rate data. Additionally, note that exchange rates fluctuate constantly, so this conversion is only accurate at the time of execution.