2010-10-08 36 views
-1

這是我的代碼 http://www.ideone.com/R1P4b在PHP函數的一些錯誤

我使用simple_html_dom類

在文件 結束,如果我敲響一行是沒有錯誤,如果我2梯級線返回一個錯誤

echo getImg($text1) . "<br/>"; 
echo getImg($text2) . "<br/>"; 
error 

echo getImg($text1) . "<br/>"; 
or 
echo getImg($text2) . "<br/>"; 

不要錯誤

請幫我解決它

+1

什麼是錯誤? – 2010-10-08 19:11:51

+0

是什麼錯誤信息? – Sarfraz 2010-10-08 19:12:30

+0

你爲什麼要嵌套功能? – halfdan 2010-10-08 19:15:16

回答

5

聲明功能isbnFromText()isbn2Image()imagePix()getImg()

我不確切知道PHP如何處理在另一個函數中聲明的函數,但顯然它將它們放入同一個作用域,如果你運行兩次外部函數,它們會再次聲明。

實施例:

function a() { 
    function b() { 
     print 'foo'; 
    } 
    b(); 
} 

a(); 
a(); 

打印

foo 
Fatal error: Cannot redeclare b() (previously declared in /t.php:4) on line 3 

更新:

Learn more about functions,特別是實施例3還表示有:

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

+0

在我的代碼中,你能幫我修復它嗎? – Thoman 2010-10-08 19:40:34

+0

@Thoman:你是什麼意思?你有沒有讀過我答案的第一句話?你做了它,仍然得到錯誤? – 2010-10-08 19:41:42

+0

對不起,因爲我沒有看到它:)) – Thoman 2010-10-08 19:45:16