Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

php - Cannot redeclare get_class() error

I'm trying to apply the class effect into input type="submit" whenever its parent form action contains the string special.

I'm using the following code, but Dreamweaver tells me there are syntax error in lines 15,16,22:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 'On');

$case1 = "special"; 
$item = "aaa"; 
$item = "something"; 

function get_class( $slug ) {
    $class_map = array(
        'special' => 'effect',
        'none'    => '',
        // .. you could add others here if appropriate

    return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : ''; //line 15
);  // line 16
?>
<HTML>
<body>
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input type="submit" class="<?= get_class( $case1 ); ?> general-class" value="Click Me"></form> // line 22
</body>
</HTML>

The output is empty, and no errors are being displayed when I try running the file.

What's wrong?

Edit 2 - Updated code:

Now getting fatal error on line 16:

Fatal error: Cannot redeclare get_class() on line 16

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 'On');

$case1 = "special"; 
$item = "aaa"; 
$item = "something"; 

function get_class( $slug ) {
    $class_map = array(
        'special' => 'effect',
        'none'    => '',
        // .. you could add others here if appropriate
);
    return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';
} // line 16
?>
<HTML>
<body>
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input type="submit" class="<?php get_class( $case1 ); ?> general-class" value="Click Me"></form>
</body>
</HTML>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have very little coding in PHP, and have forgotten most of it's syntaxes. However, from basic understanding, it seems that If that is your entire code base, then you are missing either of "}" (closing brace for function get_class) or you have misplaced your closing $class_ map array in the following code :

function get_class( $slug ) {
    $class_map = array(
        'special' => 'effect',
        'none'    => '',
        // .. you could add others here if appropriate

        return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';    //line 15
      );  // line 16

Your code should be either of the following two:

function get_class( $slug ) {
    $class_map = array(
        'special' => 'effect',
        'none'    => ''
    );

    return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';
}

OR

function get_class( $slug ) {
    $class_map = array(
        'special' => 'effect',
        'none'    => '',
        return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';
    );
}

The last one don't seem appropriate/feasible to me though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...