php-数组的元素计数

php-数组的元素计数,php,phpunit,Php,Phpunit,我想知道我到底有多少成功和失败。我想使用数组函数,但我不知道如何从这里继续: public function array_internal($the_string) $pass= Array(); $failed = Array(); if(strstr($the_string,"Success")) { $pass[] = +1; } else { $failed[] = +1; } count($pass); 此步

我想知道我到底有多少成功和失败。我想使用数组函数,但我不知道如何从这里继续:

public function array_internal($the_string)
$pass=  Array();
$failed = Array();
  if(strstr($the_string,"Success"))
    {
        $pass[] = +1;
    }
  else
    {
        $failed[] = +1;
    }

count($pass);
此步骤运行每个断言函数,如下所示:

 try {
      $this->assertEquals("off", $this->getValue("page"));
      throw new PHPUnit_Framework_AssertionFailedError("Success");
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
      $this->array_internal($e->toString());
    }
$pass= 0;
$failed = 0;
public function array_internal($the_string)

  if(strstr($the_string,"Success"))
    {
        $pass += 1;
    }
  else
    {
        $failed += 1;
    }

$pass;
函数本身是正常的。我的问题只是柜台

谢谢大家!

编辑 我试着这样做:

 try {
      $this->assertEquals("off", $this->getValue("page"));
      throw new PHPUnit_Framework_AssertionFailedError("Success");
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
      $this->array_internal($e->toString());
    }
$pass= 0;
$failed = 0;
public function array_internal($the_string)

  if(strstr($the_string,"Success"))
    {
        $pass += 1;
    }
  else
    {
        $failed += 1;
    }

$pass;

除了计数之外,你不用数组做任何事情,所以为什么不用整数呢

$pass= 0;
$failed = 0;


public function array_internal($the_string)

  global $pass, $failed;

  if(strstr($the_string,"Success"))
    {
        $pass += 1;
    }
  else
    {
        $failed += 1;
    }

}

除了计数之外,你不用数组做任何事情,所以为什么不用整数呢

$pass= 0;
$failed = 0;


public function array_internal($the_string)

  global $pass, $failed;

  if(strstr($the_string,"Success"))
    {
        $pass += 1;
    }
  else
    {
        $failed += 1;
    }

}

为什么不使用全局变量作为
$pass
$fail
,您可以通过
$pass++
$fail++

使用全局变量作为
$pass
$fail
,这两个变量可以通过
$pass++
$fail++
增加=+1在
$pass
数组中创建一个新的键值对,并将
1
添加到新值中。这可能不是你想做的。查看其他答案了解您想要执行的操作。

$pass[]=+1
$pass
数组中创建一个新的键值对,并将
1
添加到新值中。这可能不是你想做的。查看其他答案了解您想要做什么

public function array_internal($the_string)
$pass=0;
$failed=0;
if (strstr($the_string,"Success"))
{
    $pass += 1;
}
else
{
    $failed += 1;
}
$pass=  Array();
$failed = Array();
创建新的数组实例。函数
array\u internal
的返回值始终为0或1。您也从不使用
$failed

更简单的功能是:

public function array_internal( $the_string )
  $pass = 0; 
  if( strstr( $the_string, "Success" ) )
  {            
      $pass = 1;
  }
  return $pass;
}
正如Harmen所说,您需要使用外部int计数器。与Harmen不同,如果可能的话,我会尽量不使用全局变量,而是使用类变量来限制它的范围

可能是一个类的静态变量,
TestClass
,称为
$passes
,类似于:

TestClass::$passes += $this->array_internal($e->toString());
创建新的数组实例。函数
array\u internal
的返回值始终为0或1。您也从不使用
$failed

更简单的功能是:

public function array_internal( $the_string )
  $pass = 0; 
  if( strstr( $the_string, "Success" ) )
  {            
      $pass = 1;
  }
  return $pass;
}
正如Harmen所说,您需要使用外部int计数器。与Harmen不同,如果可能的话,我会尽量不使用全局变量,而是使用类变量来限制它的范围

可能是一个类的静态变量,
TestClass
,称为
$passes
,类似于:

TestClass::$passes += $this->array_internal($e->toString());

我认为
array\u internal
被多次调用,因为函数中没有循环。全局变量会更好我看你已经更新了答案;)@哈门:是的,我去掉了初始化,但最初的问题是每次都要重置它。我同意他们应该是全球性的或以其他方式处理。你能给我解释一下如何做全球性的VAR吗?(是的,
array\u internal
被多次调用)@Ronny当您在函数外部声明变量时,您需要放入
global$pass在函数内部,以便访问它。我认为
array\u internal
被多次调用,因为函数中没有循环。全局变量会更好我看你已经更新了答案;)@哈门:是的,我去掉了初始化,但最初的问题是每次都要重置它。我同意他们应该是全球性的或以其他方式处理。你能给我解释一下如何做全球性的VAR吗?(是的,
array\u internal
被多次调用)@Ronny当您在函数外部声明变量时,您需要放入
global$pass。@ronny:那么这个
if(strstrstr('u字符串,“Success”))中有错误。
@ronny:那么这个
if(strstrstr('u字符串,“Success”))中有错误。