PHP echo/print一个函数调用;s在一个变量中


PHP echo/print a function call that's inside a variable

如何将函数调用放入变量中,然后重复回显或打印它们?

目前,我有一段代码,它24次调用PHP函数"和弦"。函数是六个一组的,所以我想浓缩这六个。尽管只有在代码看起来更好、运行效率更高的时候,我才想扩大规模。

                          <?php 
                              $chord_name = 'A 7';
                              $i1 = '4';
                              $i2 = '7';
                              $i3 = '10';
                          ?>
                              <div id="chords_wrapper" class="chords_wrapper">
                                  <? echo $fretnumbers; ?>
                                  <h1><? echo $chord_name; ?></h1>
                                  <ul><?php chords (6); ?></ul>
                                  <ul><?php chords (5); ?></ul>
                                  <ul><?php chords (4); ?></ul>
                                  <ul><?php chords (3); ?></ul>
                                  <ul><?php chords (2); ?></ul>
                                  <ul><?php chords (1); ?></ul>
                              </div>
              
                          <?php 
                              $chord_name = 'A Minor';
                              $i1 = '3';
                              $i2 = '7';
                              $i3 = '0';
                          ?>
                              <div id="chords_wrapper" class="chords_wrapper">
                                  <? echo $fretnumbers; ?>
                                  <h1><? echo $chord_name; ?></h1>
                                  <ul><?php chords (6); ?></ul>
                                  <ul><?php chords (5); ?></ul>
                                  <ul><?php chords (4); ?></ul>
                                  <ul><?php chords (3); ?></ul>
                                  <ul><?php chords (2); ?></ul>
                                  <ul><?php chords (1); ?></ul>
                              </div>
              
                          <?php 
                              $chord_name = 'A Minor 7';
                              $i1 = '3';
                              $i2 = '7';
                              $i3 = '10';
                          ?>
                              <div id="chords_wrapper" class="chords_wrapper">
                                  <? echo $fretnumbers; ?>
                                  <h1><? echo $chord_name; ?></h1>
                                  <ul><?php chords (6); ?></ul>
                                  <ul><?php chords (5); ?></ul>
                                  <ul><?php chords (4); ?></ul>
                                  <ul><?php chords (3); ?></ul>
                                  <ul><?php chords (2); ?></ul>
                                  <ul><?php chords (1); ?></ul>
                              </div>
              
                      <div style="width:620px; text-align:center; clear:both;">
                      </div>
              
                  </div>
              

              我希望它看起来像这样,这样可以更容易地看到每组函数并整理代码。我已经尝试过了,但它显示了php代码而没有执行它

              <?php
                  $kn = '25';
                  $chord_dump = '<div id="chords_wrapper" class="chords_wrapper">
                          <? echo $fretnumbers; ?>
                          <h1>'.$chord_name.'</h1>
                          <ul><?php chords (6); ?></ul>
                          <ul><?php chords (5); ?></ul>
                          <ul><?php chords (4); ?></ul>
                          <ul><?php chords (3); ?></ul>
                          <ul><?php chords (2); ?></ul>
                          <ul><?php chords (1); ?></ul>
                      </div>';
                  ?>
                  <div id="chords_set">
                          <?php 
                      // A Major
                              $i1 = '4';
                              $i2 = '7';
                              $i3 = '0';
                              $chord_name = 'A Major';
                              echo $chord_dump;
              
                      // A 7
                              $i1 = '4';
                              $i2 = '7';
                              $i3 = '10';
                              $chord_name = 'A 7';
                              echo $chord_dump;
              
                      // A Minor
                              $i1 = '3';
                              $i2 = '7';
                              $i3 = '0';
                              $chord_name = 'A Minor';
                              echo $chord_dump;
              
                      // A Minor 7
                              $i1 = '3';
                              $i2 = '7';
                              $i3 = '10';
                              $chord_name = 'A Minor 7';
                              echo $chord_dump;
                          ?>
              
                      <div style="width:620px; text-align:center; clear:both;">
                      </div>
              
                  </div>   
              

              和弦函数

                  <? 
              
                  function chords ($funtion_string) {
                      /*$chrd_tn ="CGCFCE";
                      $tuning_capo =0;*/
                      $chrd_tn = $GLOBALS['chrd_tn'];
                      $tuning_capo = $GLOBALS['tuning_capo'];
                      $notes_array = '';
                      // Select Chord Key Notes
                      $result = mysql_query("SELECT * FROM `guitar_tunings_chords` WHERE note_id - ".$GLOBALS['kn']." >= 0 AND ( (( note_id - ".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i1']." OR ( (note_id-".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i2']." OR ( (note_id-".$GLOBALS['kn'].") % 12 ) = ".$GLOBALS['i3']." OR ( note_id - ".$GLOBALS['kn'].") % 12 = 0) LIMIT 24") or die(mysql_error());
                      while ( $row = mysql_fetch_array($result) ) {
                      $notes2 = $row["note"]; 
                      $notes2 = str_replace("#", "z", $notes2);
                      $notes_array = ''.$notes2.','.$notes_array.'';
                      }
                          // Delete comma from end of chord notes array
                          $notes_array = substr($notes_array,0,-1);
                      // Counter to distinguish the nut from the fretboard
                      $counter = 0;
                      $result2 = mysql_query("SELECT * 
                  FROM `guitar_tunings_links`
                  JOIN guitar_tunings_chords ON guitar_tunings_links.".$funtion_string." = guitar_tunings_chords.note
                  WHERE tuning =  '".$chrd_tn."'") or die(mysql_error()); 
                      while ( $row = mysql_fetch_array($result2) ) {
                          $bridge_note = ($row["note_id"]);           
                      }/*
                      ?><BR>$chrd_tn  = <? var_dump ($chrd_tn); 
                      ?><BR>$tuning_capo  = <? var_dump ($tuning_capo);
                      ?><BR>$funtion_string  = <? var_dump ($funtion_string);  
                      ?><BR>$key_note  = <? var_dump ($key_note); 
                      ?><BR>$bridge_note  = <? var_dump ($bridge_note); 
                      ?><BR><?*/
                          // SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= 28 LIMIT 0,8
                      $result4 = mysql_query("SELECT * FROM `guitar_tunings_chords` WHERE `note_id` >= $bridge_note LIMIT $tuning_capo,8") or die(mysql_error());
                      while ( $row = mysql_fetch_array($result4) ) {
                          //Notes
                          $note = ($row["note"]); 
                          // Replace # with z
                          $note = str_replace("#", "z", $note);
                          // Distinguish nut notes on or off
                          if (preg_match("/'b".$note."'b/i", $notes_array)) {
                              $n_nut_style = 'note_nut_on';
                          } else { $n_nut_style = 'note_nut_off'; 
                          }
                          // Distinguish fretboard notes on or off
                          if (preg_match("/'b".$note."'b/i", $notes_array)) {
                              $n_style = 'note_on';
                          } else { $n_style = 'note_off'; 
                          }
                          // Print nut notes
                          if ($counter < 1) {
                              $str_prtou = '<li class="'.$n_nut_style.'">'.$note.'</li>';
                              $numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8");
                              $str_prtou = str_replace($numbers, "", $str_prtou);
                          }
                          // Print fretboard notes
                          if ($counter > 0)       
                          $str_prtou = ''.$str_prtou.'<li class="'.$n_style.'">'.$note.'</li>
                          ';
                          $counter++;
                      }
                      $str_prtou = str_replace("z", "#", $str_prtou);
                      $numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8");
                      $str_prtou = str_replace($numbers, "", $str_prtou);
                      echo $str_prtou;
                  }
                  ?>
              

              这可能是解决方案,但它的风格很糟糕:

              <?php
                  $kn = '25';
                  class chord_dump {
                      public function __toString() {
                          global $fretnumbers, $chord_name;
                          return '<div id="chords_wrapper" class="chords_wrapper">
                          '.$fretnumbers.'
                          <h1>'.$chord_name.'</h1>
                          <ul>'.chords(6).'</ul>
                          <ul>'.chords(5).'</ul>
                          <ul>'.chords(4).'</ul>
                          <ul>'.chords(3).'</ul>
                          <ul>'.chords(2).'</ul>
                          <ul>'.chords(1).'</ul>
                      </div>';
                      }
                  }
                  $chord_dump = new chord_dump;
                  ?>
              

              然后,您可以echo $chord_dump变量,它将始终执行cord_dump类的__toString函数,然后:

              <div id="chords_set">
                      <?php 
                  // A Major
                          $i1 = '4';
                          $i2 = '7';
                          $i3 = '0';
                          $chord_name = 'A Major';
                          echo $chord_dump;
              
                  // A 7
                          $i1 = '4';
                          $i2 = '7';
                          $i3 = '10';
                          $chord_name = 'A 7';
                          echo $chord_dump;
              
                  // A Minor
                          $i1 = '3';
                          $i2 = '7';
                          $i3 = '0';
                          $chord_name = 'A Minor';
                          echo $chord_dump;
              
                  // A Minor 7
                          $i1 = '3';
                          $i2 = '7';
                          $i3 = '10';
                          $chord_name = 'A Minor 7';
                          echo $chord_dump;
                      ?>
              
                  <div style="width:620px; text-align:center; clear:both;">
                  </div>
              
              </div>   
              

              这是由PHP在将对象转换为字符串时完成的(因为该变量与echo一起使用,从而触发所谓的字符串上下文)。

              但是我建议你去掉全局变量,把它们变成参数。此外,您可以将每个输出单元规范化为一个数据结构,如ArraystdClass,将它们封装到一个数组中,在该数组上迭代并创建输出。完成后,您可以很容易地扩展程序正在做的事情以及输出。

              /* model */
              $kn = '25';
              $chords = array(        
                  array(4, 7, 0, 'A Major'),
                  array(4, 7, 10, 'A 7'),
                  ...
              );
              /* output */
              foreach ($chords as $chord_index => $chord)
              {
                  list($i1, $i2, $i3, $chord_name) = $chord;
                  echo '<div id="chord_', $chord_index, '" class="chords_wrapper">'
                        , $fretnumbers, '<h1>', $chord_name, '</h1>';
                  foreach (range(6,1) as $i)
                  {
                      echo '<ul>', chords($i), '</ul>';
                  }
                  echo '</div>';
              }
              

              只需将函数放入函数中即可。

              function allChords(){
                ?>
                <div id="chords_wrapper" class="chords_wrapper">
                  <? echo $fretnumbers; ?>
                  <h1><? echo $chord_name; ?></h1>
                  <ul><?php chords (6); ?></ul>
                  <ul><?php chords (5); ?></ul>
                  <ul><?php chords (4); ?></ul>
                  <ul><?php chords (3); ?></ul>
                  <ul><?php chords (2); ?></ul>
                  <ul><?php chords (1); ?></ul>
                </div>
                <?php
              }
              

              然后,每次你想让他们都打电话,你只需要打allChords()

              此外,对于chordDump字符串,不需要将<?php ?>标记放在其他<?php ?>标记中,只需使用.运算符来附加:

              $chord_dump = '<div id="chords_wrapper" class="chords_wrapper">
                      <? echo $fretnumbers; ?>
                      <h1>' . $chord_name . '</h1>
                      <ul>' . chords (6) . '</ul>
                      <ul>' . chords (5) . '</ul>
                      <ul>' . chords (4) . '</ul>
                      <ul>' . chords (3) . '</ul>
                      <ul>' . chords (2) . '</ul>
                      <ul>' . chords (1) . '</ul>
                  </div>';
              

              然而,为了实现这一点,chords函数需要返回值,而不是像现在这样打印。如果函数来打印它,并且没有其他选项,则可以使用输出缓冲来捕获结果,尽管它可能比执行此任务所需的高级得多。

              这是为一个基本php函数设置的。但是,根据您发布的代码,完全不清楚$fretnumbers变量设置在哪里,也不清楚您试图用&i变量和chord (*)语句实现什么。

              <?php
                  $kn = '25';
                  echo '<div id="chords_set">';
                      // A Major
                      $i1 = '4';
                      $i2 = '7';
                      $i3 = '0';
                      chord_dump('fretnumbershere', 'A Major');
              
                      // A 7
                      $i1 = '4';
                      $i2 = '7';
                      $i3 = '10';
                      chord_dump('fretnumbershere', 'A 7');
              
                      // A Minor
                      $i1 = '3';
                      $i2 = '7';
                      $i3 = '0';
                      chord_dump('fretnumbershere','A Minor');
              
                      // A Minor 7
                      $i1 = '3';
                      $i2 = '7';
                      $i3 = '10';
                      chord_dump('fretnumbershere','A Minor 7');
              
                      echo '<div style="width:620px; text-align:center; clear:both;">
                      </div>
              
                  </div>';
              function chord_dump($fretnumbers, $chord_name) {
               $chords= '<div id="chords_wrapper" class="chords_wrapper">.'
                      $fretnumbers.'
                      <h1>'.$chord_name.'</h1>
                      <ul>'.chords (6).'</ul>
                          <ul>'.chords (5).'</ul>
                          <ul>'.chords (4).'</ul>
                          <ul>'.chords (3).'</ul>
                          <ul>'.chords (2).'</ul>
                          <ul>'.chords (1).'</ul>
                      </div>';
                  echo chords;
                  }
              ?>
              

              您有多个ID具有相同的名称chords_wrapper!这是无效的。也许您可以将一些东西连接到id的末尾以进行区分。

              在不知道什么是弦的情况下很难说,但你肯定可以用一个forDocsforeachDocs循环来替换多个和弦回声。你可以把整个块做成一个函数。我不清楚确切的变量,但有这样的东西:

              <?php
              // $instance is a number / string you can pass in to differentiate your id
              // not really sure what you're up to w $i1, $i2, and $i3
              function makeCord($chord_name, $i1, $i2, $i3, $fretnumbers, $instance)
              {
                  $GLOBALS['i1'] = $i1;
                  $GLOBALS['i2'] = $i2;
                  $GLOBALS['i3'] = $i3;
                  $chords = array_map('chords', range(6,1));
                  ?>
                  <div id="chords_wrapper<?php echo $instance; ?>" class="chords_wrapper">
                      <?php 
                      echo $fretnumbers;
                      echo "<h1>$chord_name</h1>";            
                      foreach ($chords as $chord)  
                      {
                          echo "<ul>$chord</ul>";
                      }
                      ?>
                   </div>
                   <?php
              }
              ?>