2015-01-12 207 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