2011-10-07 53 views
1

我有經過了兩個默認值參數的函數...PHP函數語法查詢

function places($location="Minneapolis", $lodging="Mom's house") 
{ 
    echo "enjoys going to {$location} and staying at {$lodging} while on vacation."; 
} 
places("St. Paul","Grandma's house"); 

我需要使用定義爲通過一個可變的10個人不同的名字來傳遞功能的10倍論據。語法假設輸出類似於這個:

喬喜歡去聖保羅,並在度假期間住在奶奶的家。

+0

首先,我建議你不要在函數中使用'echo';相反,返回一個字符串或創建的字符串數組。 –

回答

0

看到代碼我的意見。您沒有將$i迭代器設置爲有效的PHP變量,所以FYI:所有PHP變量的前綴必須爲$

<?php 

// You declare your functions typically in the global scope, not 
// within a for or any other loop. 
// NOTE: $name is a required function parameter in this function. 

function places($name, $location="Minneapolis", $lodging="Mom's house") { 
    return "$name enjoys going to $location and staying at $lodging while on vacation."; 
} 

// Note, I've got $people setup to have arrays that can be passed 
// containing a "name, city, hotel" syntax. This is equivalent to 
//  $people[loop index][0] ~ $people[loop index][name] 
//  $people[loop index][1] ~ $people[loop index][city] 
//  $people[loop index][2] ~ $people[loop index][hotel] 

$people = array(
    array("James", "Brooklyn", "Granada Inn"), 
    array("Betsy", "Memphis", "Tennessee Hotel"), 
    array("Andrew", "San Francisco", "101 Hotel"), 
    array("Marvin", "San Diego", "Oceanview Beach Resort"), 
    array("Sara", "Orlando", "Disney World"), 
    array("Alicia", "Hilton Head", "Vincent Inn") 
); 

// Cache the count of the $names array members 

$c_people = count($people); 

// Loop and echo. 

for ($i = 0; $i < $c_people; $i++) { 
    echo places($people[$i][0], $people[$i][1], $people[$i][2]) . "\n"; 
} 

?> 

http://codepad.org/QHh83cKz

產出

James enjoys going to Brooklyn and staying at Granada Inn while on vacation. 
Betsy enjoys going to Memphis and staying at Tennessee Hotel while on vacation. 
Andrew enjoys going to San Francisco and staying at 101 Hotel while on vacation. 
Marvin enjoys going to San Diego and staying at Oceanview Beach Resort while on vacation. 
Sara enjoys going to Orlando and staying at Disney World while on vacation. 
Alicia enjoys going to Hilton Head and staying at Vincent Inn while on vacation. 
+0

嘿Jared!是的,這個是很棒的。非常感謝。我現在知道很少的PHP並參加了幾個在線課程。我知道有幾種編碼方式,而這正是我設想的這種操作方式。再次感謝! –

+0

確保並將其標記爲答案(通過單擊此答案旁邊的勾選的複選標記)並註冊。 ':)' –

1

你的意思是這樣的嗎?

function places($location="Minneapolis", $lodging="Mom's house") 
{ 
    echo "enjoys going to {$location} and staying at {$lodging} while on vacation.\n"; 
} 

$loc = array(
    array('location'=>'St. Paul1', 'lodging' => 'Grandma\'s house1'), 
    array('location'=>'St. Paul2', 'lodging' => 'Grandma\'s house2'), 
    array('location'=>'St. Paul3', 'lodging' => 'Grandma\'s house3'), 
    array('location'=>'St. Paul4', 'lodging' => 'Grandma\'s house4'), 
    array('location'=>'St. Paul5', 'lodging' => 'Grandma\'s house5'), 
    // etc 
); 

foreach($loc as $i) 
{ 
    places($i['location'], $i['lodging']); 
} 
0

如果我正確地讀你的問題,那麼所有你需要的是另一種參數

function places($location="Minneapolis", $lodging="Mom's house", $name="Bob") 
{ 
    echo "{$name} enjoys going to {$location} and staying at {$lodging} while on vacation."; 
} 
places("St. Paul","Grandma's house","Joe"); 
4

怎麼這樣呢?

$names = explode(',', 'James ,Betsy ,Andrew ,Marvin ,Alicia ,etc... '); 

foreach($names as $name) 
{ 
    echo $name, places(), '<br>'; 
} 

Demo

+0

這是僞代碼嗎? –

+0

這是僞代碼,我把它壓縮成了工作代碼。 – hakre

+0

@hakre - 好吧,按分類排序。我會假設每個名稱都應該是它自己的數組成員,並且函數參數不會被用於數組或其他任何方式。 –