为返回的数据添加顺序号
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;
Newb在这里尝试新事物(对我来说) p>
我在尝试什么 :我正在尝试创建一个与每次返回迭代的创意编号,因此巨型kaiju(奇怪的野兽 - 又名哥斯拉和朋友)的事件报告具有以编程方式创建的个别案例编号。 这意味着返回一个将是#1,返回两个将是#2,返回3将是#3等人。 p>
我做了什么:我试图把我的int放在 在开始,中间,结束,返回之后等等,但不管我把它放在哪里,我总是得到相同的数字(12)。 我已经阅读了关于在php.net上迭代的内容,并查看了堆栈溢出和其他地方,但我没有找到可以内化/理解足以复制的东西。 p>
function __toString(){
$ kID = 1; // kaiju事件编号
$ myReturn =“&lt; p&gt;事件ID:kID”。 $ kID;
$ kID ++; //不通过
$ myReturn迭代。= $ kID;
$ myReturn。=“| Massive Terrestial Organsim报道:”。 $ this-&gt; movTitle。 “”;
$ myReturn。=“|位置名称:”。 $这 - &GT; entWhat。 “”;
$ myReturn。=“|事件严重程度报告:”。 $ this-&gt; movRating。 “&LT; / P&gt;” 中 ;
返回$ myReturn;
code> pre>
div>
$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++;
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++ ;