Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
echo "\n題目一===================================\n";
$text = 'php is great';
// 先轉成大寫
$upperText = strtoupper($text);
// 替換 GREAT 為 AWESOME
$finalText = str_replace('GREAT', 'AWESOME', $upperText);
// 輸出結果
echo $finalText;
echo "\n題目二===================================\n";
// 定義函式:取得單一學生名字長度(不含空白)
function getSingleNameLength(string $name): int {
// 移除所有空白後計算長度
$nameWithoutSpaces = str_replace(' ', '', $name);
return strlen($nameWithoutSpaces);
}
// 定義函式:取得所有學生名字長度
function getStudentsNameLength(array $students): array {
$result = [];
foreach ($students as $name) {
$result[$name] = getSingleNameLength($name);
}
return $result;
}
// 定義函式:在結果中加入學生總數量
function appendStudentCount(array $studentsNameLength): array {
$studentsNameLength['student_count'] = count($studentsNameLength);
return $studentsNameLength;
}
// 測試資料
$students = ['Ethan Caldwell', 'Lucas Harper', 'Mason Bennett'];
// 執行流程
$nameLengths = getStudentsNameLength($students);
$finalResult = appendStudentCount($nameLengths);
// 輸出結果
foreach ($finalResult as $name => $length) {
echo $name . ': ' . $length . "\n";
}
echo "\n題目三===================================\n";
$schema = 'https';
$host = 'example.com';
$path = 'v1/api/doge/transactions';
$query = [
'page' => 1,
'per_page' => 15
];
// 轉換 query 陣列成字串
$queryString = http_build_query($query);
// 組出 full URL
$fullUrl = sprintf('%s://%s/%s?%s', $schema, $host, $path, $queryString);
// 輸出結果
echo $fullUrl;