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
//ex1
$ex1 = "php is great";
echo strtoupper(str_replace("great","awesome",$ex1));
echo "\n";
//ex2
$ex2 = ['Ethan Caldwell','Lucas Harper','Mason Bennett'];
function getSingleNameLength(string $name):int {
return strlen(str_replace(' ','',$name));
}
function getStudentsNameLength(array $students):array {
$ret = [];
foreach ($students as $student) {
$ret[$student] = getSingleNameLength($student);
}
return $ret;
}
function appendStudentCount(array $studentsNameLength) : array {
$ret = getStudentsNameLength($studentsNameLength);
$ret['student_count'] = count($studentsNameLength);
return $ret;
}
//echo getSingleNameLength(str_replace(' ', '','Ethan Caldwell'));
//print_r(getStudentsNameLength($ex2));
print_r(appendStudentCount($ex2));
//ex3
$schema = 'https';
$host = 'example.com';
$path = 'v1/api/doge/transactions'
$query = [
'page' => 1,
'per_page' => 15
];
/*-------------------------*
* 1) 組合查詢字串 (key=value)
*-------------------------*/
$queryString = http_build_query($query); // page=1&per_page=15
// 若要使用 RFC 3986 (%20 而非 +) 可寫:
// $queryString = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
/*-------------------------*
* 2) sprintf 拼接整體 URL
*-------------------------*/
$url = sprintf('%s://%s/%s?%s', $schema, $host, $path, $queryString);
echo $url;
//sprintf("%s")