>  기사  >  백엔드 개발  >  PHP에서 스와핑

PHP에서 스와핑

王林
王林원래의
2024-08-29 13:12:08600검색

이 기사에서는 PHP에서 숫자를 바꾸는 방법을 배웁니다. 스와핑을 정의하는 방법, 두 개의 숫자를 바꾸는 코딩 방법, 두 개 이상의 숫자와 세 개의 숫자를 바꾸는 방법, 임시 변수가 있거나 없는 숫자를 바꾸는 방법 등을 알아봅니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

먼저 정의부터 살펴보겠습니다.

“PHP에서 스와핑은 값을 교환하는 것으로 정의되는 용어입니다.”

두 숫자 교환은 임시 변수를 사용하거나 사용하지 않고 두 값을 교환하는 프로세스입니다. 이 예제들이 스와핑의 개념을 배우고자 하는 모든 프로그래머에게 도움이 되기를 바랍니다.

PHP에서 두 숫자를 바꾸는 방법은 무엇입니까?

번호를 바꾸는 방법에는 두 가지가 있습니다. 이 숫자는 숫자 값을 담고 있습니다.

  • 임시 변수로 두 숫자를 교환합니다.
  • 임시 변수 없이 두 숫자를 교환합니다.

값이 10인 변수가 하나 있다고 가정합니다.

숫자1 = 10 ;

그리고 값이 20인 다른 변수는

숫자2 = 20;

이 두 숫자를 바꾸면 결과는 다음과 같습니다.

번호1 =20

숫자2= 10

이는 세 번째 임시 변수를 사용하거나 임시 변수 없이도 가능합니다. +, -, * 및 / 연산자를 사용하여 두 숫자를 교환할 수 있습니다.

1. 임시 변수로 두 숫자 바꾸기

코드:

<?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에서 스와핑

2. 임시 변수 없이 두 숫자 바꾸기

코드:

<?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에서 스와핑

3. list()와 array() 같은 함수를 사용하여 두 숫자 교환

코드:

<?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;
?>

출력:

PHP에서 스와핑

PHP에서 세 개의 숫자를 바꾸는 방법은 무엇입니까?

번호를 바꾸는 방법에는 두 가지가 있습니다. 이 숫자는 숫자 값을 담고 있습니다.

  • 임시 변수로 세 개의 숫자를 교환합니다.
  • 임시 변수 없이 세 개의 숫자를 교환합니다.

1. 임시 변수를 사용하여 세 숫자 바꾸기

이제 두 숫자 바꾸기를 배웠으니 세 숫자 바꾸기도 배웠습니다. 다음 예는 임시(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;
?>

출력:

PHP에서 스와핑

2. 임시 변수를 사용하지 않고 세 개의 숫자 바꾸기

논리에서는 총합을 계산하여 $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에서 스와핑

이 기사가 숫자 교환을 배우고자 하는 모든 프로그래머에게 도움이 되기를 바랍니다. 이 기사에는 적절한 예와 함께 두 숫자와 세 숫자의 교환이 모두 포함되어 있습니다. 이러한 예를 연습해 보면 개념을 이해하는 데 도움이 되고 논리도 기억하는 데 도움이 됩니다.

위 내용은 PHP에서 스와핑의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP의 별 패턴다음 기사:PHP의 별 패턴