【PHP学习】函数
来源:互联网 发布:centos 安装opera 编辑:程序博客网 时间:2024/05/17 22:03
1.创建函数
其实PHP里面对于函数的操作跟C语言类似,只是多加了些功能,这里我就写下多出来的了,没写的照搬C语言就行。
从函数返回多个值
利用list()构造可以很方便地从数组中获取值。比如:
<?php $colors=array("red","blue","green"); list($red,$blue,$green)=$colors;?>
执行了list()构造后,$red
、$blue
、$green
分别被赋值为red、blue和green。
<?php function retrieveUserProfile() { $user[]="Jason Gilmore"; $user[]="jason@example.com"; $user[]="English"; return $user; } list($name,$email,$language)=retrieveUserProfile();echo "Name:$name,email:$email,language:$language";?>
递归函数
这里可能是书本写的不太全,目前不太明白PHP的递归机制,所以可能会查些资料,作为补充。
这是书本上还贷计算器的代码:
<?php function amortizationTable($pNum,$periodicPayment,$balance,$monthlyInterest) { $paymentInterest=round($balance*$monthlyInterest,2); $paymentPrincipal=round($periodicPayment-$paymentInterest,2); $newBalance=round($balance-$paymentPrincipal,2); if($newBalance<$paymentPrincipal){ $newBalance=0; } printf("<tr><td>%d</td>",$pNum); printf("<td>$%s</td>",number_format($newBalance,2)); printf("<td>$%s</td>",number_format($periodicPayment,2)); printf("<td>$%s</td>",number_format($paymentPrincipal,2)); printf("<td>$%s</td></tr>",number_format($paymentInterest,2)); if($newBalance>0){ $pNum++; amortizationTable($pNum, $periodicPayment, $newBalance, $monthlyInterest); } else{ return 0; } } $balance=10000.00; $interestRate=.0575; $monthlyInterest=$interestRate/12; $termLength=5; $paymentsPerYear=12; $paymenetNumber=1; $totalPayments=$termLength+$paymentsPerYear; $intCalc=1+$interestRate/$paymentsPerYear; $periodicPayment=$balance*pow($intCalc,$totalPayments)*($intCalc-1)/(pow($intCalc,$totalPayments)-1); $periodicPayment=round($periodicPayment,2); echo "<table width='50%' align='center' border='1'>"; echo "<tr> <th>Payment Number</th><th>Balance</th> <th>Payment</th><th>Principal</th><th>Interest</th> </tr>"; amortizationTable($paymenetNumber,$periodicPayment,$balance,$monthlyInterest); echo "</table>";?>
函数库
0 0
- php url函数学习
- PHP学习笔记-函数
- php学习---函数
- php排序函数学习
- php学习之函数
- PHP函数学习笔记
- 【PHP学习】函数
- php学习-12函数
- PHP函数随机学习。
- PHP学习:函数
- php入门学习-----函数
- 学习PHP函数记录
- php学习笔记--函数
- PHP函数学习之PHP函数点评
- PHP学习笔记【10】--PHP函数
- php 常用函数学习1
- 学习PHP数组函数使用方法
- php学习之mail函数
- 提高效率—编程中的技巧
- jsp 生成验证码代码
- Spring中的事务控制(Transacion Management with Spring)
- Google I/O 2015 为 Android 开发者带来了哪些福利?
- getopt getopts
- 【PHP学习】函数
- Twemproxy源码分析之三:其进程以及时间模型
- ndk-lib实现编译并将生成文件拷贝到项目libs中
- 推荐一本Android开发入门书籍
- Android学习之SharePreferences轻量级数据存储
- 开始刷leedtcode day72:Product of Array Except Self
- 周一工作
- [高可用]Ubuntu搭建高可用的MariaDB Galera数据库集群
- JAVA常用类之——Date,Calendar,DateFomat