» »

Decrementing/Incrementing operatorji PHP

Decrementing/Incrementing operatorji PHP

PrihajaNodi ::

   
$d = 'A8';
for ($n=0; $n<5; $n++) 
    echo ++$d /
    

Od kje pride A9 B0, B1. Kateri del nakazuje na znak B?

Vesoljc ::

Abnormal behavior of abnormal brain makes me normal...

Ribič ::

Tole je meni malo čudno. Kaj imaš v tisti spremenljivki $d? Meni zgleda kot nek tekstovni string, ker je v narekovajih.
Če bi imel heksadecimalno valuto, potem bi zaporedje moralo iti A8, A9, AA, AB, AC, AD, AE, AF, B0, B1, itd.
Vse ribe so mi pobegnile!

Miha 333 ::

RTFM: http://php.net/manual/en/language.opera... in http://perldoc.perl.org/perlop.html#Aut... (pravilo je isto kot pri Perl).

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

The above example will output:

== Characters ==
X
Y
Z
AA
AB
AC
== Digits ==
A9
B0
B1
B2
B3
B4
A09
A10
A11
A12
A13
A14

Incrementing or decrementing booleans has no effect.


The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*\z/ , the increment is done as a string, preserving each character within its range, with carry:

print ++($foo = "99"); # prints "100"
print ++($foo = "a0"); # prints "a1"
print ++($foo = "Az"); # prints "Ba"
print ++($foo = "zz"); # prints "aaa"

Zgodovina sprememb…

  • spremenilo: Miha 333 ()

mojster_joni ::

Ribič je izjavil:

Tole je meni malo čudno. Kaj imaš v tisti spremenljivki $d? Meni zgleda kot nek tekstovni string, ker je v narekovajih.
Če bi imel heksadecimalno valuto, potem bi zaporedje moralo iti A8, A9, AA, AB, AC, AD, AE, AF, B0, B1, itd.

govoriš o phpju kjer je konsistentnost na 0

Miha 333 ::

Tudi v PHPju je konsistentnost, samo moraš vedet, s čim imaš opravka in kaj hočeš.

string: $a = 'AF';
integer (v hex obliki): $a = 0x1A;

Zgoraj je govora izključno o stringih, prilepil pa sem pravilo iz navodil, kaj se z njimi dogaja, če jih povečuješ in nima veze s hexadecimalnimi števili. Vse je v navodilih.

technolog ::

String increment je feature, ki ga poznajo tudi drugi programski jeziki, npr. Ruby:

"z".next # vrne string "aa"

@Vesoljc: Pojma nimas.

PrihajaNodi ::

Porabljeno kar nekaj casa kaj ima to veza s hexadecimalnimi stevili.
Hvala

Zgodovina sprememb…

mojster_joni ::

da je php zmazek od jezika in bolj hack kot ne

Miha 333 ::

PHP je to "podedoval" od Perla.

mojster_joni ::

jp pa tudi niso se ravno posvetili popravilom najbolj očitnih bedarij