• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

You can't only use it once; you can only declare it once. Every time the function hoeveelzijner is used, the function hoeveelhoeveel is declared. If you call it more than once, you'll redeclare it, which is forbidden.

You have two options: the first is to take the function definition outside the containing function. The function will be declared when the file is first parsed, then used repeatedly. If you want to restrict the function definition to a particular scope (a good idea in principle), you can use the anonymous function syntax introduced in PHP 5.3:

function hoeveelzigner($jaar, $id)
{
    $hoeveelhoeveel = function($beginstamp, $endstamp, $id)
    {
            $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"');
            return mysql_num_rows($dates);
    };

    // etc
}

You can then use the function as $hoeveelhoeveel(...).

  • 15
Reply Report
    • Thanks for the excellent post. I was struggling with this as well, and the only solution I could find was this, which received a negative rating on php.net. And now I see why :)

Although PHP let's you declare a function anywhere I think what you are doing is not considered best practice.

For most people it just look wrong.

You should just declare the function outside the parent function.

Alternatively you could add a function_exists() around the inner function, although again I would just declare the inner function outside the parent function.

Are perhaps even make a class for it.

  • 0
Reply Report

You can't declare it again, especially if the function calls it more than once. Here's a simple change that should work.

function hoeveelhoeveel($beginstamp, $endstamp, $id)

    $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"');
    return mysql_num_rows($dates);
}

function hoeveelzijner ($jaar, $id)
{
    $i = 1;
    while ($i < 13)
    {
        $hoeveel[$i-1] = hoeveelhoeveel(mktime(0, 0, 0, $i, 1, $jaar),mktime(0, 0, 0, $i, cal_days_in_month(CAL_GREGORIAN,$i,$jaar),$jaar),$id);
        $i = $i+1;
    }
    return $hoeveel;
}

Then you can call either function in a similar way in other functions too, if needed be.

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions