写在前面:今天文章介绍的是用php函数实现的一些小功能。仅做学习使用。
一、打印乘法表
/**
* @desc 源代码
* @author PHP学习坊
* @param int $end 终止的数字
*/
function printTable( $end = 9 ){
for ( $i = 1; $i <= $end; $i++ ){
for ( $j = 1; $j <= $i; $j++ ){
echo ” {$j} * {$i} = “.$i * $j;
}
echo “\n”;
}
}
printTable( 11 );
/**
* @author PHP 学习坊
* @desc 打印菱形 源代码 关键点是每行$char个数的计算以及空格的数量的计算
* @param $height 层数
* @param $char 输出的图形
*/
function printRhombus( $height = 7 , $char = ‘*’){
$result = array();
$max = $height * 2 – 1;
for ( $i = 1; $i <= $height; $i++ ){
//第i层多少个$char
$num = 2 * $i -1;
$str = ”;
for ( $j = 0; $j < $height – $i+1; $j++ ){
$str .= ‘ ‘;
}
for ( $j = 0; $j < $num; $j++ ){
$str .= $char;
}
$result[$i] = $str;
$result[$height * 2 – $i + 1] = $str;
}
for ( $i = 1; $i <= $height * 2; $i++ ){
echo $result[$i],”\n”;
}
}
printRhombus(9 , ‘#’);
ps:最近几天会每天介绍两个小方法,一方面帮助大家熟悉函数,一方面熟悉一下php各种内置的方法,算是对前几天的一个总结。感谢关注 PHP学习坊 ,文章中如有错误或疏漏之处,欢迎大家在评论区指出。也欢迎大家积极转发评论。