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.
Related
Given any character from a to z, what is the most efficient way to get the next letter in the alphabet using PHP?
The most efficient way of doing this in my opinion is to just increment the string variable.
$str = 'a';
echo ++$str; // prints 'b'
$str = 'z';
echo ++$str; // prints 'aa'
As seen incrementing 'z' give 'aa' if you don't want this but instead want to reset to get an 'a' you can simply check the length of the resulting string and if its >1 reset it.
$ch = 'a';
$next_ch = ++$ch;
if (strlen($next_ch) > 1) { // if you go beyond z or Z reset to a or A
$next_ch = $next_ch[0];
}
It depends on what you want to do when you hit Z, but you have a few options:
$nextChar = chr(ord($currChar) + 1); // "a" -> "b", "z" -> "{"
You could also make use of PHP's range() function:
$chars = range('a', 'z'); // ['a', 'b', 'c', 'd', ...]
Well, it depends what exactly you want to do with the "edge cases". What result do you expect when the character is z or Z? Do you want the next letter of the same case, or just the next letter, period?
Without knowing the answer to that, for the very basic case, you can just do this:
$next_character = chr(ord($current_character) + 1);
But when you're at Z this will give you [, and z will give you {, according to ASCII values.
Edited as per comment:
If you need the next character of the same case, you can probably just add simple checks after the line above:
if ($next_character == '[')
$next_character = 'A';
else if ($next_character == '{')
$next_character = 'a';
These are very simple operations, I really wouldn't worry about efficiency in a case like this.
How about using ord() and chr()?
<?php
$next = chr(ord($prev)+1);
?>
Since I only care about lowercase characters in this case, I'll use the following code, based on the answers posted here:
function nextLetter(&$str) {
$str = ('z' === $str ? 'a' : ++$str);
}
Thanks for the help, guys!
$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);
Nice and easy :-)
Create an array of all letters, search for existing letter and return its next letter. If you reach the last letter return first letter.
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
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.
I need to invent my own conversion function because of some encoding issue.
I decided that for now I will create a conversion table for my characters.
I want to know how I can possibly do an operation like the following C code to print out characters 'a' to 'z':
char a='a';
for(i=0;i<26;i++){
printf("%c",a);
}
How can I do that (incrementing characters value by value) in PHP ?
Variables containing characters can be incremented in PHP exactly as in C:
<?php
for ($i = 'a'; $i < 'z'; ++$i) echo $i;
Additionally, PHP allows incrementing of strings:
$x = "abc";
echo ++$x; # abd
You can also use range:
echo implode(range('a', 'z')); # abcdef....
Finally, you can convert between character and numeric ASCII index via chr and ord. respectively.
Given any character from a to z, what is the most efficient way to get the next letter in the alphabet using PHP?
The most efficient way of doing this in my opinion is to just increment the string variable.
$str = 'a';
echo ++$str; // prints 'b'
$str = 'z';
echo ++$str; // prints 'aa'
As seen incrementing 'z' give 'aa' if you don't want this but instead want to reset to get an 'a' you can simply check the length of the resulting string and if its >1 reset it.
$ch = 'a';
$next_ch = ++$ch;
if (strlen($next_ch) > 1) { // if you go beyond z or Z reset to a or A
$next_ch = $next_ch[0];
}
It depends on what you want to do when you hit Z, but you have a few options:
$nextChar = chr(ord($currChar) + 1); // "a" -> "b", "z" -> "{"
You could also make use of PHP's range() function:
$chars = range('a', 'z'); // ['a', 'b', 'c', 'd', ...]
Well, it depends what exactly you want to do with the "edge cases". What result do you expect when the character is z or Z? Do you want the next letter of the same case, or just the next letter, period?
Without knowing the answer to that, for the very basic case, you can just do this:
$next_character = chr(ord($current_character) + 1);
But when you're at Z this will give you [, and z will give you {, according to ASCII values.
Edited as per comment:
If you need the next character of the same case, you can probably just add simple checks after the line above:
if ($next_character == '[')
$next_character = 'A';
else if ($next_character == '{')
$next_character = 'a';
These are very simple operations, I really wouldn't worry about efficiency in a case like this.
How about using ord() and chr()?
<?php
$next = chr(ord($prev)+1);
?>
Since I only care about lowercase characters in this case, I'll use the following code, based on the answers posted here:
function nextLetter(&$str) {
$str = ('z' === $str ? 'a' : ++$str);
}
Thanks for the help, guys!
$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);
Nice and easy :-)
Create an array of all letters, search for existing letter and return its next letter. If you reach the last letter return first letter.