简体   繁体   English

PHP回显/打印变量内部的函数调用

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

How would I go about putting function calls into a variable and then echo or printing them repeatedly? 如何将函数调用放入变量,然后回显或重复打印它们?

Currently I have this code which is calling the PHP function "chords" 24 times. 目前,我有这段代码将PHP函数“ chords”调用了24次。 The functions are in groups of six so I would like to condense that six. 这些功能以六个为一组,所以我想浓缩六个。 I would like to scale this up though only once I have the code looking nicer and running more efficiently. 我只想让代码看起来更漂亮,运行效率更高,就可以扩大规模。

                          <?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>
              

              I would like to have it look something like this so that each group of functions can be seen more easy and tidy up the code. 我希望它看起来像这样,以便可以更轻松地查看每组函数并整理代码。 I have tried this but it displays the php code without executing it. 我已经尝试过了,但是它没有执行就显示了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>   
              

              The chord function 和弦功能

                  <? 
              
              
              
                  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;
                  }
                  ?>
              

              This could be the solution, but it's bad style: 这可能是解决方案,但是风格很糟糕:

              <?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;
                  ?>
              

              You can then echo the $chord_dump variable and it will always execute the __toString function of the cord_dump class then: 然后,您可以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>   
              

              That's done by PHP when it converts the object to string (because the variable is used with echo , triggering the so called string-context). 这是由PHP在将对象转换为字符串时完成的(因为该变量与echo ,从而触发了所谓的字符串上下文)。

              But instead I suggest you remove the global variables and turn them into parameters. 但是我建议您删除全局变量并将其转换为参数。 Additionally you could normalize each output-unit into a data-structure like an Array or stdClass , wrap them into an array, iterate over that array and create the output. 另外,您可以将每个输出单元标准化为一个数据结构,如ArraystdClass ,将它们包装到一个数组中,遍历该数组并创建输出。 That done you could easily extend both, what your program is doing as well as the output. 这样一来,您可以轻松扩展程序的功能以及输出。

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

              Just put your function inside a function. 只需将您的函数放入函数中即可。

              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
              }
              

              Then, each time you want them all called, you just call allChords() . 然后,每次您都希望调用它们时,只需调用allChords()

              Also, for your chordDump string, instead of putting <?php ?> tags inside of other <?php ?> tags, you simply use the . 另外,对于您的chordDump字符串,您只需使用.代替将<?php ?>标记放在其他<?php ?>标记内. operator to append: 要附加的运算符:

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

              However, for this to work, your chords function needs to return the value instead of printing it, as it seems to be doing now. 然而,对于这个工作,你的chords函数需要返回的值,而不是打印出来的,因为它似乎现在做的事情。 If your function has to print it, and there's no other option, you can use output buffering to capture the result, though it's probably way more advanced than you need for this task. 如果您的函数必须打印它,并且没有其他选择,则可以使用输出缓冲来捕获结果,尽管它可能比此任务所需的方法先进得多。

              You have multiple IDs with the same name of chords_wrapper ! 您有多个具有chords_wrapper相同名称的chords_wrapper That's not valid. 那是无效的。 Maybe you can concatenate something to the end of the id to differentiate. 也许您可以将某些内容连接到id的末尾以进行区分。

              Hard to say without knowing what cords is, but you can definitely replace the multiple chords echos with a single for Docs or foreach Docs loop. 难说不知道线是什么,但是你绝对可以用单取代多和弦回声for 文档foreach 文档循环。 And you could make the whole block into a function. 您可以将整个块变成一个函数。 I'm unclear as to the variables exactly, but something like this: 我不清楚确切的变量,但是像这样:

              <?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
              }
              ?>
              

              This is a set up for a basic php function. 这是基本php功能的设置。 However, based on your posted code, it's entirely unclear where the $fretnumbers variable is set or what you are trying to accomplish with the &i variables and the chord (*) statement. 但是,根据您发布的代码,完全不清楚$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;
                  }
              
              ?>
              

              声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

               
              粤ICP备18138465号  © 2020-2024 STACKOOM.COM