PHP - 调用char []数组时遇到问题

时间:2017-09-30 11:41:40

标签: php

 char[]  validChars= {'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 
   'C',
   'D', 'E', 'F', 'G', 'H', 'J', 'K','L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',   

   'U', 'V', 'W', 'X', 'Y', 'Z'}

 bool VerifyKey(string key)
  {
  if (key.Length != 10)
   return false;
   char checkDigit = GenerateCheckCharacter(key.ToUpper().Substring(0,
    9 ));
   return key[9] == checkDigit;
   }
   // Implementation of Luhn Mod N algorithm for check digit.
   char GenerateCheckCharacter(string input)
   {
   int factor = 2;
   int sum = 0;
   int n = validChars.Length;
   // Starting from the right and working leftwards is easier since
  // the initial "factor" will always be "2"
  for (int i = input.Length - 1; i >= 0; i--)
   {
  int codePoint = Array.IndexOf(validChars, input[i]);
  int addend = factor * codePoint;
  // Alternate the "factor" that each "codePoint" is multiplied by
  factor = (factor == 2) ? 1 : 2;
 // Sum the digits of the "addend" as expressed in base "n"
 addend = (addend / n) + (addend % n);
  sum += addend;
  }
 // Calculate the number that must be added to the "sum"
 // to make it divisible by "n"
 int remainder = sum % n;
 int checkCodePoint = (n - remainder) % n;
 return validChars[checkCodePoint];
 }

我正在使用此代码验证字符串,我正在调用validChars,并且在尝试在php中调用上述方法时出现以下错误

解析错误:语法错误,第46行代码中的意外'validChars'(T_STRING)。##

我正在执行类似'validChars('W56HFY6')'的调用;以下行显示上述错误

   char[]  validChars= {'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 
   'C',
   'D', 'E', 'F', 'G', 'H', 'J', 'K','L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',   

   'U', 'V', 'W', 'X', 'Y', 'Z'}

1 个答案:

答案 0 :(得分:1)

这不是一个php数组,但是这个:

$validChars = array('2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K','L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');