duanhan9334 2013-04-16 16:28
浏览 8
已采纳

从变量调用函数(PHP)[关闭]

I have a website I am making as a school project, and it appears I have jumped into something a bit too deep. I'm using PHP to create a page for each array object. In each page, I want to personalize the contents to match the page. For example, I have a page that gets created named Darryl and it's going to contain all my images. Other pages will be created and the contents of those pages will be created dynamically depending on the page.

What I am trying to do, is to change the img src using PHP and use a counter to make different images pop up. This is incredibly hard for me to explain.

I have this line of html code which contains the php(Which determines which images get shown:

 <td><img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br><?php print $pageName; ?></td>

The part i am having issue with is the " print $imgSrc; " part. This is the area where my php will create the src path. It uses this variable $imgSrc to get the correct path.

That variable is:

 $imgSrc = "images"."/".$pageName."/".counter();

Which writes the

  • 写回答

1条回答 默认 最新

  • douyuliu9527 2013-04-16 16:42
    关注

    It looks like you're confused about what a function is or perhaps when it gets executed. If this is all the code you have on your page:

    $imgSrc = "images"."/".$pageName."/".counter();
    <td>
        <img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br>
        <?php print $pageName; ?>
    </td>
    

    Then $imgSrc is only ever written to once. So whatever counter() does, it only does once. You could wrap the whole thing in a for loop and do away withe the counter() call. You could remove the local variable of $imageSrc and make the attribute defined on the fly, like so:

    <td>
        <img style="width:11em;" class="magnify" src="<?php print "images"."/".$pageName."/".counter(); ?>"/><br>
        <?php print $pageName; ?>
    </td>
    

    Then if the counter() function is stateful itself (as in, it returns a different value each time you call it), you're all set.

    You can make a stateful function like the following:

    $counter_var = 0;
    
    function counter() {
        global $counter_var;
        return $counter_var++;
    }
    

    But globals are frowned upon, because the create hard to trace side effects (and other reasons). Most people would tell you to use a for loop. I only mention it because it looks closer to what you were trying to do in your original code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。