[英]Why does the same code for Java and PHP not work?
I wrote a program in PHP and Java which generates all possible words with length 2. I used recursion. 我用PHP和Java编写了一个程序,它生成所有可能的长度为2的单词。我使用了递归。 Why does the program work in Java but not in PHP?
为什么程序在Java中工作而在PHP中不工作? It's the same code.
这是相同的代码。
Java Java的
package com.company;
public class Words {
public static void main(String[] args) {
generate("", 2);
}
static void generate(String prefix, int remainder) {
if (remainder == 0) {
System.out.println(prefix);
} else {
for (char c = 'A'; c <= 'Z'; c++) {
generate(prefix + c, remainder - 1);
}
}
}
}
PHP PHP
generate('', 2);
function generate($prefix, $remainder)
{
if ($remainder == 0) {
echo "$prefix\n";
} else {
for ($c = 'A'; $c <= 'Z'; $c++) {
generate($prefix . $c, $remainder - 1);
}
}
}
$c
has string type in PHP. $c
在PHP中有字符串类型。 The ++
operator works differently for it compared to numbers. 与数字相比,
++
运算符的工作方式不同。
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. 当处理字符变量而不是C的算术运算时,PHP遵循Perl的约定。 For example, in PHP and Perl
$a = 'Z'; $a++;
例如,在PHP和Perl
$a = 'Z'; $a++;
$a = 'Z'; $a++;
turns $a
into 'AA'
, while in C a = 'Z'; a++;
将
$a
变为'AA'
,而在C a = 'Z'; a++;
a = 'Z'; a++;
turns a into '['
(ASCII value of 'Z' is 90, ASCII value of '[' is 91) . 将a转换为
'['
('Z'的ASCII值为90,'['的ASCII值为91) 。 Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (az, AZ and 0-9) are supported. 请注意,字符变量可以递增但不会递减,即使只支持纯ASCII字母和数字(az,AZ和0-9)。 Incrementing/decrementing other character variables has no effect, the original string is unchanged.
递增/递减其他字符变量无效,原始字符串不变。
Source: http://php.net/manual/en/language.operators.increment.php 资料来源: http : //php.net/manual/en/language.operators.increment.php
Change your loop from 改变你的循环
for ($c = 'A'; $c <= 'Z'; $c++) {
to 至
foreach (range('A', 'Z') as $c){
============================ ============================
EDIT : 编辑 :
Sorry, I tried to find an official document about this, but I cannot. 对不起,我试图找到一份关于此的官方文件,但我不能。 So I'll try to explain a little
所以我会尝试解释一下
In php, when you compare 2 strings, the system wil try to compare the first character and then the second ..... the compare operator will be stop when the first different character appear 在php中,当你比较2个字符串时,系统会尝试比较第一个字符然后第二个字符.....比较运算符将在第一个不同字符出现时停止
Example 例
$a = 'ABCDEZ';
$b = 'ABCEZZ';
String $b greater than $a because the value ABC in the beginning of $a and $b is the same but E (the value in index 3 of string $b) greater than D (the value in index 3 of string $a), and they don't need to compare other characters 字符串$ b大于$ a,因为$ a和$ b开头的值ABC相同,但E(字符串$ b的索引3中的值)大于D(字符串$ a的索引3中的值) ,他们不需要比较其他角色
In this question, the original loop is 在这个问题中,原始循环是
for ($c = 'A'; $c <= 'Z'; $c++) {
it's fine until $c = 'Z', but after that $c++ will be 'AA' and when php start to compare it, 'AA' < 'Z' and the loop continue, that's why I changed the loop to 它是好的,直到$ c ='Z',但在那之后$ c ++将是'AA'并且当php开始比较它时,'AA'<'Z'并且循环继续,这就是为什么我将循环更改为
foreach (range('A', 'Z') as $c){
To make it work 使它工作
I hope you enjoy it and happy with my explain, sorry because I don't see any document about this, it's just how php work and I know it 我希望你喜欢它,并对我的解释感到高兴,抱歉,因为我没有看到任何关于这个的文档,它只是php工作的方式,我知道它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.