2012-11-10 85 views
0

我有5個參數和默認站點的功能寬度可變獲取函數的參數值PHP

$site_width ='1000px'; 
function MyPrintFunction ($divName,$width=false,$html1='',$html2='', $echo = true){ 
     ... plenty of code above, no need to post complete function 

     $html = '<div id="'.$divName.'">'; 
     $html .= '</div>'; 
     if($echo){ 
      echo $html1.$html.$html2; 
     }else{ 
      return $html1.$html.$html2; 
     } 


} 

,比我打印HTML這樣的:

MyPrintFunction ('divname1',true); 
MyPrintFunction ('divname2'); 
MyPrintFunction ('divname3'); 

我需要檢查是否有這些功能已經將第二個參數設置爲true,如果是,我需要第一個參數名稱。

這樣我可以添加參數名稱的CSS代碼段(將在後面的頭被注入),並設置寬度爲它

#ARrument1NameSHouldGoHereAsVariable{ 
    width:$site_width; 
} 

我希望我解釋了這一權利。對不起,如果我沒有。任何幫助表示讚賞。謝謝!

+0

@ L0j1k不知道我明白 – Benn

+0

如果你在那裏創建div,爲什麼不把寬度作爲div的'style'呢?看起來這會更容易。 –

+0

@ G-Nugget哈哈,你有我了,我已經有了它的功能,並且想把它從內聯中取出並放在它所屬的地方, – Benn

回答

1

野應不,不,你絕對可以有可選參數。這是一個有點混亂的情況,但完全有可能。

echo MyPrintFunction('dafsd',435); 
function MyPrintFunction ($divName,$width=2342){ 
    return $divName . $width; 
} 

它氣餒的原因是因爲... 讓我們假設你想設置的高度,而不是一個寬435個

echo MyPrintFunction('dafsd',435); 
function MyPrintFunction ($divName,$width=2222, $height=1111){ 
    return $divName . $width . $height; 
} 

可選參數,通常不應使用,如果他們是將它們放置在最後一個數組

請哦,請讓這是你正在尋找什麼......

function MyPrintFunction ($divName,$width=false){ 
    $newObj = array(); 
    $newObj['divName'] = $divName; 
    $newObj['width'] = $width; 
    return $newObj; 
} 
function hasWidth($newObj) { 
    if ($newObj['width']) { 
     return $newObj['width']; 
    } 
    else { 
     return 'no width!'; 
    } 
} 
$tempStore1 = MyPrintFunction('dafsd',123); 
$tempStore2 = MyPrintFunction('dafsd'); 
echo hasWidth($tempStore1); 
echo hasWidth($tempStore2); 
+0

以及這是我所擁有的,但又一次,有人可以幫助我,如果這一切都可能找出第一個和第二個參數在函數之外的是什麼? – Benn

+0

我沒有發佈完整的功能,是的我檢查打印前定義的所有參數,完整的功能比上面的例子大得多,但我的問題不是我的功能如何制定 – Benn

+0

你是否在尋找類似http:// php.net/manual/en/function.func-get-args.php –

1

這是幹什麼用的?

function MyPrintFunction ($divName,$width=false,$html1='',$html2='', $echo = true){ 

的邏輯是這樣的

<?php 
function blahblah($param1, $param2) { 
//Put some weirdy stuff here 

if($param2 == '1') { 
//Than it's true 
    } else { 
//false 
    } 
} 
blahblah("div1", "1"); //This is true 
blahblah("div1", "0"); //This is false 
?> 
+0

這是所有可能的選項,html之前或之後的主要div名稱等 – Benn

+0

但嘗試這種邏輯,因爲你的代碼看起來非常混亂 –

+0

井會做但有點仍然沒有給我一個線索我的問題。如何獲得函數參數值 – Benn