Understanding why php increments characters the way it does - php

PHP Manual states:
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).
If PHP converts the characters to ascii values (assuming) when dealing with arithmetic operations on characters, should it not print '[' instead of AA? Why and how does PHP increment characters the way it does?

If PHP converts the characters to ascii values (assuming) when dealing with arithmetic operations on characters ...
Your assumption is false, since it treats "0" and 0 as equal, instead of "0" and 48.
$ php
<?php
echo "0" == 0 ; echo "\n";
echo "0" == 48 ; echo "\n";
1
​

Related

strrev() Function and Integers in PHP

I've seen a function that checks if a number is a palindrome.
class Solution {
/**
* #param Integer $x
* #return Boolean
*/
function isPalindrome($x) {
return $x >= 0 && $x === (int)strrev($x);
}
}
My question is: while the strrev function should accept a string as an argument, here it takes an integer and works fine. How is this possible?
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.
$s = 'W';
for ($n=0; $n<6; $n++) {
echo ++$s . PHP_EOL;
}
The above example will output:
X
Y
Z
AA
AB
AC

Alphabetical counter

Hello PHP professionals,
With:
$count = 0; // start count
$count = $count +1; // addition value
i can define an automatic counter per text paragraph.
At each paragraph the value will be incremented automatically:
1st paragraph: echo $count++;. (results is 1.) text
2nd paragraph: echo $count++;. (results is 2.) text
3rd paragraph: echo $count++;. (results is 3.) text
4th paragraph: echo $count++;. (results is 4.) text
etc.
this works without errors.
Question:
How could it be achieved that not digits 1 to x are output, but alphabetically from A to Z?
Try the following:
$letter = chr($count + 65);
If you have more than 26 paragraphs use modulo
Instead of starting with 0, you can start with 'A' or "A". The increment operator in PHP works for letters as well:
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.
In your case you simply write $count = 'A'; and use echo $count++; as usual.
The chr() function transfers the passed in integer into the according ACSII character.
So you can do this:
$count = 1;
echo chr(65 + $count++); // 'A' == 65
echo chr(65 + $count++);
echo chr(65 + $count++);
It work fine, and for small letters it works with:
echo chr(97 + $count++); // 'a' == 97
Thanks for help.

Incrementing chars in for loop works, decrementing does not?

So I was doing some exercises and ran across this code (which produces "1. Item A", "2. Item B", etc ):
echo "\n<ol>";
for ($x='A'; $x<'G'; $x++){
echo "<li>Item $x</li>\n";
}
echo "\n</ol>";
Curious, I attempted to do the reverse (which produces an infinite loop of Zs):
echo "\n<ol>";
for ($x = 'Z'; $x > 'M'; $x--){
echo "<li>Item $x</li>\n";
}
echo "\n</ol>";
What have I missed here?
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.
from PHP manual link

Why i am not getting correct output.(php)

<?php
$count='a';
for($i=1;$i<=6;$i++)
{
for($j=1;$j<=(7-$i);$j++)
{
echo $count--;
}
echo "<br/>";
}
?>
count++ is working correctly if i set count='a'. but count-- is not working.
what is the reason for it.
Quoting from the friendly manual
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.
(my emphasis)
Try this:
$count='a';
for($i=1;$i<=6;$i++)
{
for($j=1;$j<=(7-$i);$j++)
{
charMinus($count);
echo $count;
}
echo "<br/>";
}
function charMinus(&$char) {
$ascii=ord($char);
$ascii==97 ? $ascii=123;
$char=chr($ascii-1);
}

Why 2a0++ equals 2a1, BUT 2e0++ equals 3?

Code:
$a = "2c0";
for($i = 0; $i < 25; $i++) {
print "$a ";
$a++;
}
Output:
2c0
2c1
2c2
2c3
2c4
2c5
2c6
2c7
2c8
2c9
2d0
2d1
2d2
2d3
2d4
2d5
2d6
2d7
2d8
2d9
2e0
3
4
5
6
Why, php?
2e0 is interpreted as 2 * 10^0 (see PHP exponentiation) which is 2. The next value would therefore be 3.
From the manual:
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.
Using the ++ operator on "2c0" causes PHP to increment the string value, thus "2c1". "2e0" is treated as scientific notation, i.e., 2 * 10^0 = 2, so incrementing that gives 3.
See this blog post: http://blog.rstack.cc/post/2a0_misunderstanding - it explains the problem and links the php docs for reference.

Categories