programing

함수에 출력이 있는지 확인하는 가장 좋은 방법은?

cafebook 2023. 10. 25. 23:43
반응형

함수에 출력이 있는지 확인하는 가장 좋은 방법은?

저는 컨텐츠를 어떤 post_id에서 가져와 사이트의 프론트엔드에 출력할지 결정하기 위해 상당히 깊은 루틴을 실행하는 함수 목록을 가지고 있습니다.

이 함수가 내용물을 반환할 때 html 포장지로 포장하기를 원합니다.이 html 포장지는 함수가 반환할 출력이 있는 경우에만 로드되기를 원합니다.

예를 들어, 다음과 같은 것들이 있습니다.

public static function output_*() {
  //  my routines that check for content to output precede here
  //  if there IS content to output the output will end in echo $output;
  //  if there is NO content to output the output will end in return;
}

충분히 설명하자면 다음과 같은 것이 있습니다.

만약 이 함수들 중 하나가 출력을 반환한다면 나는 그것을 html 래퍼로 감싸기를 원합니다. 그래서 이론적으로 이와 같은 것이 내가 이루고자 하는 것입니다.

public static function begin_header_wrapper() {
  // This only returns true if an output function below returns content, 
  // which for me is other than an empty return;
  include(self::$begin_header_wrapper);
}

public static function output_above_header() {
  //  my routines that check for content to output precede here
  //  if there is content to return it will end in the following statement
  //  otherwise it will end in return;
  include($begin_markup); // This is the BEGIN html wrapper for this specifc output
  // It is, so let's get this option's post id number, extract its content,
  //  run any needed filters and output our user's selected content
  $selected_content = get_post($this_option);
  $extracted_content = kc_raw_content($selected_content);
  $content = kc_do_shortcode($extracted_content);
  echo $content;
  include($end_markup); // This is the END html wrapper for this specifc output
}
public static function output_header() {
  //  the same routine as above but for the header output
}
public static function output_below_header() {
  //  the same routine as above but for the below header output
}

public static function end_header_wrapper() {
  // This only returns true if an output function above returns content, 
  // which for me is other than an empty return;
  include(self::$end_header_wrapper);
}

지금은 미리 알고 있습니다. 출력 함수 중 하나에 출력이 있는지 여부를 두 번(시작할 때 한 번, 끝할 때 한 번) 확인하고 싶지 않지만 이 토끼 구멍에서 시작하여 함수가 무언가를 반환하는지 여부를 결정하는 가장 좋은 방법을 찾고 싶습니다.

아니면 이것에 접근할 수 있는 완전히 더 나은 방법이 있다면 전력을 다해주세요, ㅋㅋㅋ 그리고 저에게 알려주세요.

나는 온라인에서 이 기사와 다른 사람들을 보았다 @ 기능이 php로 출력이 되는지 확인하세요

그래서 결국, 나는 단지 이것에 접근하는 더 나은 방법이 있는지 그리고 그 조건들을 기반으로 내 HTML 래퍼를 실행할 수 있도록 내 함수가 반환할 출력을 가지고 있는지 확인하는 가장 좋은 방법이 무엇인지 알고 싶었습니다.

ob_get_length가 가장 좋은 방법일까요?제가 목적을 검토해보니, 이것이 가장 훌륭하고, 가장 단순해 보였지만, 조언과 피드백을 얻고 싶었습니다.아니면 내 변수가 어떤 것인지 확인할 수 있을 겁니다.$content반환되었습니까?감사해요.정말 감사합니다!

결과를 잡고 변수에 저장하면 빈() 함수에 제공됩니다.

if(!empty(($output = yourFunctionToTest(param1, paramN)))) {
   // do something with $output (in this case there is some output
   // which isn't considered "empty"
}

함수를 실행하고 출력을 변수(이 경우 출력)에 저장하고 빈()을 실행하여 변수 내용을 확인합니다.이후에 $output의 내용을 사용할 수 있습니다.

empty()는 빈 문자열 또는 0을 "empty"로 간주하므로 반환됩니다.true.

대안으로, 변수가 다음과 같이 set()과 같은 함수를 사용하여 변수가 다음과 같이 결정할 수 있습니다.null.

http://php.net/isset

http://php.net/empty

언급URL : https://stackoverflow.com/questions/39110662/best-way-to-determine-if-a-function-has-an-output

반응형