Question

Newb here attempting something new (for me)

What I am attempting: I am attempting to create an idea number that iterates with each return, so the incident report for the giant kaiju (strange beast - aka Godzilla and friends) has an individual case number that is created programmatically). This means return one would be #1, return two would be #2, returned 3 would be #3 et al.

What I have done: I've attempted to put my int at the beginning, in the middle, at the end, after the return, etc, but regardless of where I put it, i always get the same number (12). I've read about iterating over at php.net, and looked through stack overflow and other places but i've not found something i could internalize/understand enough to replicate.

function __toString(){
$kID = 1;//kaiju incident number

    $myReturn = "<p> Incident ID: kID" . $kID ;
    $kID++; //not iterating thru
    $myReturn .= $kID ;

    $myReturn .= " | Massive Terrestial Organsim Reported: " . $this->movTitle . "   " ;
    $myReturn .= " | Location Name: " . $this->entWhat. " ";
    $myReturn .= " | Severity of Incident Reported: " . $this->movRating . "</p>" ;

    return $myReturn;
Was it helpful?

Solution

$kID is set to 1 each time the function is called. Make it static so it will retain it's values across function calls:

static $kID = 1;//kaiju incident number

Also:

$myReturn = "<p> Incident ID: kID" . $kID;
$kID++;

OTHER TIPS

Right now every time your function is called, this code executes:

$kID = 1
$myReturn = $kID;
$kID++; 
$myReturn .= $kID ;

So $kID is being set to 1, then 2, hence the 12 in your output.

You need to define $kID outside your function. Maybe a rewrite like this:

function __toString(&$kID){
  $myReturn = "<p> Incident ID: kID" . $kID++ ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top