首页 > 代码库 > 学习函数知识

学习函数知识

 2016.12.20.星期二

        一、递归的含义

              1. 递归的公式

            function 函数($n){

    if($n满足了结束条件){

return 结束值;

}else{

return 规律运算;

    }

        }

        2.用递归输出从1加到100;

          function hanshu($n){

        if($n==0){

    return $n=0;

      }else{

return $n+hanshu($n-1);

    }

        }

                echo hanshu(100);

        二、用php写出简单的计算器代码:

            

<!DOCTYPE html>

<html>

<head>

<meta charset="utf8">

<title></title>

</head>

<body>

<?php

if (isset($_POST[‘fuhao‘]) && !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘+‘) {

$result=$_POST["num1"] + $_POST["num2"];

}

elseif (isset($_POST[‘fuhao‘]) && !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘-‘) {

$result=$_POST["num1"] - $_POST["num2"];

}

elseif (isset($_POST[‘fuhao‘]) && !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘x‘) {

$result=$_POST["num1"]*$_POST["num2"];

}

elseif (isset($_POST[‘fuhao‘]) && !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘/‘) {

$result=$_POST["num1"]/$_POST["num2"];

}

?>


<form action="" method="post">

        <input type="text" name="num1" value="http://www.mamicode.com/<?php if(!empty($_POST[‘num1‘])){echo $_POST[‘num1‘];}?>">

<select name="fuhao">

<option value="http://www.mamicode.com/+" <?php 

if(isset($_POST[‘fuhao‘]) &&                     !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘+‘)

{

    echo "selected";

}

?>>+</option>

<option value="http://www.mamicode.com/-" <?php 

if(isset($_POST[‘fuhao‘]) &&         !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘-‘)

{

    echo "selected";

}

?>>-</option>

<option value="http://www.mamicode.com/x" <?php 

if(isset($_POST[‘fuhao‘]) &&     !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘x‘)

{

    echo "selected";

}

?>>x</option>

<option value="http://www.mamicode.com/" <?php 

if(isset($_POST[‘fuhao‘]) &&         !empty($_POST[‘fuhao‘]) &&$_POST[‘fuhao‘] == ‘/‘)

{

    echo "selected";

}

?>>/</option>

        </select>

    <input type="text" name="num2" value="http://www.mamicode.com/<?php if(!empty($_POST[‘num2‘])) {echo $_POST[‘num2‘];}?>">

    <input type="submit" valuehttp://www.mamicode.com/="=">

<span><?php if(isset($_POST[‘fuhao‘]) &&         !empty($_POST[‘fuhao‘]))

                {

echo $result;

        }

            ?></span>

</form>


</body>

</html>

    完成图:

        技术分享



学习函数知识