2009-12-04 142 views
1

我遇到了這個函數的問題。它應該回應一些東西,但由於某種原因,當我打電話時它不會這樣做。PHP函數調用時不會執行

這裏的功能:

$count = count($info['level']); 
function displayInfo() 
{ 
    for ($i=0; $i<$count; $i++) 
    { 
    echo "[b]".$info['name'][$i]."[/b]\n\n" 
    ."Level: ".$info['level'][$i] 
    ."\nPrice: ".$info['price'][$i] 
    ."\nSellback: ".$info['sell'][$i] 
    ."\nLocation: ".$location 
    ."\n\nType: ".$info['type'][$i] 
    ."\nElement: ".$info['element'][$i] 
    ."\nDamage: ".$info['damage'][$i] 
    ."\nBTH: ".$info['bth'][$i] 
    ."\n\nSPECIAL" 
    ."\nHits: ".$info['hits'][$i] 
    ."\nType: ".$info['stype'][$i] 
    ."\nElement: ".$info['selement'][$i] 
    ."\nDamage: ".$info['sdamage'][$i] 
    ."\nBTH: ".$info['sbth'][$i] 
    ."\nRate: ".$info['rate'][$i] 
    ."\n\nDescription\n".$description 
    ."\n".$img 
    ."\n\n\n"; 
    } 
} 

這裏是我用來調用函數的代碼:

<?PHP 
    displayInfo(); 
?> 

我無法找出什麼是錯的 - 它不是一個sintax錯誤,頁面無中斷負載。

在此先感謝。

+0

爲了測試是否被執行的功能,你應該使用'你的功能下一次的開始echo'甚至'exit'添加一個簡單的調試語句。正如你在答案中看到的那樣,你的函數*被執行,只是以不同於預期的方式執行...... – Franz 2009-12-04 19:57:24

回答

8

你聲明你的函數的$count$info變量外:

// $info already exists 
$count = count($info['level']); // and $count is initialized here 
function displayInfo() 
{ 
for ($i=0; $i<$count; $i++) 
... 

在PHP中,函數外部聲明的變量在函數內部是不可見的。


如果你希望你的「外部」變量是從函數內部可見,你必須把他們定義爲global的功能:

$count = count($info['level']); 
function displayInfo() 
{ 
global $count, $info; 
// $count is now visible ; same for $info 
for ($i=0; $i<$count; $i++) 
... 


但它通常被認爲是更好的傳遞變量作爲參數傳遞給函數:你必須把他們定義爲一個參數:

function displayInfo($count, $info) 
{ 
for ($i=0; $i<$count; $i++) 
... 

並傳遞給函數時愈傷組織ng:

$count = count(...); 
displayInfo($count, $info); 

傳遞參數而不是使用全局變量可以確保您知道您的函數有權訪問和修改。


編輯:感謝您的注意,X-Istence!沒:-(

+1

$ info從哪裏來? – 2009-12-04 19:49:50

5

$ count和$ info被聲明在函數之外,因此它們在其中不可見。你可以通過$信息到函數,然後計算$內算,這樣的:

//get info from db, or somewhere 
$info = array();  

displayInfo($info); 

function displayInfo($info) 
{ 
    $count = count($info['level']); 
    //now $count and $info are visible. 
} 

http://php.net/manual/en/language.variables.scope.php

+1

$ info從哪裏來? – 2009-12-04 19:49:18

+0

是啊,我太快了,沒有正確閱讀 – 2009-12-04 19:50:59

3

讀夠給定的代碼添加global對於應當從功能訪問變量,而不是參數:

function displayInfo() 
{ 
    ## bad design 
    global $count, $info; 
    ... 

或者通過您的數組作爲參數:

function displayInfo($info) 
{ 
    ## this is local variable accessable only inside function 
    $count = count($info['level']); 
    ... 
} 

調用它

<?php 
    ## don't forget to pass array as parameter 
    displayInfo($info); 
?> 
+0

哇,謝謝你們的快速回復! $ info是一個外部數組,我沒有意識到它必須在函數內聲明。 再次感謝! – Hussain 2009-12-04 19:52:16

1

您可以將您的$數設定的功能內離子,或將其傳遞給函數。另外,你的$ info數組也必須傳遞給函數,或者是全局的。這裏是計數和信息被傳入函數的函數原型。

function displayInfo($count, $info) 
{ 

    for ($i=0; $i<$count; $i++) 
    { 
    // Do stuff 
    } 
} 

<?php 
    $count = count($info['level']); 
    displayInfo($count, $info); 
?> 
0

事實上,沒有語法錯誤,但還有其他錯誤,正如其他反應中指出的那樣。我強烈建議配置PHP以在編寫新代碼時顯示所有錯誤。這樣你就可以看到關於$ info和$ count的通知沒有在你的函數中定義。

您可以通過幾種方式打開錯誤。

  1. 配置您的服務器來執行此操作。
  2. 使用.htaccess文件打開它們
  3. 在腳本的一開始就使用以下PHP代碼。

例子:

error_reporting(E_ALL | E_NOTICE); //turn on all errors 
ini_set("display_errors","On"); //activate display_error