2015-01-12 289 views
-2

在PHP我有一個功能PHP - 傳遞變量

ob_start(); 
     echo "<p>Your Feed URL - <a href='/blah'>Click Here</a></p>"; 
     $feedURL = ob_get_contents(); 
     ob_end_clean(); 

現在這個存儲我的ECHO爲名爲$ feedURL一個局部變量,我需要在我的PHP腳本的另一部分使用這個變量的代碼。每次發佈表單時都會生成echo語句。我需要的是當表單被POST時,所創建的變量可以在函數的腳本的另一部分中使用。

我試過使用return $ var;

當我然後嘗試echo $ var在一個單獨的函數它返回一個空字符串?

我是愚蠢的還是有我失蹤的東西?

我想在這個其他函數中使用該變量。

function add_page_content_Send_Feed(){ 
echo "<h2>Send your feed via email</h2>"; 

echo "<p>Below you can send your feed URL. Just enter the email you wish to send to.</p>"; 
echo "<p>This email will contain the URL leading to your feed</p>"; 
echo "<form id='post' action='http://localhost/wp/ultrait/admin.php?page=page6' method='POST'>"; 
echo "<label for='email'>Email:</label> <br /> <input type ='text' value = '' name = 'email' id  = 'email'/><br /><br />"; 
echo "<input type='submit' name='send_feed' value='Send my feed' id='submit'/>"; 
echo "</form>"; 

// Example using the array form of $headers 
// assumes $to, $subject, $message have already been defined earlier... 

$headers[] = 'From: UltraIT <[email protected]>'; 
$subject = 'Feed URL from UltraIT Property Management'; 
$test = "blah"; 
$message = 'Hello, please see your feed URL below. ' . $test; 


if(isset($_POST['send_feed'])){ 
if(empty($_POST['email'])) // If no email has been specified 
{ 
    echo "<p>Please enter an Email</p>"; 
} 
else{ 
    $email = $_POST['email']; 
    print ($email); 
} 

    $to = $email; 
    wp_mail($to, $subject, $message, $headers); 

}}

我需要的變量是$消息變量的值。

+4

顯示你的代碼,以及如何使用您的話,那麼我們也許能夠幫助你。 – NoLiver92

回答

0

要從任何function訪問它,你可以使用global關鍵字,但它不是一個好的做法,使用函數param來保持你的代碼鬆散耦合。

function myFunc() { 
    global $feedURL; 
    //then use it 
} 

確保你已經在上面包含了上面的文件。

更新

你需要傳遞$feedURL作爲參數在你的功能

add_page_content_Send_Feed($feedURL) { 
    ... 
    $message = 'Hello, please see your feed URL below. ' . $feedURL; 
    ... 
} 
+1

downvoting爲使用* global *在函數體=>是不好的praxis – donald123

+0

嗨@ donald123我已經完成了我的答案,請參閱。 – Saqueib

+0

也許你應該考慮在發佈之前完成它 – NoLiver92