2010-09-30 57 views
0

我有以下代码:PHP变量太多写

require("class.XMLHttpRequest.php"); 
function hot($news){ 
    $url="https://localhost/search.aspx?search=".$news.""; 
$ajax=new XMLHttpRequest(); 
$ajax->setRequestHeader("Cookie","Cookie: host"); 
$ajax->open("GET",$url,true); 
$ajax->send(null); 
if($ajax->status==200){ 
    $rHeader=$ajax->getResponseHeader("Set-Cookie"); 
    if(substr_count($rHeader, "Present!")>0) { return true; } 
}else{ return false; } 
} 

$content1= hot("britney") ? "britney found" : ""; 
$content2= hot("gaga") ? "gaga found" : ""; 
$content3= hot("carol") ? "carol found" : ""; 

$filename = 'result.txt'; 
$handle = fopen($filename, 'a'); 
fwrite($handle, "$Content1\r\n"); 
fwrite($handle, "$Content2\r\n"); 
fwrite($handle, "$Content3\r\n"); 
fwrite($handle, "$Content4\r\n"); 
fclose($handle); 

我想缩短脚本cuz我有很多$ ContentN变量 也许类似的foreach?

回答

3

我会做这样的:

$celebrities = array('britney','gaga','carol'); 
$filename = 'result.txt'; 
$handle = fopen($filename, 'a'); 

foreach($celebrities as $celebrity) 
{ 
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); } 
} 

fclose($handle); 

如果你需要更多的名人,只是将它们添加到阵列。

+0

我在这两种情况下都有空格 – adam 2010-09-30 13:05:31

+0

是的,对不起。但在他的情况下,测试更容易。 'if(hot($ celebrity))fwrite($ handle,“{$ celebrity} found \ r \ n”);'不再有空格。 – 2010-09-30 13:07:51

+0

如果你不想空行,它更容易...更新我的答案。 (谢谢Loïc) – Mischa 2010-09-30 13:15:41

2

这样的事情将是非常接近你的实际代码,但

for($i = 1 ; $i <= 4 ; $i++) 
fwrite($handle, "${Content$i}\r\n"); 

它使用可变的变量可能不被recomanded:http://php.net/manual/en/language.variables.variable.php

这里不是最好的解决办法:你为什么不只需使用一个数组?

$content[1]= hot("britney") ? "britney found" : ""; 
$content[2]= hot("gaga") ? "gaga found" : ""; 
$content[3]= hot("carol") ? "carol found" : ""; 
for($i = 1 ; $i <= 4 ; $i++) 
fwrite($handle, $Content[$i]."\r\n"); 

或者以前更好,使用captaintokyo的解决方案,因为你可能不希望在文本文件中的空行。

+1

-1建议变量变量 +2暗示的数组 – mattbasta 2010-09-30 12:53:27

+0

这就是为什么我建议他使用数组。存在变量变量,结果更接近他的使用数组的代码:我首先尝试尽可能接近他的代码,然后我建议使用数组,这是在这里使用它的好方法。 – 2010-09-30 12:55:05

+0

是的!有用!非常感谢 – adam 2010-09-30 12:58:13

0

下面是一个简短的代码重构你。更新的变量名称,数组中保存的趋势主题以及您询问的foreach。代码未经测试。

require("class.XMLHttpRequest.php"); 

$result_filename = 'result.txt'; 

$hot_topics = array(
    'britney', 
    'gaga', 
    'carol' 
); 

$handle = @fopen($result_filename, 'a+'); 
if (!$handle) { 
    exit("Unable to open $result_filename"); 
} 

foreach($hot_topics as $topic) { 
    if (is_hot($topic)) { 
    fwrite($handle, "$topic found\r\n"); 
    } 
} 

fclose($handle); 

exit("\ncomplete"); 

function is_hot($news) { 
    $url = "https://localhost/search.aspx?search=".$news; 

    $ajax = new XMLHttpRequest(); 
    $ajax->setRequestHeader("Cookie", "Cookie: host"); 
    $ajax->open("GET", $url, true); 
    $ajax->send(null); 

    if ($ajax->status == 200) { 
    $rHeader = $ajax->getResponseHeader("Set-Cookie"); 
    if (substr_count($rHeader, "Present!") > 0) { 
     return true; 
    } 
    } 
    return false; 
} 
+0

它打印注意到文件 – adam 2010-09-30 13:21:32

+0

尝试在foreach循环内添加一个打印$ topic语句,首先在if(is_hot())之前,然后在该条件之内以确保它正常工作。之后,尝试在is_hot()函数中添加print $ rHeader。这会让你看到返回的信息。是否有机会不写任何文件,因为没有任何内容是“热”的? – Craig 2010-09-30 13:36:41