//1. Use a loop statement to output any two-dimensional array.
$arr=array(
array(1,2,3,4),
array(5,6,7,8),
array(9,10,11,12),
array(13,14,15 ,16)
);
foreach ($arr as $var){
foreach ($var as $val1){
echo "$val1 ";
}
echo " ";
} p>
echo " ";
//2. Use the loop control statement to output the Yang Hui triangle.
function yanghuisanjiao($line){
$sc[][]=array();
$sc[0][0]=1;
for($i=1;$i<=$line;$i++) {
for($j=0;$j<=$i;$j++){
if($j==0 or $i==$j){
$sc[$i][$j]=1 ; //Set the first number and last number of each line to 1
}else{
$sc[$i][$j]=$sc[$i-1][$j-1]+$ sc[$i-1][$j];
}
}
}
foreach ($sc as $value){
foreach($value as $v1){
echo $v1.' ';
}
echo '
';
}
}
yanghuisanjiao(5);
echo " ";
//3. Use Loop and predefined variables, get multiple parameters. The number of parameters is undetermined.
function avg(){
$ags=func_get_args();
$sum=0;
foreach ($ags as $v){
$sum+=$v;
}
return 'The average is:'.$sum /func_num_args();
}
echo avg(1,2,3,4,5,6,7);
//4. Use a loop to output a two-dimensional array, and Find the sum of the diagonal elements of the rectangle.
function getSum($theCount){
$b=0;
echo '';
echo "
";
for($i=1;$i<=$theCount;$i++ ){
echo "
";
for($j=1;$j<=$theCount;$j++){
if($j==$i || $theCount+1-$i== $j){
echo "
$j
";
$b=$b+$j;
if($j==$i && $theCount+ 1-$i==$j){
$b=$b+$j;
}
}
else{
echo "
$j
";
}
}
echo "< /tr>";
}
echo "
";
echo "The sum of the diagonal elements is:".$b;
}
getSum(6);
?>
Copy code
You may also like:
php example of array recursive sum
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a
Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a
Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application.
Data persists only for the subsequent request by default:
$request-
This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev
Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive.
The basic implementation provides a variety of response type shortcuts:
use Illuminate\Support\Facades\Http;
Http::fake([
'google.com' => 'Hello World',
'github.com' => ['foo' => 'bar'],
'forge.laravel.com' =>
The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.
Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom
The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio