我有运算符代码。=并生成此错误

时间:2015-05-09 12:54:22

标签: php

我有这个变量

   $cache_content .= '
<div class="tr">
<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';

这会产生错误通知:Notice: Undefined variable: cache_content in C:\wamp\www\Lucru\config\jucatori.php on line 30

我该如何解决?

我的脚本是

   <table width="100%" border="0" cellspacing="2" cellpadding="2">  
   <?php 
    $query = "Select player.id,player.name,player.level,player.exp,player.job,player.gold,player.playtime,player_index.empire,guild.name AS guild_name
  FROM player.player 
  LEFT JOIN player.player_index 
  ON player_index.id=player.account_id 
  LEFT JOIN player.guild_member 
  ON guild_member.pid=player.id 
  LEFT JOIN player.guild 
  ON guild.id=guild_member.guild_id
  INNER JOIN account.account 
  ON account.id=player.account_id
  WHERE player.name NOT LIKE '[%]%' AND account.status!='BLOCK'

  ORDER BY level DESC, exp DESC, playtime DESC limit 0,10";       
      $i = "0" ;  

 $doQuery = $mysqli->query($query) or die(mysqli_error());
while($getPlayers = mysqli_fetch_object($doQuery))
   {
   $i = $i + 1 ;



   $cache_content .= '
<div class="tr">
<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';


}

if ( !file_exists('config/player_cache.txt'))
 {
$q1_cache = fopen('config/player_cache.txt', 'w');
fwrite($q1_cache, $cache_content);
fclose($q1_cache);
 }
 else
 {
     if (filemtime('config/player_cache.txt') < (time() - 200))
     {
        unlink('config/player_cache.txt');
        $q1_cache = fopen('config/player_cache.txt', 'w');
        fwrite($q1_cache, $cache_content);
        fclose($q1_cache);
     }
     else
     {
         $q1_cache = fopen('config/player_cache.txt', 'r');
         $cache_content = fread($q1_cache, filesize('config/player_cache.txt') + 1);
         fclose($q1_cache);
         echo $cache_content;
     }
 }



                ?> </table>

如果我使用varbile $ chache_content =&#39;&#39 ;;在文件player_chache.txt插入1行,但我想插入10行

2 个答案:

答案 0 :(得分:1)

删除.=运算符,仅使用=运算符。

.=表示您尝试将某些内容附加到已存在的现有变量中。 所以这有效:

$cache_content = '<div class="tr">';

$cache_content .= '<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';

注意第一个是如何创建变量的=,并使用.=运算符将值附加到它。

或者你可以通过这样做来解决这个问题:

$cache_content = '
<div class="tr">
<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';

因此要么确保您的变量$cache_content存在,然后才能向其添加内容,或者只是分配给它而不是附加到它。

答案 1 :(得分:0)

.=运算符用于将另一个字符串连接到预先存在的变量。该错误表示在达到此代码行时尚未定义$cache_content。如果此行之前没有条件流,则只需将.=替换为=

$cache_content = '
<div class="tr">
<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';

如果还有其他流可能会初始化$cache_content,请确保在点击此行之前始终初始化它:

$cache_content = '';

/* other code ... */

$cache_content .= '
<div class="tr">
<span class="t-rank">'. $i .'</span>
<span class="t-name"><a href="'.$config['base_url'].'main/highscore" class="fui txt" $first>'.$getPlayers->name.'</a></span>
<span class="t-lvl"><a class="fui txt"><img src="'.$config['base_url'].'resurse/imagini/regate/'.$getPlayers->empire.'.png" border="1px" width="22px" height="15px"/></a></span>
</div>';