开发者

php - count elements of array

I want to know in the end how much success and failed i have. I wanted to use the array function but i don't know how to continue from here:

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

count($pass);

This step is running every assert function like this:

 try {
      $this->assertEquals("off", $this->getValue("page"));
      throw new PHPUnit_Framework_AssertionFailedError("Success");
    } catch (PHPUnit_Framework_AssertionFailedError $e) {
      $this->array_internal($e->toString());
    }

The function itself is ok开发者_开发知识库. my problem is only with the counter.

Thank you!

Edit I tried to do something like this:

$pass= 0;
$failed = 0;
public function array_internal($the_string)

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

$pass;


You're not doing anything with the array except counting, so why not just use an integer?

$pass= 0;
$failed = 0;


public function array_internal($the_string)

  global $pass, $failed;

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

}


Why don't you just use global variables as $pass and $fail, which you can increment by $pass++ and $fail++?


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


$pass[] = +1 creates a new key value pair in the $pass array, and adds 1 to the new value. This is probably not what you want to do. See other answers for what you do want to do.


$pass=  Array();
$failed = Array();

Creates new array instances. The return value of your function array_internal will always be 0 or 1. You also never use $failed.

A much simpler function would be:

public function array_internal( $the_string )
  $pass = 0; 
  if( strstr( $the_string, "Success" ) )
  {            
      $pass = 1;
  }
  return $pass;
}

Like Harmen said, you need to use an external int counter. Unlike Harmen, I would try not to use a global variable if possible, but a class variable to limit it's scope.

Maybe a static variable of a class, TestClass, called $passes like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜