【问题标题】:PHP Fatal error break;PHP 致命错误中断;
【发布时间】:2011-09-20 19:06:17
【问题描述】:

我正在使用一个 wordpress 插件,该插件从雅虎财经获取股票报价提要。它只是很少发生......否则它不会出现。我在第 140 行随机收到致命错误...并且第 140 行中断;我正在复制下面的报价以了解代码。编辑:现在我已经把整个 PHP 文件...!!

<?php

/* 插件名称:股票行情 插件 URI:http://www.dagondesign.com/articles/stock-quotes-plugin-for-wordpress/ 描述:在您的网站上显示股票报价 作者:达贡设计 版本:1.0.1 作者 URI:http://www.dagondesign.com */

$ddsq_version = '1.0.1';

// 如果选项不存在,则设置默认值 add_option('ddsq_format', '%NAME%: %LAST% (%CHANGE%)'); add_option('ddsq_up_color', '00BB00'); add_option('ddsq_down_color', 'FF0000'); add_option('ddsq_update_time', '10');

函数 ddsq_add_option_pages() { if (function_exists('add_options_page')) { add_options_page('股票行情', 'DDStockQuotes', 8, FILE, 'ddsq_options_page'); }
}

函数 ddsq_options_page() {

global $ddsq_version;

if (isset($_POST['set_defaults'])) {
    echo '<div id="message" class="updated fade"><p><strong>';

    update_option('ddsq_format', '<strong>%NAME%</strong> %LAST% [<strong>%CHANGE%</strong>]');
    update_option('ddsq_up_color', '00BB00');
    update_option('ddsq_down_color', 'FF0000');
    update_option('ddsq_update_time', '10');

    echo 'Default Options Loaded!';
    echo '</strong></p></div>';

} else if (isset($_POST['info_update'])) {

    update_option('ddsq_format', (string) $_POST["ddsq_format"]);
    update_option('ddsq_up_color', (string) $_POST["ddsq_up_color"]);
    update_option('ddsq_down_color', (string) $_POST["ddsq_down_color"]);
    update_option('ddsq_update_time', $_POST["ddsq_update_time"]);

    echo 'Configuration Updated!';
    echo '</strong></p></div>';

} ?>

<div class=wrap>

<h2>Stock Quotes v<?php echo $ddsq_version; ?></h2>

<p>For information and updates, please visit:<br />
<a href="http://www.dagondesign.com/articles/stock-quotes-plugin-for-wordpress/">http://www.dagondesign.com/articles/stock-quotes-plugin-for-wordpress/</a></p>


<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<input type="hidden" name="info_update" id="info_update" value="true" />


<h3>Usage</h3>
<p>There are two ways you can display quotes:</p>
<p><strong>1) In a post or page</strong>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
[stock MSFT]</p>
<p><strong>2) In a template file</strong>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;?php echo ddsq_get_quote('MSFT'); ?&gt;</p>


<h3>Options</h3>
<table width="100%" border="0" cellspacing="6" cellpadding="4">

<tr valign="top"><td width="13%">
    <strong>Output Format </strong>
</td><td align="left">
    <input name="ddsq_format" type="text" size="100" value="<?php echo htmlspecialchars(stripslashes(get_option('ddsq_format'))) ?>"/>
    <br />Placeholders for data: <strong>%NAME% %LAST% %CHANGE%</strong>
</td></tr>

<tr valign="top"><td width="13%">
    <strong>Up Color </strong>
</td><td align="left">
    <input name="ddsq_up_color" type="text" size="7" value="<?php echo get_option('ddsq_up_color') ?>"/>
     Color to use for price change when up (leave blank to disable)
</td></tr>

<tr valign="top"><td width="13%">
    <strong>Down Color </strong>
</td><td align="left">
    <input name="ddsq_down_color" type="text" size="7" value="<?php echo get_option('ddsq_down_color') ?>"/>
     Color to use for price change when down (leave blank to disable)
</td></tr>

<tr valign="top"><td width="13%">
    <strong>Update Time </strong>
</td><td align="left">
    <input name="ddsq_update_time" type="text" size="3" value="<?php echo get_option('ddsq_update_time') ?>"/>
    Number of minutes to wait, before fetching updated stock prices
</td></tr>
</table>

<div class="submit">
    <input type="submit" name="set_defaults" value="<?php _e('Load Default Options'); ?> &raquo;" />
    <input type="submit" name="info_update" value="<?php _e('Update options'); ?> &raquo;" />
</div>

</form>
</div><?php

}

函数 ddsq_get_quote($symb) {

$up_color = trim(get_option('ddsq_up_color'));
$down_color = trim(get_option('ddsq_down_color'));
$quote_format = stripslashes(get_option('ddsq_format'));
$update_time = trim(get_option('ddsq_update_time'));

$update_time = $update_time * 60;
$t_out = '';
$time_diff = '';

// Trying to open local cached file first
$file_path = WP_PLUGIN_DIR . "/" . $symb . ".txt";
if (file_exists($file_path)){
    $time_diff = date('U') - filemtime($file_path);
}

$lf = @fopen($file_path, "r");
if (($lf == FALSE) || ($time_diff >= $update_time)) {
    $url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symb . "&f=sl1c1";
    $fp = @fopen($url, "r");
    if ($fp == FALSE) {
        #$t_out = 'Error opening: ' . htmlspecialchars($url);
        break;
    } else {
        $array = @fgetcsv($fp , 4096 , ', ');
        @fclose($fp);
        $sq_name = $array[0];
        $sq_last = $array[1];
        $sq_change = $array[2];

        $col = NULL;
        if (($sq_change[0] == '-') && ($down_color != '')) {
            $col = $down_color;
        } else if (($sq_change[0] == '+') && ($up_color != '')) {
            $col = $up_color;
        }

        if ($col !== NULL) {
            $sq_change =  '<span style="color: #' . $col . ';">' . $sq_change . '</span>';
        }

        $t_out = $quote_format;
        $t_out = str_replace('%NAME%', $sq_name, $t_out);
        $t_out = str_replace('%LAST%', $sq_last, $t_out);
        $t_out = str_replace('%CHANGE%', $sq_change, $t_out);

        $cache_file = fopen($file_path, "w+");
        fwrite($cache_file, $t_out);
    }
} else {
    fclose($lf);
    $t_out = file_get_contents($file_path);
    if ($t_out == FALSE){
        echo "ERROR opening cache file";
    }
}

return $t_out;

}

函数 ddsq_process($content) {

$results = array();

preg_match_all("/\[\s?stock\s?(.*)\s?\]/", $content, $results);

$i = 0;
foreach ($results[0] as $r) {

    $content = str_replace($r, ddsq_get_quote($results[1][$i]), $content);
    $i++;
}

return $content;

}

add_filter('the_content', 'ddsq_process'); add_action('admin_menu', 'ddsq_add_option_pages');

?>

【问题讨论】:

  • 致命错误只是说“在第 140 行”?这很奇怪。
  • 那里有for/while循环,否则那里没有break的理由。
  • 是的,发布所有代码会很有用。你已经把它切断了。
  • 刚刚发布了更多代码。我想现在更清楚了。!!
  • @Imran Jafri 如果您使用的是for, foreach, while, do-while or switch structure,每个人都很好奇,请参阅手册 (php.net/manual/en/control-structures.break.php)。只要您发布的内容之前有代码,我们就不会知道。

标签: php wordpress


【解决方案1】:

据我所知,没有循环或控制结构(如switch)在其内部接受break;。我猜,您有时会在循环中调用此代码。在这种情况下,break; 是允许的。但有时并非如此。 break;if-block 中根本没有任何意义。

【讨论】:

  • 所以我应该注释掉break; ??
  • “我猜,您有时会在循环中调用此代码。在这种情况下,break; 是允许的。” PHP 真的允许你这样做吗?
  • @Imran:完全删除它。 @phant0m:事实上这就是主要目的:打破循环。
  • @KingCrunch:我误解了这一点。我以为你的意思是,调用一个包含 break 的函数,它将退出函数和循环......我真的很困惑 :) @mhitza:我在使用 include 时也遇到了错误(我希望得到一个)。
  • 我现在已经发布了整个 php 文件。
【解决方案2】:

我不确定文件指针是否可以评估为 false,但在检查 fopen 是否有错误时,您需要使用:

if ($fp === FALSE)

除此之外,看看@mhitza 的评论。

【讨论】:

  • 我完全是新手这个插件是我的开发者制作的。我只是想在打扰他之前修复它:)
猜你喜欢
  • 2018-11-13
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多