Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:46:33+00:00 2026-05-29T09:46:33+00:00

How would I go about putting function calls into a variable and then echo

  • 0

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. 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
                  $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;
                  }
                  ?>
              
              • 1 1 Answer
              • 0 Views
              • 0 Followers
              • 0
              Share
              • Facebook
              • Report

              Leave an answer
              Cancel reply

              You must login to add an answer.

              Forgot Password?

              Need An Account, Sign Up Here

              1 Answer

              • Voted
              • Oldest
              • Recent
              • Random
              1. Editorial Team
                Editorial Team
                2026-05-29T09:46:34+00:00Added an answer on May 29, 2026 at 9:46 am

                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:

                <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).

                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. 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>';
                }
                
                • 0
                • Reply
                • Share
                  Share
                  • Share on Facebook
                  • Share on Twitter
                  • Share on LinkedIn
                  • Share on WhatsApp
                  • Report

              Sidebar

              Related Questions

              Just a quick question about how you would go about implementing this. I want
              Anyone have any idea how I would go about converting a timestamp in milliseconds
              As a programmer I have no idea how one would go about programming menus
              let's say that I have this function in Rails (I have actually :P -
              EDIT Just to clarify, there is no intent of putting this into production. Purely
              How would I go about putting a transparent bar along the top of a
              Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
              Any ideas how I would go about writing a javascript method to insert an
              I would like to know how I would go about altering the HTML that
              I'm wondering how you would go about designing a good permgen space string in

              Explore

              • Home
              • Add group
              • Groups page
              • Communities
              • Questions
                • New Questions
                • Trending Questions
                • Must read Questions
                • Hot Questions
              • Polls
              • Tags
              • Badges
              • Users
              • Help
              • SEARCH

              Footer

              © 2021 The Archive Base. All Rights Reserved
              With Love by The Archive Base

              Insert/edit link

              Enter the destination URL

              Or link to existing content

                No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.