reference variable &
In PHP, there is no concept of a "reference variable" in the same way as in some other programming languages like C++. However, PHP does support references in the form of reference assignment and pass-by-reference for function arguments.
Here's how you can use references in PHP:
1. **Reference Assignment**:
You can create a reference to a variable using the `&` symbol. When you assign a variable by reference, it means that both variables refer to the same data in memory. Any changes made to one variable will affect the other.
```php
$original = 10;
$reference = &$original;
$reference = 20; // Changes $original as well
echo $original; // Output: 20
```
2. **Passing by Reference**:
In PHP, function arguments are passed by value by default, which means a copy of the variable's value is passed into the function. However, you can pass a variable by reference by using the `&` symbol in both the function definition and the function call. This allows the function to modify the original variable.
```php
function multiplyByTwo(&$value) {
$value *= 2;
}
$num = 5;
multiplyByTwo($num);
echo $num; // Output: 10
```
Note that while passing by reference can be useful, it should be used judiciously as it can make code harder to understand and debug.
3. **Returning by Reference**:
You can also return a reference from a function. This can be used to modify variables outside of the function scope.
```php
function &getArrayItem(&$array, $index) {
return $array[$index];
}
$myArray = [1, 2, 3];
$item = &getArrayItem($myArray, 1);
$item = 10; // Modifies $myArray to [1, 10, 3]
```
In PHP, references can be powerful, but they should be used with caution to avoid unexpected side effects and make your code more readable and maintainable.
Number | Title | Author | Date | Votes | Views |
40 |
php formatter
siwon
|
2024.11.26
|
Votes 0
|
Views 389
|
siwon | 2024.11.26 | 0 | 389 |
39 |
html center 중앙정렬 tailwind
siwon
|
2024.07.27
|
Votes 0
|
Views 762
|
siwon | 2024.07.27 | 0 | 762 |
38 |
dropdown menu alpinejs 사용 버전
siwon
|
2024.04.30
|
Votes 0
|
Views 824
|
siwon | 2024.04.30 | 0 | 824 |
37 |
dropdown menu 간단 버전
siwon
|
2024.04.30
|
Votes 0
|
Views 785
|
siwon | 2024.04.30 | 0 | 785 |
36 |
The Standard PHP Library (SPL) is a collection of classes and interfaces that provide core functionality to PHP developers.
siwon
|
2023.10.24
|
Votes 0
|
Views 1130
|
siwon | 2023.10.24 | 0 | 1130 |
35 |
session 과 쿠키
siwon
|
2023.10.24
|
Votes 0
|
Views 807
|
siwon | 2023.10.24 | 0 | 807 |
34 |
Late Static Binding (LSB):메서드 내부에서 현재 클래스의 정적 메서드 또는 프로퍼티를 호출할 때 사용
siwon
|
2023.10.24
|
Votes 0
|
Views 754
|
siwon | 2023.10.24 | 0 | 754 |
33 |
PHP 예외 처리(Exception Handling)
siwon
|
2023.10.10
|
Votes 0
|
Views 967
|
siwon | 2023.10.10 | 0 | 967 |
32 |
php exception
siwon
|
2023.10.10
|
Votes 0
|
Views 1241
|
siwon | 2023.10.10 | 0 | 1241 |
31 |
예외(Exception)를 처리하기 위해 try...catch 블록을 사용하는 방법
siwon
|
2023.10.10
|
Votes 0
|
Views 829
|
siwon | 2023.10.10 | 0 | 829 |
30 |
Preserving Parent Class Functionality in overriding
siwon
|
2023.09.26
|
Votes 0
|
Views 663
|
siwon | 2023.09.26 | 0 | 663 |
29 |
oop 세부항목
siwon
|
2023.09.26
|
Votes 0
|
Views 660
|
siwon | 2023.09.26 | 0 | 660 |
28 |
method chaining
siwon
|
2023.09.25
|
Votes 0
|
Views 794
|
siwon | 2023.09.25 | 0 | 794 |
27 |
interface implements
siwon
|
2023.09.19
|
Votes 0
|
Views 708
|
siwon | 2023.09.19 | 0 | 708 |
![]()
siwon
|
2023.10.24
|
Votes 0
|
Views 589
|
siwon | 2023.10.24 | 0 | 589 | |
26 |
abstract class : 부모 class로 사용되며 자식(extends 한)에게 abstract method를 강제함(그들만의 방식으로)
siwon
|
2023.09.19
|
Votes 0
|
Views 661
|
siwon | 2023.09.19 | 0 | 661 |
25 |
isset() / unset()
siwon
|
2023.09.18
|
Votes 0
|
Views 733
|
siwon | 2023.09.18 | 0 | 733 |
24 |
magic methods-어떤 상황이 되면 call 하지 않아도 자동으로 실행되는 메소드
siwon
|
2023.09.18
|
Votes 0
|
Views 733
|
siwon | 2023.09.18 | 0 | 733 |
23 |
MD(markdown) file
siwon
|
2023.09.12
|
Votes 0
|
Views 740
|
siwon | 2023.09.12 | 0 | 740 |
22 |
usort
siwon
|
2023.08.30
|
Votes 0
|
Views 676
|
siwon | 2023.08.30 | 0 | 676 |
21 |
closure=unanimous function
siwon
|
2023.08.30
|
Votes 0
|
Views 759
|
siwon | 2023.08.30 | 0 | 759 |
![]()
siwon
|
2023.10.24
|
Votes 0
|
Views 1417
|
siwon | 2023.10.24 | 0 | 1417 | |
20 |
reference variable &
siwon
|
2023.08.29
|
Votes 0
|
Views 748
|
siwon | 2023.08.29 | 0 | 748 |
![]()
siwon
|
2023.08.30
|
Votes 0
|
Views 769
|
siwon | 2023.08.30 | 0 | 769 | |
19 |
PHP 변수 : 스칼라(Scalar), 복합(Composite), 그리고 리소스(Resource)
siwon
|
2023.08.22
|
Votes 0
|
Views 816
|
siwon | 2023.08.22 | 0 | 816 |
18 |
if : vs {}
siwon
|
2023.08.22
|
Votes 0
|
Views 632
|
siwon | 2023.08.22 | 0 | 632 |
17 |
null coalescing operator
siwon
|
2023.08.18
|
Votes 0
|
Views 852
|
siwon | 2023.08.18 | 0 | 852 |
16 |
arrary functions
siwon
|
2023.08.18
|
Votes 0
|
Views 695
|
siwon | 2023.08.18 | 0 | 695 |