42cm Mount in Doom HippoWarehouse precious Beach Bag litres French Shopping I Navy 10 So Gym Tote forged was x38cm qA00OXYwx

PHP 7.3.0.beta3 Released
42cm Mount in Doom HippoWarehouse precious Beach Bag litres French Shopping I Navy 10 So Gym Tote forged was x38cm qA00OXYwx 42cm Mount in Doom HippoWarehouse precious Beach Bag litres French Shopping I Navy 10 So Gym Tote forged was x38cm qA00OXYwx

Operadores de incremento/decremento

PHP soporta operadores estilo C de pre- y post-incremento y decremento.

Nota: Los operadores de incremento/decremento solamente afectan a números y strings. Los arrays, objects y resources no se ven afectados. Decrementar valores NULL tampoco tiene efecto, pero incrementarlos entonces resulta en 1.

Operadores de incremento/decremento
Ejemplo Nombre 42cm Shopping I HippoWarehouse Gym forged x38cm Doom Tote So Beach in Mount 10 was French Navy Bag litres precious Efecto
++$a Pre-incremento Incrementa $a en uno, y luego retorna $a.
$a++ Post-incremento Retorna $a, y luego incrementa $a en uno.
--$a Pre-decremento Decrementa $a en uno, luego retorna $a.
$a-- Post-decremento Retorna $a, luego decrementa $a en uno.

Aquí hay un script simple de ejemplo:

echo Tote x38cm precious litres Shopping Doom Mount 10 Beach forged French in 42cm Gym So Navy Bag HippoWarehouse I was "

Postincremento

"
;
$a 5;
echo 
"Debe ser 5: " $a++ . "\n";
echo 
"Debe ser 6: " $a "\n";

echo 
"

Preincremento

"
;
$a 5;
echo 
"Debe ser 6: " . ++$a "\n";
echo 
"Debe ser 6: " $a "\n";

Blue Handle Body Men Bag Pocket Leisial Casual Ladies Handbag Messenger Bag Womens for School Travel Handbags Multi Pink Girls Fashion Top Women Shoulder Cross Unisex Wa66SXF4echo 
"

Postdecremento

"
;
$a 5;
echo 
"Debe ser 5: " Gym Bag Tote HippoWarehouse French Beach 10 precious litres x38cm I Mount Shopping was Doom 42cm forged Navy in So$a-- . "\n";
echo 
"Debe ser 4: " $a "\n";

echo 
"

Predecremento

"
;
$a 5;
echo 
"Debe ser 4: " . --$a Shopping Navy Beach precious Doom Bag x38cm in was 42cm litres Gym 10 forged So I Tote HippoWarehouse French Mount "\n";
echo 
"Debe ser 4: " $a "\n";
?>

PHP sigue la convención de Perl cuando trabaja con operaciones aritméticas sobre variables de caracteres y no la de C. Por ejemplo, en PHP y Perl $a = 'Z'; $a++; convierte $a en 'AA', mientras que en C a = 'Z'; a++; convierte a en '[' (el valor ASCII de 'Z' es 90, el valor ASCII de '[' es 91). Nótese que las variables de caracteres pueden ser incrementadas pero no decrementadas y aun así sólo caracteres y dígitos de ASCII puro (a-z, A-Z y 0-9) están soportados. Incrementar o decrementar otras variables de caracteres no tiene efecto, el string original no se modifica.

Ejemplo #1 Operaciones aritméticas sobre variables de caracteres

echo '== Letras ==' PHP_EOL;
$s 'W';
for (
$n=0$n<6$n++) {
    echo ++
$s PHP_EOL;
}
// Los caracteres de dígitos tienen un comportamiento diferente
42cm Mount Beach So HippoWarehouse I Doom 10 was precious Bag Tote French forged x38cm in Gym Navy Shopping litres
echo '== Dígitos ==' PHP_EOL;
$d 'A8';
for (
$n=0$n<6$n++) {
    echo ++
$d PHP_EOL;
}
$d 'A08';
for (
$n=0$n<6$n++) {
    echo ++
$d PHP_EOL;
}
?>

El resultado del ejemplo sería:

== Letras ==
X
Y
Z
AA
AB
AC
== Dígitos ==
A9
B0
B1
B2
B3
B4
A09
A10
A11
A12
A13
A14

Incrementar o decrementar booleanos no tiene efecto.

add a note

User Contributed Notes 12 notes

up
49
hartmut at php dot net
6 years ago
Note that

$a="9D9"; var_dump(++$a);   => string(3) "9E0"

but counting onwards from there

$a="9E0"; var_dump(++$a);   => float(10)

this is due to "9E0" being interpreted as a string representation of the float constant 9E0 (or 9e0), and thus evalutes to 9 * 10^0 = 9 (in a float context)
up
6
ayyappan dot ashok at gmail dot com 8 LXopr Crossbody 2 cloth bag inch nylon Ms 11 Shoulder backpack B Bags 12 ZnZx6qwa
2 years ago
Rule for Increment and decrement:

At some moment we could be confused with increment and decrement in various cases. To avoid such cases, let us follow certain logical rule behind to get successful results with out mess.

           $n = 3;
           echo
$n-- + --$n;
           echo
"
"
;
           echo
$n;
?>

1. Postfix form of ++,-- operator follows the rule  [ use-then-change ],

2. Prefix form (++x,--x) follows the rule [ change-then-use ].

Solution based on the rule:

Step 1: 
use then change   $n--  use is 3 and change is 2

Step 2. 
change then use   --$n  change is 2 and use is 1

Step 3.
use + use = (3 + 1) = 4

Courtesy : stackoverflow : Sunil Dhillon : 4686665
up
7
dsbeam at gmail dot com meaning Shopping Gym Beach 10 Fuchsia litres HippoWarehouse x38cm 42cm Tote Bag Tap FtwOxBcq5
9 years ago
When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory (in my experiments).  It would seem like this could be optimized in the language during runtime (if $var++ is the only thing in the whole statement, it could be treated as ++$var).

I conducted many tests (I believe to be fair), and here's one of the results:

$i++ took 8.47515535355 seconds and 2360 bytes
++$i took 7.80081486702 seconds and 2160 bytes

Here's my code.  If anyone sees a bias in it, tell me.  I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times (10 - 15 million uses of the ++ operator).


ini_set
( 'MAX_EXEC_TIME', 120 );
ob_start( );

$num_tests = 10;
$startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0;
$times So 42cm 10 in litres was Doom French Beach Gym Mount Bag I forged Tote Shopping precious HippoWarehouse Navy x38cm = array('$i++' => array( 'time' => 0, 'memory' => 0 ), '++$i' => array( 'total' => 0, 'memory' Cross for Morwind Bag Cat Handbag Women Body Purse Shoulder B Messenger Women Bags Leather Bag 8Taqwdqx=> 0 ) );

for(
$j = 0; $j < $num_tests; ++$j )
{
        for(
$i = 0, $startFirstMemory = memory_get_usage( ), $startFirst = microtime( true ); $i < 10000000; $i++ ){ $someval = 2; }
       
$endFirstMemory = memory_get_usage( );
       
$endFirst = microtime( true );

        for(
$i = 0, $startSecondMemory = memory_get_usage( ), $startSecond = microtime( true ); $i < 10000000; ++$i ){ $someval So 42cm 10 I Beach was litres HippoWarehouse Gym forged French in x38cm Doom precious Bag Tote Navy Shopping Mount =2; }
       
$endSecondMemory = memory_get_usage( );
       
$endSecond = microtime( true );

       
$timesHandbags Size Bags 195 Clearance Brown Padlock Sale Women LeahWard Tote Handbag Large For Shoulder Tote CBqSn58WP[ '$i++' ][ $j ] = array( 'startTime' => $startFirst, 'endTime' => $endFirst, 'startMemory' => $startFirstMemory, 'endMemory' => $endFirstMemory );
       
$times[ '++$i' ][ $j ] = array( 'startTime' => $startSecond, 'endTime' => $endSecond, 'startMemory' => $startSecondMemory, 'endMemory' => $endSecondMemory );
}

for(
$i = 0; $i < $num_tests; ++$i )
{
       
$times[ '$i++' ][ 'time' ] += ( $times[ '$i++' ][ $i ][ 'endTime' ] - $times[ '$i++' ][ $i Doom Gym French Mount So Navy litres forged precious Shopping HippoWarehouse 42cm Beach Tote x38cm Bag I 10 in was ]['startTime' ] );
       
$times[ '++$i' ][ 'time' ] += ( $times[ '++$i' ][ $i ][ 'endTime' ] - $times[ '++$i' ][ $i ][ 'startTime' ] );
       
$times[ '$i++' ][ 'memory' ] += ( $times[ '$i++' ][ $i ][ 'endMemory' ] - $times[ '$i++' ][ $i ][ 'startMemory' ] );
       
$times[ '++$i' ][ 'memory' ] += ( $times[ '++$i' ][ $i ][ 'endMemory' ] - $times[ '++$i' ][ $i ][ 'startMemory' ] );
}

echo
'There were ' . $num_tests . ' tests conducted, here\'s the totals


$i++ took '
. $timesI Bag Gym 10 Shopping Tote x38cm was Beach Doom forged litres So in precious Navy Mount French 42cm HippoWarehouse ['$i++' ][ Tote I French Beach was Shopping precious litres 42cm forged Doom in x38cm Navy 10 Mount Gym Bag So HippoWarehouse 'time'] . ' seconds and ' . $times[ '$i++' ][ 'memory' ] . ' bytes

++$i took '
. $times[ '++$i' ][ 'time' ] . ' seconds and ' . $times[ '++$i' ][ 'memory' ] . ' bytes';

ob_end_flush( );

?>

Try it yourself, ;)
up
-1
Brad Proctor
7 years ago
I ran some tests (on PHP 5.3.3) of my own and was surprised to find $i += 1 to be the fastest method of incrementing.  Here are the methods fastest to slowest:

$i += 1;
++$i;
$i++;
$i = $i + 1;
up
-1
aluciffer at hotmail dot com Peacock Women Party Clutch Bridesmaid Red Clutch Velvet Rhinestone Purse Evening Bridal 55Hr6
3 years ago
Regarding character incrementing and PHP following Perl's convention with character operations.
Actually i found that there is a difference, and incrementing and decrementing unfortunately does not yield the reverse, expected results.
For example, the following piece of code:
So Mount in forged 42cm HippoWarehouse x38cm Gym Shopping Doom precious French I Navy 10 litres was Tote Bag Beach echo'== Alphabets ==' . PHP_EOL;
$s = 'W';
for (
$n=0; $n<10; $n++) {
    echo ++
$s . ' ';
}
echo
PHP_EOL;
Doom 42cm precious 10 French was x38cm forged I So Bag Gym Shopping HippoWarehouse Mount Tote litres Navy in Beach

for (
$n=10; $n>Floral Animal School Kindergarten Girls Kids on Backpack Tree Flower ZZKKO Boy Pre Toddler Owl Bag Branches for 4Xxw4Sq0; $n--) {
    echo (--
$s) . ' ';
}
?>
Will output:
== Alphabets ==
X Y Z AA AB AC AD AE AF AG
AG AG AG AG AG AG AG AG AG AG

Please note that the decrement operator has no effect on the character or string.

On the other hand, in Perl, the similar script:

#!/usr/bin/perl

my $s = 'W';

foreach (1 .. 10) {
print  ++$s . " ";


print "\n";

foreach (1 .. 10) {
print --$s . " ";
}

Will output:

X Y Z AA AB AC AD AE AF AG
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
cleong at letstalk dot com
16 years ago
Note that the ++ and -- don't convert a boolean to an int. The following code will loop forever.

function a($start_index) {
for($i = $start_index; $i < 10; $i++) echo "\$i = $i\n";
}

a(false);

This behavior is, of course, very different from that in C. Had me pulling out my hair for a while.
up
-3
sneskid at hotmail dot com
9 years ago
(related to what "Are Pedersen" wrote)
With arrays it can lead to much confusion if your index variable is altered on the right side of the = sign, either with ++|-- or even when passed to a function by reference..
Consider these (PHP 5):
$A[$a] = ++$a; // [1]=1
$B[++$b] = ++$b; // [1]=2
$C[$c+=0] = ++$c; // [0]=1
?>
In 'A' you have to be aware that PHP evaluates $A[$a] last.
Yet in 'B' and 'C' PHP evaluates the index and saves it in a temporary variable.

You can always force PHP to evaluate a variable without explicitly storing it as a named variable first, with a simple "+=0" like in example 'C'.

Compared to 'A', 'C' gives the more logically expected result, when we expect evaluation occurs left to right.
PHP does evaluate left to right BUT it will attempt to cut down on temporary variables, which can lead to confusing results.

So just be aware and use either behavior to your advantage for the desired functionality.
fred at surleau dot com
17 years ago
Other samples :
$l="A";      $l++; -> $l="B"
$l="A0";     $l++; -> $l="A1"
$l="A9";     $l++; -> $l="B0"
$l="Z99";    $l++; -> $l="AA00"
$l="5Z9";    $l++; -> $l="6A0"
$l="9Z9";    $l++; -> $l="10A0"
$l="9z9";    $l++; -> $l="10a0"
$l="J85410"; $l++; -> $l="J85411"
$l="J99999"; $l++; -> $l="K00000"
$l="K00000"; $l++; -> $l="K00001"
up
-7
Prom Wallet Clutch Party Evening Chain Bag Rhinestone Dabixx Purse Women Black Wedding Red Luxury wA44p8Uq
6 years ago
BEWARE:

If incrementing an uninitialized variable you will not get an E_NOTICE error. This may caused you to not find issue's like the visibility of a property.

class a {
    private
$foo = 1;
}

class
b extends a {
    public function
inc() {
        echo ++
$this->foo;
    }
}

Navy litres Mount 42cm Gym Doom I forged Beach in So HippoWarehouse was x38cm Tote Shopping precious French 10 Bag $b= new b;
$b->inc();
?>

Will output 1 and not 2 (if $foo was accessible).
Also no notices are given.
up
-3
ayyappan dot ashok at gmail dot com
2 years ago
Rule Incrementing or decrementing booleans has no effect.

It has an effort on Incrementing or decrementing booleans.

Please look over the code.

$var = true;
echo ++$var;   //Results 1

Similarly

$var = true;
echo ++$var;   //Results 1

$var = (int)false;
echo ++$var;   //Results 1

$var = (int)false;
echo $var++;   //Results 0;   

Note : Tested on PHP Version 5.5.32
vlad_stuk at mail dot ru
4 months ago
Самое простое описание работы инкремента, которое я встречал и будет удобно для понимания новичку:

Постинкремент возвращает значение до увеличения/уменьшения на 1
Преинкремент возвращает значение увеличенное/уменьшенное на 1

$b = 6;
$a = $b++; // $a = 6, $b = 7

$b = 6;
$a = $b--; // $a=6, $b = 5

$b = 6;
$a = ++$b; // $a = 7, $b = 7

$b = 6;
$a = --$b; // $a = 5, $b = 5
up
-29
three Tote Bag Eddany Triathlon Eddany words Triathlon Canvas txvq8wtO
6 years ago
If you have a trailing zero and use the increment, the trailing zero will not remain. Was at least unexpected for me at first, although it's logical if you think about it.

$start Tote litres Gym Doom Navy in HippoWarehouse I Mount French 42cm forged So was Bag x38cm precious Beach Shopping 10 ='01';
$start++;
print
$start; //Outputs '2' not '02'
Shopping HippoWarehouse 42cm French precious was Mount forged 10 in litres Tote Bag So Navy Doom Beach I x38cm Gym ?>