2010-11-10 125 views
0

我想知道我到底有多少成功和失败。我想用阵列功能,但我不知道如何从这里继续:php - 数组元素

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

count($pass); 

这一步是运行的每个assert函数是这样的:

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; 

回答

2

你不是用数组做任何事情,除了计数,那么,为什么不只是使用一个整数?

$pass= 0; 
$failed = 0; 


public function array_internal($the_string) 

    global $pass, $failed; 

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

} 
+0

我认为'array_internal'被多次调用,因为在功能上没有循环。全局变量会更好。 - 我看到你已经更新了你的答案;) – Harmen 2010-11-10 16:17:40

+0

@哈曼是的我把初始化了,但原来的问题也是每次重置它。我同意他们应该是全球性的或以其他方式处理。 – Fosco 2010-11-10 16:20:13

+0

你能向我解释一下如何做全球变量吗? (并且是的,'array_internal'被多次调用) – Ronny 2010-11-10 16:27:56

2

你为什么不只是使用全局变量作为$pass$fail,您可以通过$pass++$fail++增加?

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

我试过这个,但$ pass最后是0。 – Ronny 2010-11-10 16:38:51

+0

@ronny:那么你在这个'if(strstr($ the_string,“Success”))中有错误) ' – Svisstack 2010-11-10 17:48:39

1

$pass[] = +1创建$pass阵列中一个新的密钥值对,并添加到1新值。这可能不是你想要做的。查看你想做什么的其他答案。

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

创建新的数组实例。您的功能array_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());