Blogger Information
Blog 14
fans 0
comment 0
visits 9502
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 函数及条件判断
Mr.Ran
Original
905 people have browsed it

函数

1.普通函数

代码实例:

  1. function writeTitle(){
  2. echo "PHP中文网";
  3. }
  4. //输出
  5. writeTitle();

运行结果: PHP中文网

2.带参数的函数

代码示例:

  1. function jia(int $num_1,int $num_2){
  2. echo $num_1.'+'.$num_2.'='.($num_1 + $num_2);
  3. }
  4. //计算
  5. jia(2,1);

运行结果:2+1=3

3.有返回值的函数

代码示例:

  1. function jian(int $num_1,int $num_2){
  2. return $num_1 - $num_2;
  3. }
  4. // 调用
  5. echo jian(4,1)

运行结果:3

条件

使用 比较运算符 实现条件判断
代码示例:

  1. $age = 19; //设定年龄,判断其年龄段划分
  2. if ($age < 5) {
  3. echo "童年";
  4. } else if($age >= 5 && $age <= 9){
  5. echo "幼年";
  6. } elseif ($age >= 10 && $age <= 19) {
  7. echo "少年";
  8. } elseif ($age >= 20 && $age <= 34) {
  9. echo "青年";
  10. }else {
  11. echo "其他";
  12. }

运行结果:少年

三元运算符

一般用于单行代码动态处理数据的情况使用.
代码示例:

  1. $title = 'PHP中文网';
  2. echo isset($title) ? $title :'无标题';

运行结果:PHP中文网

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post