是否有一个标志来隐藏php函数中的echo语句。我希望能够控制何时打印echo语句。这是一个示例函数,我想要一个开关来控制echo语句。
function create_liturgical_calendar($year) {
if ( !isset($year) ) {
$year = get_year();
}
global $add_data, $skip;
echo "<div style='border-bottom: 4px solid #404040;'>year ".$year." add data ".$add_data." skip ".$skip."</div><div style='height: 92%; overflow: auto;'><br/>";
$Baptism_of_the_Lord_date = get_Baptism_of_the_Lord($year);
echo 'Baptism of the Lord '.$Baptism_of_the_Lord_date->month.' '.$Baptism_of_the_Lord_date->date.' '.$Baptism_of_the_Lord_date->year.' '.$Baptism_of_the_Lord_date->date_string.'<br/>';
$Ash_Wednesday_date = get_Ash_Wednesday_date($year);
echo 'Ash Wednesday will be '.$Ash_Wednesday_date->month.' '.$Ash_Wednesday_date->date.' '.$Ash_Wednesday_date->year.' '.$Ash_Wednesday_date->date_string.'<br/>';
$Annunciation_date = get_Annunciation_date($year);
echo 'Annunciation will be '.$Annunciation_date->month.' '.$Annunciation_date->date.' '.$Annunciation_date->year.' '.$Annunciation_date->date_string.'<br/>';
$Palm_Sunday_date = get_Palm_Sunday_date($year);
echo 'Palm Sunday will be '.$Palm_Sunday_date->month.' '.$Palm_Sunday_date->date.' '.$Palm_Sunday_date->year.' '.$Palm_Sunday_date->date_string.'<br/>';
$Holy_Thursday_date = get_Holy_Thursday_date($year);
echo 'Holy Thursday will be '.$Holy_Thursday_date->month.' '.$Holy_Thursday_date->date.' '.$Holy_Thursday_date->year.' '.$Holy_Thursday_date->date_string.'<br/>';
$Good_Friday_date = get_Good_Friday_date($year);
echo 'Good Friday will be '.$Good_Friday_date->month.' '.$Good_Friday_date->date.' '.$Good_Friday_date->year.' '.$Good_Friday_date->date_string.'<br/>';
$Holy_Saturday_date = get_Holy_Saturday_date($year);
echo 'Holy Saturday will be '.$Holy_Saturday_date->month.' '.$Holy_Saturday_date->date.' '.$Holy_Saturday_date->year.' '.$Holy_Saturday_date->date_string.'<br/>';
$Easter_date = get_Easter_date($year);
echo 'Easter will be '.$Easter_date->month.' '.$Easter_date->date.' '.$Easter_date->year.' '.$Easter_date->date_string.'<br/>';
$Divine_Mercy_date = get_Divine_Mercy_date($year);
echo 'Divine Mercy will be '.$Divine_Mercy_date->month.' '.$Divine_Mercy_date->date.' '.$Divine_Mercy_date->year.' '.$Divine_Mercy_date->date_string.'<br/>';
$Ascension_date = get_Ascension_date($year);
echo 'Ascension will be '.$Ascension_date->month.' '.$Ascension_date->date.' '.$Ascension_date->year.' '.$Ascension_date->date_string.'<br/>';
$Pentecost_date = get_Pentecost_date($year);
echo 'Pentecost will be '.$Pentecost_date->month.' '.$Pentecost_date->date.' '.$Pentecost_date->year.' '.$Pentecost_date->date_string.'<br/>';
$Most_Holy_Trinity_date = get_Most_Holy_Trinity_date($year);
echo 'Most Holy Trinity will be '.$Most_Holy_Trinity_date->month.' '.$Most_Holy_Trinity_date->date.' '.$Most_Holy_Trinity_date->year.' '.$Most_Holy_Trinity_date->date_string.'<br/>';
$Corpus_Christi_date = get_Corpus_Christi_date($year);
echo 'Corpus Christi will be '.$Corpus_Christi_date->month.' '.$Corpus_Christi_date->date.' '.$Corpus_Christi_date->year.' '.$Corpus_Christi_date->date_string.'<br/>';
$Most_Sacred_Heart_date = get_Most_Sacred_Heart_date($year);
echo 'Most Sacred Heart will be '.$Most_Sacred_Heart_date->month.' '.$Most_Sacred_Heart_date->date.' '.$Most_Sacred_Heart_date->year.' '.$Most_Sacred_Heart_date->date_string.'<br/>';
$Immaculate_Heart_date = get_Immaculate_Heart_date($year);
echo 'Immaculate Heart will be '.$Immaculate_Heart_date->month.' '.$Immaculate_Heart_date->date.' '.$Immaculate_Heart_date->year.' '.$Immaculate_Heart_date->date_string.'<br/>';
$Christ_the_King_date = get_Christ_the_King_date($year);
echo 'Christ the King will be '.$Christ_the_King_date->month.' '.$Christ_the_King_date->date.' '.$Christ_the_King_date->year.' '.$Christ_the_King_date->date_string.'<br/>';
}
答案 0 :(得分:3)
停止回声的唯一方法是使用输出缓冲。 e.g。
ob_start();
echo 'Hello World';
ob_end_clean();
不产生任何输出。
答案 1 :(得分:0)
if ( ....your condition ) {
echo ...
}
elseif {
echo ...
}
else {
echo ...
}
//etc...
or
switch ($someVar) {
case 0:
echo ...
break;
case 1:
echo ...
break;
case 2:
echo ...
break;
}
条件是你的旗帜......如果你希望函数不回显任何东西,那么设置一个条件并使用它,但它最有意义。
function echoThings() {
echo //etc
}
if (!$echoOff) {
echoThings();
}
然后在需要禁用该功能时设置$echoOff = true
。
答案 2 :(得分:0)
看起来因为没有一个通用标志来切换回声,所以最好是编写一个函数。
$print = true;
function print_echo ( $print_text ) {
if ( $print ) echo $print_text;
}
print_echo ('Print if true');