一起使用递归函数和Heredoc

时间:2019-05-14 08:10:31

标签: php function recursion heredoc

我想回显大量文本,而不必费心地转义所有字符,这将是调试的噩梦,并希望使用heredoc。

我已阅读文档,无法解决。

const https = require('https');   
const fs = require('fs');

var key=fs.readFileSync(__dirname+'/../ssl/sslforfree_private.key','utf8');  
var cert= fs.readFileSync(__dirname + '/../ssl/sslforfree_certificate.crt','utf8');    
var credentials = {key: key, cert: cert};  
var port = normalizePort(process.env.PORT || '4000');
app.set('port', port);

var server = https.createServer(credentials,app);

并使用function MySwal($items, $k){ if ($k!=0) $k = $k+1; if ($items[$k] != "end") { //if ($items != "end") { echo <<<MySWALSTUFF_A <p> at {$k} : processing {$items[$k]} . next item is {$items[$k+1]} MySWALSTUFF_A; //include this later echo MySwal($items, ($k+1)); //recursive function } else { //else for if item is !=end echo "<HR><h3>This is the end, beautiful friend... </h3>"; }//if for item != end ends here :-) } 进行调用,其中数组的最后一项是“结束”

现在,递归在第1次之后不再显示该函数的输出。如果MySwal ($myarray, 0)$myarray,则上面的heredoc仅对“一个”执行。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为您在函数中增加了k

if ($k!=0) $k = $k+1;

以及在函数调用中:

echo MySwal($items, ($k+1));

这将导致它跳过数组中索引1处的值。删除函数内部的增量,即可正常使用:https://3v4l.org/SuXLR