PHP implements two code programs that use functions to find the number of digits of multi-digit values, use functions to input 3 numbers, and output them in the form of forms

Table of contents

foreword

1. Use functions to find the number of digits in multi-digit values

1.1 Operation process (thought)

1.2 code snippet

1.3 run screenshot

2. Use the function to input the sum of 3 numbers and output it in form

2.1 Operation process (thought)

2.2 code snippet

2.3 Running Screenshot


foreword

1. Due to multiple reasons, this blog post consists of two code programs. If you have a choice, you can quickly search in the directory;

2. This pop-up window interface can realize one-use functions according to simple requirements. At the same time, custom settings can be realized;

3. This article introduces PHP code exercises. I use the code written by Adobe Dreamweaver2021 software. In principle, other software such as HBX, VSCode or DW are compatible with lower and higher versions. If you need it and the writing software is not DW, etc. software, please paste the content in <?php?>;

4. Here is a special note, if the package name of the complete code to be pasted is inconsistent with mine, the program is specified to be unable to run, please change it manually;

5. This code was written by me when I was in school. There are some places that have not been perfectly implemented. Please forgive me and give me more advice. If you find any problems, please give me feedback, so that I can correct mistakes, summarize and improve, Continuous improvement! 

6. This is a blog post that needs to be published and run. I use Google Chrome, and Google Chrome is recommended;

7. To use PHP code alone, please paste the content in <?php ?>;


提示:以下是本篇文章正文内容,下面案例可供参考

1. Use functions to find the number of digits in multi-digit values

1.1 Operation process (thought)

This is a way to submit values ​​using a form, then judge through a while loop, and finally output the program. The specific setting idea is as follows:

1.2 code snippet

The code is as follows (example):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用函数求多位数上的位数</title>
</head>
<form method="post" action="">
 请输入一个整数:
 <input type="number" min="1" nax="1000" name="num">
 <input type="submit" value="提交" name="tijiao"> 
  </form>
<body>
<?php
 function weishu($n)
 {
 $k=0;
 while($n!=0){
  $k++;
  $n=(int)($n/10);
 }
  return $k;
 }
  if(isset($_POST["tijiao"])){
   $num=$_POST["num"];
  echo "{$num}的是".weishu($num)."位数。";
}
 
 ?>

</body>
</html>

1.3 run screenshot

The code is as follows (example):

2. Use the function to input the sum of 3 numbers and output it in form

2.1 Operation process (thought)

This is where the values ​​are first entered in the form and then summed. Finally, program output is performed. The specific setting idea is as follows:

2.2 code snippet

The code is as follows (example):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用函数方式输入3个数,求和并以表单形式输出</title>
</head>

<body>
    <form method="post" action="">
        请输入第一个整数:
 <input type="num1" min="1" nax="100" name="num1"><br>
  请输入第二个整数:
 <input type="num2" min="1" nax="100" name="num2"><br>
  请输入第三个整数:
 <input type="num3" min="1" nax="100" name="num3"><br>
 <input type="submit" value="提交" name="tijiao">
</form>
    <?php
    if(isset($_POST["tijiao"])){
function sum($num1, $num2, $num3) {
   $sum = $num1 + $num2 + $num3;
   return $sum;
}

$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];

$total = sum($num1, $num2, $num3);

echo "总和为: $total";
        }
    ?>
</body>
</html>

2.3 Running Screenshot

The code is as follows (example):

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/130009213