未定义的变量:位置 [英] Undefined variable: position

查看:143
本文介绍了未定义的变量:位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起球员,我读和搜索在网上,我无法找到这个:(一个解决方案。 如果可以请帮我:)谢谢。

如果我硬code 位置,然后它工作得很好。我已经尝试过这几种不同的方法......

P.S。    我要创建某种图片库AJAX页面锻炼:)我认为这位置变量应该是在单/静态类我猜。我没能尚未测试这个类,但我认为这将只对第一个和最后一个图像总是:)(在Java code这将是很容易做到的:))。

错误:  的通知:未定义的变量:位置在D:\瓦帕\的www \在线11测试\ gethint.php

 < PHP

类指数{
    私人$位置= 1;

    下一个公开函数(){
        返回$位置++ ;;
    }

    公共职能preV(){
        返回$ position--;
    }

     公共功能复位(){
        $位置= 1;
        返回$地位;
    }
}

$行动= $ _ REQUEST [行动];
    $指数= 0;

如果($行动!= 1){
    $指数=指数:: preV();
} 其他 {
    $指数=指数::接下来的();
}

如果($指数< 1){
    $指数= 7;
}
如果($指数大于7){
    $指数= 1;
}

    $回应=。< IMG的边界= \0 \SRC = \$指数。JPG \ALT = \$指数。\WIDTH = \500 \高度= \ 334 \>中;

 回声$反应;
 ?>
 

解决方案

改变这一行

 返回$位置++ ;;
 

 返回$位置++; // 额外 ;
 

和这样的访问类变量

  $这个 - >仓位++; //不是$位置
 

有关创建静态变量使用静态关键字

 私有静态$位置= 1;
 

有关访问静态变量使用

 自:: $地位;
 

改变这种

 如果($行动!= 1){
    $指数=指数:: preV();
} 其他 {
    $指数=指数::接下来的();
}
 

  $ =中新指数();
如果($行动!= 1){
    $指数= $ IN-> preV();
} 其他 {
    $指数= $ IN  - >接下来();
}
 

Sorry guys, I am reading and searching over the web and I cannot find a solution for this :(. If you can please help me :) Thanks.

If I hard code position then it works well. I already tried this on several different ways...

P.S. I have to create some kind of image gallery ajax page for exercise :) I assume this position variable should be in singleton/static class i guess. I was not able to test this class yet, but I think this will work only for first and last image always :) (in java code this would be very easy to do :) ).

Error: Notice: Undefined variable: position in D:\Wamp\www\test\gethint.php on line 11

<?php

class Index {
    private $position=1;

    public function next(){
        return $position++;;
    }

    public function prev(){
        return $position--;
    }

     public function reset(){
        $position=1;
        return $position;
    }
}

$action=$_REQUEST["action"];
    $index = 0;

if($action!=1){
    $index=Index::prev();
} else {
    $index=Index::next();
}

if($index < 1){
    $index=7;
}
if($index > 7){
    $index=1;
}

    $response="<img border=\"0\" src=\"".$index.".jpg\" alt=\"".$index."\" width=\"500\" height=\"334\">";

 echo $response;
 ?>

解决方案

change this line

return $position++;;

to

return $position++; // extra ;

and access class variable like this

$this->position++; // not $position

for creating static variable use static keyword

private static $position=1;

for accessing static variable use

self::$position;

change this

if($action!=1){
    $index=Index::prev();
} else {
    $index=Index::next();
}

to

$in = new Index();
if($action!=1){
    $index= $in->prev();
} else {
    $index= $in->next();
}

这篇关于未定义的变量:位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆