Task 3a: String Functions";
// Reverse a string
$word = "Kgonagalo";
$reversed = strrev($word);
echo "Original: $word
";
echo "Reversed: $reversed
";
// Convert string to lowercase
$greeting = "KGONAGALO!";
$lowercase = strtolower($greeting);
echo "Original: $greeting
";
echo "Lowercase: $lowercase
";
// Calculate length of a sentence
$sentence = "Kgonagalo is the Son of Kgalema!";
$length = strlen($sentence);
echo "Sentence: "$sentence"
";
echo "Length: $length characters
";
// Split a CSV string into an array
$csv = "Mogau,Kgonagalo,Kgalemelo,Kgalema";
$names = explode(",", $csv);
echo "Names:
";
foreach ($names as $name) {
echo "- $name
";
}
echo "
";
// Repeat a pattern multiple times
$pattern = "K";
$repeated = str_repeat($pattern, 10);
echo "Repeated pattern: $repeated
";
////////////////////////////// Task 3b: Date and Time//////////////////////////
echo "
Task 3b: Date and Time
";
// Get current date and time details
$currentDate = new DateTime();
$year = $currentDate->format('Y');
$month = $currentDate->format('m');
$dayOfWeek = $currentDate->format('l');
$weekNumber = $currentDate->format('W');
// Calculate days until Christmas 2025
$christmas = new DateTime('2025-12-25');
$interval = $currentDate->diff($christmas);
$daysToChristmas = $interval->days;
echo "Year: $year
";
echo "Month: $month
";
echo "Day of the Week: $dayOfWeek
";
echo "Week Number of the Year: $weekNumber
";
echo "Days to Christmas (25-Dec-2025): $daysToChristmas
";
//////////////////////////////Task 3c: SADC Countries Array//////////////////////////
echo "Task 3c: SADC Countries
";
// Define an array of SADC countries
$sadcCountries = [
"Angola",
"Botswana",
"Democratic Republic of Congo",
"Eswatini",
"Lesotho",
"Madagascar",
"Malawi",
"Mauritius",
"Mozambique",
"Namibia",
"South Africa",
"Tanzania",
"Zambia",
"Zimbabwe"
];
// Display original list
echo "Original List of SADC Countries:
";
foreach ($sadcCountries as $country) {
echo "$country
";
}
// Sort and display in descending order
rsort($sadcCountries);
echo "
SADC Countries in Descending Order (Z to A):
";
foreach ($sadcCountries as $country) {
echo "$country
";
}
////////////////////////////// Task 3d: Country Capital Lookup//////////////////////////
echo "Task 3d: SADC Country Capitals
";
// Check if a country was selected from the form
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['country']) && $_GET['country'] != "") {
$country = $_GET['country'];
// Define capital cities for selected countries
$capitals = [
"Angola" => "Luanda",
"Botswana" => "Gaborone",
"DRC" => "Kinshasa",
"Eswatini" => "Mbabane",
"Lesotho" => "Maseru",
"Madagascar" => "Antananarivo",
"Malawi" => "Lilongwe",
"Mozambique" => "Maputo",
"Namibia" => "Windhoek",
"South Africa" => "Pretoria"
];
// Lookup and display capital
$capital = $capitals[$country] ?? "Unknown";
echo "Capital of $country: $capital
";
}
?>