doubi7306 2014-04-18 18:52
浏览 55

php常量在foreach循环中重复

I've been strugling with this problem for some time now. I can't figure out whats wrong. Following code is very simple, VISOR_URL is a constant defined in another file. If i echo it outside forearch loop it prints the constant value. If I print it inside the loop it's value gets duplicated. Same problem occurs if I use a variable. Any ideas?. Thanks in advance. Sebastian

<?php
require_once('conf.php');//I require the file where VISOR_URL is defined
//VISOR_URL is defined in conf.php. define('VISOR_URL', $server_ip.'/'.VISOR_NAME);

echo VISOR_URL; //echoes http://192.168.0.15/tncvisornuevo

if (!empty($occurrence_ids)) {//occurrence_ids is an array and values are printed fine

    foreach ($occurrence_ids as $key => $value) {
        echo VISOR_URL; //echoes http://192.168.0.15/tncvisornuevohttp://192.168.0.15/tncvisornuevo 

        $ocurrencia = new ca_occurrences($value);

        $nombre_ocurrencia = $ocurrencia->get('ca_occurrences.preferred_labels');

        $link = '<a href="'.VISOR_URL.'/views/occurrenceDetails.php?occurrence_id='.$value.'">'.$nombre_ocurrencia.'</a>';

        echo $link."<br>";
    }

}
?>

Following simple example does not duplicate the constant value:

<?php

//Define a constant
define('CONSTANT', 'imaconstant');

echo CONSTANT."<br>"; //Echoes imaconstant

$test_array = array(0,1,2,3,4,5,6,7,8);

foreach ($test_array as $key => $value) {
    echo $value.CONSTANT,"<br>"; //Echoes nimaconstant, n+1imaconstant

}

?>
  • 写回答

1条回答 默认 最新

  • douhan8581 2014-04-18 18:58
    关注

    It'll echo it as many times the loop will run. For example -

    $array = array('aa','bb','cc');
    $var = "abc";
    
    foreach($array as $key => $value);
    {
        echo $var.'<br>';
    }
    
    /*   
       abc
       abc
       abc
    */
    

    Now above since the array has size of 3, the loop will run 3 times and echoes the variable...ofcourse 3 times.

    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败