douhuanbai6729
2014-01-21 03:02 阅读 40
已采纳

标志在php中停止回声

Is there a flag to hide echo statements in a php function. I want to be able to control when the echo statements are printed. Here is a sample function where I want a switch to control the echo statements.

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/>';

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dpjpo746884 dpjpo746884 2014-04-01 21:06

    Looks like since there isn't a general flag to toggle echo, the best thing is to write a function.

      $print = true;
    
      function print_echo ( $print_text ) {
             if ( $print ) echo $print_text;
      }
    
      print_echo ('Print if true');
    
    点赞 评论 复制链接分享
  • doubo4336 doubo4336 2014-01-21 03:05
    if ( ....your condition ) {
      echo ...
    }
    elseif {
      echo ...
    }
    else {
      echo ...
    }
    //etc...
    
    or
    
    switch ($someVar) {
      case 0:
        echo ...
      break;
      case 1:
        echo ...
      break;
      case 2:
        echo ...
      break;
    }
    

    Conditions are your flag... If you want the function to not echo anything, then setup a condition and use it however it makes the most sense.

    function echoThings() {
      echo //etc
    }
    
    if (!$echoOff) {
      echoThings();
    }
    

    then just set $echoOff = true when you need to disable the function.

    点赞 评论 复制链接分享
  • dscdttg4389 dscdttg4389 2014-01-21 03:09

    The only way to stop an echo is to use output buffering. e.g.

    ob_start();
    echo 'Hello World';
    ob_end_clean();
    

    Produces no output.

    More info here: http://au2.php.net/manual/en/intro.outcontrol.php

    点赞 评论 复制链接分享

相关推荐