PHP echo $config inside array


PHP echo $config inside array

好的,我已经有了一个配置文件,看起来像这样:

<?php
# EPICMC CMS CONFIG FILE 
return array(
    'sitename' => 'EPICMC',
    'color' => '00aa00',
    'url' => 'stats.epicmc.us',
    'protocol' => 'https://',
);

这是我的网页。

<head>
       <link rel="icon" 
          type="image/png" 
          href="avatar/<?php                     
             $statsJSON = file_get_contents($configs['url']/api.php?task=stats&player='.$_GET['player'].'');
              $stats = json_decode($statsJSON);
              if(empty($stats)){
              echo $_GET['player'];
              echo '/16';
              } else {
                           echo strtoupper($stats->skin); 
                           echo '/16';
             }
             ?>">
       <link rel="apple-touch-icon" 
          type="image/png" 
          href="avatar/<?php                     
             if(empty($stats)){
             echo $_GET['player'];
             echo '/250';
             } else {
                          echo strtoupper($stats->skin); 
                          echo '/250';
             }
             ?>">
       <title><?php echo "$configs['sitename']' - "; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?></title>
       <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?>">
       <meta name=viewport content="width=device-width, user-scalable=no">
       <meta name="description" content="View <?php echo $_GET['player'];?>'s <?php echo $configs['sitename']?> stats!">
       <style>#profile::before{display:block;height:144px;border-radius:0;background:#0a0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02);content:''}#profile{background:#fafafa;box-shadow:rgba(0,0,0,.14902) 0 1px 1px 0,rgba(0,0,0,.09804) 0 1px 2px 0;padding-bottom:38px;text-align:center;max-width:80%}#profile>img{position:relative;top:-52px;display:block;margin:auto;width:96px;height:96px;border:4px solid #fff;border-radius:0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02)}#profile h2,#profile h3{font-weight:400;color:#77767e;margin-bottom:30px}#profile h2{margin-top:-30px;padding:0 16px 38px;font-size:21px}#profile h3{margin-top:-60px;padding:0 38px 16px;font-size:11px}#profile h4{font-weight:400}#locked{margin-bottom:-80px}table,td,th{border:1px solid #000;border-collapse:collapse;width:80%}td,th{padding:5px;text-align:left}</style>
    </head>
    <body>
       <?php include("layout/header.php"); ?>
       <BR>
       <BR>
       <center>
          <aside id="profile">
             <?php
                function mcus_timeAgoString($dateInterval) {
                $daysAgo = $dateInterval->days;
                //echo $difference;
                if ($daysAgo == 0){
                if ($dateInterval->h == 0){
                  $diffStr = $dateInterval->i . " Minutes Ago";
                } else {
                   $diffStr = $dateInterval->h . " Hours Ago";
                }
                } else {
                if($daysAgo === 1) {
                  $diffStr = "1 Day Ago";
                } else if($daysAgo === 7) {
                  $diffStr = "1 Week Ago";
                } else if($daysAgo === 14) {
                  $diffStr = "2 Weeks Ago";
                } else {
                  $diffStr = "$daysAgo Days Ago";
                }
                }
                return $diffStr;
                }
                      $get_player = $_GET['player'];
                      $infoJSON = file_get_contents('$url/api.php?task=info&player='.$_GET['player'].'');
                      $info = json_decode($infoJSON);
                      $statsJSON = file_get_contents('$url/api.php?task=stats&player='.$_GET['player'].'');
                     $stats = json_decode($statsJSON);
                      // $now = time(); // or your date as well`enter code here`
                ?>
             <img src="avatar/<?php                     
                if(empty($stats)){
                echo $_GET['player'];
                echo '/96';
                } else {
                             echo strtoupper($stats->skin); 
                             echo '/96';
                }
                ?>">
             <?php 
                echo '<style>';
                echo '#verified {';
                echo 'fill:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '.toggle {';
                echo 'background-color:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background:';
                echo '#';
                    if(empty($info->theme)){
                echo "$color"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo ' !important }';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background-image:';
                echo 'url(';
                echo substr($info->cover, 0, 50); 
                echo ' ) !important;';
                echo '</style>';
                ?>
             <h2>
                <?php
                   if($info->locked == 1){
                             echo '<div id="locked"><h5>This username is locked.</h5></div>';
                       }else {
                       echo strip_tags($_GET['player']);
                       }
                       ?>
                <?php
                   if(substr($info->verified,0,2) == 1) {
                         echo '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="verified" width="24" height="24" viewBox="0 0 24 24">
                   <path d="M10,17L5,12L6.41,10.58L10,14.17L17.59,6.58L19,8M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3Z" />
                </svg>
                '
                ;
                }
                //print_r ($stats);
                if(substr($info->locked,0,2) == 0) {
                echo '
                <h3>
                ';
                echo 'Logged In ';
                if(empty($info)){
                echo "Not Registered";
                } else { 
                //echo substr($playerAccountInfo->lastlogin,0,50); 
                $dtNow = new DateTime('Now');       
                $dtLastLogin = new DateTime($info->lastlogin);
                $diLastLoginToNow = $dtNow->diff($dtLastLogin);
                echo mcus_timeAgoString($diLastLoginToNow);
                }
                echo '';
                ;
                }
                ?>
             </h2>
             <center>
                <table>
                   <tr>
                      <th>Kills</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Deaths</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Ratio</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->ratio, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>
                <table>
                   <tr>
                      <th>Joins</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->joins, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Quits</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->quits, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Kicked</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kicked, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>    
                <table>
                   <tr>
                      <th>Places</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->places, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Breaks</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->breaks, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Chats</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->chats, 0, 50); ?></td>
                   </tr>
                </table>
                </div>
             </center>
             <BR>
       </center>
       </aside>
       </center>
       <center>
          <form class="search" action ="stats.php" method="GET"><input type="text" name="player" autocomplete="off" placeholder="Player Name"><BR><input type="submit" value="Search Stats"></form>
       </center>
       <BR>
       <BR>
       <?php include("layout/footer.php"); ?>
    </body>
    </html>

我在header。php中回显它们我在header。php中使用

包含了配置
 <?php $configs = include('config.php'); ?>

在header。php中我可以回显sitename使用:

 <?php echo $configs['sitename']?>

,但我的网页不能工作。请记住,包含了header.php(其中包含了config.php)。我是一名PHP新手,我很确定我只是把它们听错了。我试图得到$sitename, $url和$color是动态的。

工作版本:

<?php $configs = include('config.php');
if($_GET['player'] === 0) header("Location: http://" + $configs['url']);
 ?>
<head>
       <link rel="icon" 
          type="image/png" 
          href="face.php?u=<?php                     
             $statsJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=stats&player='.$_GET['player']);
              $stats = json_decode($statsJSON);
              if(empty($stats)){
              echo $_GET['player'];
              echo '&s=16';
              } else {
                           echo strtoupper($stats->skin); 
                           echo '&s=16';
             }
             ?>">
       <link rel="apple-touch-icon" 
          type="image/png" 
          href="face.php?u=<?php                     
             if(empty($stats)){
             echo $_GET['player'];
             echo '&s=250';
             } else {
                          echo strtoupper($stats->skin); 
                          echo '&s=250';
             }
             ?>">
       <title><?php echo $configs['sitename'] . " - "; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?></title>
       <meta name="apple-mobile-web-app-title" content="<?php echo ""; if(empty($_GET['player'])){ echo "STATS";} else { echo strtoupper($_GET['player']); }?>">
       <meta name=viewport content="width=device-width, user-scalable=no">
       <meta name="description" content="View <?php echo $_GET['player'];?>'s <?php echo $configs['sitename']?> stats!">
       <style>#profile::before{display:block;height:144px;border-radius:0;background:#0a0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02);content:''}#profile{background:#fafafa;box-shadow:rgba(0,0,0,.14902) 0 1px 1px 0,rgba(0,0,0,.09804) 0 1px 2px 0;padding-bottom:38px;text-align:center;max-width:80%}#profile>img{position:relative;top:-52px;display:block;margin:auto;width:96px;height:96px;border:4px solid #fff;border-radius:0;-webkit-box-shadow:0 4px 0 rgba(0,0,0,.02);box-shadow:0 4px 0 rgba(0,0,0,.02)}#profile h2,#profile h3{font-weight:400;color:#77767e;margin-bottom:30px}#profile h2{margin-top:-30px;padding:0 16px 38px;font-size:21px}#profile h3{margin-top:-60px;padding:0 38px 16px;font-size:11px}#profile h4{font-weight:400}#locked{margin-bottom:-80px}table,td,th{border:1px solid #000;border-collapse:collapse;width:80%}td,th{padding:5px;text-align:left}</style>
    </head>
    <body>
       <?php include("layout/header.php"); ?>
       <BR>
       <BR>
       <center>
          <aside id="profile">
             <?php
                function mcus_timeAgoString($dateInterval) {
                $daysAgo = $dateInterval->days;
                //echo $difference;
                if ($daysAgo == 0){
                if ($dateInterval->h == 0){
                  $diffStr = $dateInterval->i . " Minutes Ago";
                } else {
                   $diffStr = $dateInterval->h . " Hours Ago";
                }
                } else {
                if($daysAgo === 1) {
                  $diffStr = "1 Day Ago";
                } else if($daysAgo === 7) {
                  $diffStr = "1 Week Ago";
                } else if($daysAgo === 14) {
                  $diffStr = "2 Weeks Ago";
                } else {
                  $diffStr = "$daysAgo Days Ago";
                }
                }
                return $diffStr;
                }
                      $get_player = $_GET['player'];
                     $infoJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=info&player='.$_GET['player']);
                      $info = json_decode($infoJSON);
                      $statsJSON = file_get_contents('http://'.$configs['url'].'/api.php?task=stats&player='.$_GET['player']);
                     $stats = json_decode($statsJSON);
                      // $now = time(); // or your date as well`enter code here`
                ?>
             <img src="face.php?u=<?php                     
                if(empty($stats)){
                echo $_GET['player'];
                echo '&s=96';
                } else {
                             echo strtoupper($stats->skin); 
                             echo '&s=96';
                }
                ?>">
             <?php 
                echo '<style>';
                echo '#verified {';
                echo 'fill:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";";
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '.toggle {';
                echo 'background-color:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo '}';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background:';
                echo '#';
                    if(empty($info->theme)){
                echo $configs['color'] . ";"; 
                } else {
                echo substr($info->theme, 0, 50); 
                }
                echo ' !important }';
                echo '</style>';
                ?>
             <?php 
                echo '<style>';
                echo '#profile::before {';
                echo 'background-image:';
                echo 'url(';
                echo substr($info->cover, 0, 50); 
                echo ' ) !important;';
                echo '</style>';
                ?>
             <h2>
                <?php
                   if($info->locked == 1){
                             echo '<div id="locked"><h5>This username is locked.</h5></div>';
                       }else {
                       echo strip_tags($_GET['player']);
                       }
                       ?>
                <?php
                   if(substr($info->verified,0,2) == 1) {
                         echo '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="verified" width="24" height="24" viewBox="0 0 24 24">
                   <path d="M10,17L5,12L6.41,10.58L10,14.17L17.59,6.58L19,8M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3Z" />
                </svg>
                '
                ;
                }
                //print_r ($stats);
                if(substr($info->locked,0,2) == 0) {
                echo '
                <h3>
                ';
                echo 'Logged In ';
                if(empty($info)){
                echo "Not Registered";
                } else { 
                //echo substr($playerAccountInfo->lastlogin,0,50); 
                $dtNow = new DateTime('Now');       
                $dtLastLogin = new DateTime($info->lastlogin);
                $diLastLoginToNow = $dtNow->diff($dtLastLogin);
                echo mcus_timeAgoString($diLastLoginToNow);
                }
                echo '';
                ;
                }
                ?>
             </h2>
             <center>
                <table>
                   <tr>
                      <th>Kills</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Deaths</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Ratio</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->ratio, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>
                <table>
                   <tr>
                      <th>Joins</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->joins, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Quits</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->quits, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Kicked</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->kicked, 0, 50); ?></td>
                   </tr>
                </table>
                <BR>    
                <table>
                   <tr>
                      <th>Places</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->places, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Breaks</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->breaks, 0, 50); ?></td>
                   </tr>
                   <tr>
                      <th>Chats</th>
                      <td><?php echo empty($stats) ? "--" : substr($stats->chats, 0, 50); ?></td>
                   </tr>
                </table>
                </div>
             </center>
             <BR>
       </center>
       </aside>
       </center>
       <center>
          <form class="search" action ="stats.php" method="GET"><input type="text" name="player" autocomplete="off" placeholder="Player Name"><BR><input type="submit" value="Search Stats"></form>
       </center>
       <BR>
       <BR>
       <?php include("layout/footer.php"); ?>
    </body>
    </html>

当使用引号(或将其连接)时,需要在数组周围使用花括号。

http://sandbox.onlinephpfunctions.com/code/28fb84721d5c91b0c48279163c967489923f150b

echo "$configs['sitename']' - ";
应该

echo "{$configs['sitename']}' - ";

或者可以是

echo $configs['sitename'] . "' - ";

文档(用于大花括号(复杂)的使用):http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

代码前面的一个单独问题是

$statsJSON = file_get_contents($configs['url']/api.php?task=stats&player='.$_GET['player'].'');

您需要将数组值与查询字符串

连接起来
$statsJSON = file_get_contents($configs['url'] . '/api.php?task=stats&player='.$_GET['player']);

你应该打开错误报告来获取脚本遇到的所有错误。

如何在PHP中获得有用的错误信息?

要启用并显示错误,添加

error_reporting(-1); // to enable all errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
但是,不要在生产机器上这样做,因为您不希望用户遇到错误。