Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/php_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PHP_FUNCTION(serialize);
PHP_FUNCTION(unserialize);
PHP_FUNCTION(memory_get_usage);
PHP_FUNCTION(memory_get_peak_usage);
PHP_FUNCTION(swap);

PHPAPI void php_var_dump(zval *struc, int level);
PHPAPI void php_var_export(zval *struc, int level);
Expand Down
38 changes: 38 additions & 0 deletions ext/standard/tests/general_functions/swap_01.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
swap function test
--FILE--
<?php
$value1 = 2;
$value2 = 'abc';

var_dump(swap($value1, $value2));
var_dump($value1);
var_dump($value2);

class obj1 {
public $value = 2;
}

class obj2 {
public $value = 'abc';
}

$value1 = new obj1;
$value2 = new obj2;

var_dump(swap($value1, $value2));
var_dump($value1->value);
var_dump($value2->value);

var_dump(swap());
var_dump(swap($value1, $value2, $value2));
?>
--EXPECT--
bool(true)
string(3) "abc"
int(2)
bool(true)
string(3) "abc"
int(2)
bool(false)
bool(false)
21 changes: 21 additions & 0 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,27 @@ PHP_FUNCTION(memory_get_peak_usage) {
}
/* }}} */

/* {{{ proto bool swipe(mixed var1, mixed var2)
Swaps the value of var1 and var2. */
PHP_FUNCTION(swap)
{
zval temp, *val1, *val2;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &val1, &val2) == FAILURE) {
RETURN_FALSE;
}

ALLOC_INIT_ZVAL(temp);

ZVAL_COPY_VALUE(temp, val1);
ZVAL_COPY_VALUE(val1, val2);
ZVAL_COPY_VALUE(val2, temp);

RETURN_TRUE;

}
/* }}} */

/*
* Local variables:
* tab-width: 4
Expand Down