在本文中,我们将学习 PHP 中的数字交换。我们将学习如何定义交换、如何编写代码来交换两个数字、如何交换两个以上数字和三个数字以及如何在有或没有临时变量的情况下交换数字等等。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
让我们先从定义开始。
“PHP 中的交换是一个定义为值交换的术语。”
交换两个数字是使用或不使用临时变量交换两个值的过程。我希望这些例子对所有想要学习交换概念的程序员有所帮助。
交换号码有两种方法。这些数字包含数值。
假设我们有一个变量的值为 10,
数字1 = 10;
另一个变量的值为 20,
数字2 = 20;
交换这两个数字,结果应该是,
数字1 =20
数字2= 10
这可以通过使用第三个临时变量来实现,也可以不使用临时变量。可以使用 +、-、* 和 / 运算符交换两个数字。
代码:
<?PHP // Example of swapping of two numbers using a temporary variable // Declaring two variables $num1 = 100; $num2 = 200; // using echo statement print the variables before swapping two numbers echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // declaring temporary variable to be zero $temp = 0; // performing swap of numbers $temp = $num1; $num1 = $num2; $num2 = $temp; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
输出:
代码:
<?php // Example of swapping of two numbers without using a temporary variable // Declaring two variables $num1 = 100; $num2 = 200; // using echo statemnt print the variables before swapping two numbers echo "<br>"."Swap done without using temparory variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // performing swap of numbers $num1 = $num1 - $num2; $num2 = $num1 + $num2; $num1 = $num2 - $num1; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
输出:
代码:
<?php // Example of swapping of two numbers using list() with array() // Declaring two variables $num1 = 100; $num2 = 200; // using echo statement print the variables before swapping two numbers echo "<br>"."Swap done without using predefined functions"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // performing swap of numbers list($num1, $num2) = array($num2, $num1); //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
输出:
交换号码有两种方法。这些数字包含数值。
现在我们已经学会了两个数字的交换,我们已经学会了三个数字的交换。以下示例演示了临时(temp)变量如何交换三个数字。
代码:
<?php // Example of swapping three numbers using temporary variable // Declaring three variables $num1 = 100; $num2 = 200; $num3 = 300; // using echo statement print the variables before swapping three numbers echo "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // performing swap of numbers //assign first number the total of three numbers $temp = $num1; $num1 = $num2; $num2 = $num3; $num3 = $temp; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
输出:
逻辑是计算总和并将其分配给 $num1 变量。
然后,
计算$num1的值,将该值赋给$num2,
计算$num2的值,将该值赋给$num3,
计算$num3的值,并将这个值再次赋给$num1。
代码:
<?php // Declaring three variables $num1 = 100; $num2 = 200; $num3 = 300; // using echo statement print the variables before swapping three numbers echo "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // performing swap of numbers //assign first number the total of three numbers $num1 = $num1 + $num2 + $num3; $num2 = $num1 - ($num2 + $num3); $num3 = $num1 - ($num2 + $num3); $num1 = $num1 - ($num2 + $num3); //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
输出:
希望这篇文章对所有希望学习数字交换的程序员有所帮助。本文提供了两个和三个数字的交换以及适当的示例。如果练习这些示例,将帮助您理解概念并帮助您记住逻辑。
以上是PHP 中的交换的详细内容。更多信息请关注PHP中文网其他相关文章!