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

时间:2012-02-07 23:54:31

标签: php function variables

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

目前我有这个代码,它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;
    }
    ?>

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

只需将您的功能放在一个功能中。

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功能需要返回值,而不是打印,因为它现在似乎在做。如果您的函数来打印它,并且没有其他选项,您可以使用输出缓冲来捕获结果,尽管它可能比您执行此任务所需的更高级。

答案 2 :(得分:0)

您有多个同名chords_wrapper的ID!那是无效的。也许你可以将某些东西连接到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
}
?>

答案 3 :(得分:0)

这是一个基本的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;
    }

?>