0

I am trying to use include_once as I get the Fatal error cannot redeclare function() error, but I cannot figure out how to.

The call_trendy() function is inspired by this famous link: Reference - What does this error mean in PHP? But it seems it's used to call a function outside the file, which is not the case in my issue.

<?php

function trendy($Name, $Price, $percent, $trend){
//used to create the winners and losers table
    $link = "https://signal-invest.com/tick/?ticker=";
    echo "<tr>
    <td><a href=\"$link$Name\" style='color:#616161;' >$Name</a></td>
    <td>$Price</td><td style='color: "; echo ($percent < 0 ? '#FF0000' : '#4ca64c'); echo ";'>$percent%</td><td>$trend</td></tr>";}

if (file_exists($trendy)) {
  include_once($trendy);
}

$query_uptrend= $con -> prepare(my query, which is irrelevant);
$query_uptrend->execute();
$results_query_uptrend = $query_uptrend->get_result();

echo "<body>";

?>
<div class="table_container_frontpage">
                                <div class="table_latest" style="box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);">
                                    <table style="border-radius: 0.50rem;">
                                         <tr>
                                                <th>NAME</th>
                                                <th>PRICE TODAY</th>
                                                <th>CHANGE</th>
                                                <th>TREND DAYS IN A ROW</th>
                                         </tr>
                                         <?php
                                                foreach ($results_query_uptrend as $res){
                                                    $name = $res["Name"];
                                                    $Price = number_format((float)$res['Price'], 2, '.', '');
                                                    $percent_diff = number_format((float)$res['PercentDiff'], 2, '.', '');
                                                    $trend = $res["uptrend"];
                                                    echo trendy($name, $Price, $percent_diff, $trend);
                                                    }
                                                ?>
                                    </table>
                                </div>
                            </div>
<?php

?>

Not entirely sure how to include it and make the below error go away.

Fatal error: Cannot redeclare trendy() (previously declared in /var/www/signal-> invest.com/public_html/wp-content/plugins/insert-php-code-snippet/shortcode-> handler.php(97) : eval()'d code:3) in /var/www/signal-invest.com/public_html/wp-> content/plugins/insert-php-code-snippet/shortcode-handler.php(97) : eval()'d code > on line 3

EDIT: Tried below still returns me the Fatal error

if (file_exists($trendy)) {
  include_once($trendy);
}
doomdaam
  • 691
  • 1
  • 6
  • 21
  • 2
    Would reading the [documentation](https://www.php.net/manual/en/function.include-once.php) on `include_once` help? – Alain Doe May 12 '21 at 18:04
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Funk Doc May 12 '21 at 18:06
  • You just asked this question almost exactly just moments ago. Why spam these forums? – Funk Doc May 12 '21 at 18:07
  • Not spamming, just asking for help after trying different solutions. – doomdaam May 12 '21 at 18:07
  • @FunkDoc using the link got me to the first solution which includes a nested function, but didn't work. – doomdaam May 12 '21 at 18:08
  • @AlainDoe I tried to understand but not sure how to apply it. I'm sure I did but didn't solve my issue. – doomdaam May 12 '21 at 18:16
  • 1
    What is `include_once $obj = trendy($Name, $Price, $percent, $trend);` supposed to do anyway? `include_once` is to include a file – brombeer May 12 '21 at 18:17
  • @brombeer tried applying this: https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/13879461#13879461 – doomdaam May 12 '21 at 18:18
  • 2
    Your code seems all over the place. Your `trendy` function prints some HTML, but then you call `echo trendy()` as if it returned something. Then you try to include the result of the function (which is nothing, because it doesn't return), which makes no sense at all. You need to identify the point where this function is actually being redeclared, which cannot be concluded from the code you posted. – El_Vanja May 12 '21 at 18:19
  • `include_once` is used to include a file, not a function. Your use of it appears to make no sense. And if `trendy` and `call_trendy` are in the same file then you don't need to include anything anyway. But the exact structure of your code is unclear from your unlabelled and out of context examples. The error mentions code called via `eval()`...are you storing this `trendy` function code in a database and loading it that way, perhaps? If you eval the same function code multiple times it would produce such an error.You never seem actually use the `call_trendy` function either as far as I can see – ADyson May 12 '21 at 18:20

0 Answers0