2012-09-18 38 views
0

感謝您的快速響應!雖然下面的代碼看起來像我想打印的值在屏幕上查看我需要的值例如。 $ user_profile [email]在另一個函數中(註冊新的wordpress用戶)。以下是完整的代碼。我該怎麼做呢?指定一個返回(值)到一個新變量

<?php 
try{ 
include_once "src/fbaccess.php"; 
}catch(Exception $e){ 
error_log($e); 
} 
try{ 
require_once "wp-blog-header.php"; 
}catch(Exception $e){ 
error_log($e); 
} 

try{ 
require_once "wp-includes/registration.php"; 
}catch(Exception $e){ 
error_log($e); 
} 

?> 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <title>Florian's Facebook login</title>  

</head> 
<body> 
<!-- Copy the below code to display FB Login/Logout button --> 
<?php if ($user): ?> 
    <a href="<?php echo $logoutUrl; ?>">Logout</a> 
<?php else: ?> 
    <div> 
    <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> 
    </div> 
<?php endif ?> 
<!-- Copy the above code to display FB Login/Logout button --> 

<h3>PHP Session</h3> 
<pre><?php print_r($_SESSION); ?></pre> 

<?php if ($user): ?> 
    <h3>You</h3> 
    <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> 

    <h3>Your User Object (/me)</h3> 
    <pre><?php return ($user_profile [email]); ?></pre> 
<pre><?php return ($user_profile [first_name]); ?></pre> 
<pre><?php return ($user_profile [last_name]); ?></pre> 
<pre><?php return ($user_profile [location]); ?></pre> 
<?php else: ?> 
    <strong><em>You are not Connected.</em></strong> 
<?php endif ?> 
+0

你使用'return'做什麼?你很可能誤解了它的用途。 – zerkms

+0

我使用return來獲取$ user_profile [email]的值,所以我可以在另一個函數中使用它。我不想打印價值。 –

+0

你仍然不明白它是什麼。請參閱[文檔](http://php.net/return) – zerkms

回答

2

你說你想將其分配給一個變量

$var = $user_profile['email'];

,但在你的例子,它看起來像你想打印:

echo $user_profile['email'];

或啓用短標籤:<?= $user_profile['email']; ?>

+0

我不想打印值,而是需要在另一個函數中使用它。我試過$ var = $ user_profile ['email'];然後,爲了查看它是否有效,試圖通過print_r($ var)打印它;沒有成功。 –

+0

我沒有看到任何功能。請張貼代碼和調用代碼。 –

+0

我仍然看不到函數調用。如果我們不知道您正在嘗試做什麼,我們無法提供幫助。 –

1

基於您的代碼,我覺得你只是想做到以下幾點:

if(!empty($user_profile)) 
{ 
    <pre><?php echo $user_profile['email']; ?></pre> 
    <pre><?php echo $user_profile['first_name']; ?></pre> 
    <pre><?php echo $user_profile['last_name']; ?></pre> 
    <pre><?php echo $user_profile['location']; ?></pre> 
} 
else 
{ 
    <strong><em>You are not Connected.</em></strong> 
} 

你的代碼看起來像你正在嘗試呼應出來,這將工作,但您可能還需要使用的功能empty()檢查變量是否有任何內容。

關於返回數據,這是更常見的功能,而不是其他任何東西 - 這hsz描述很好的另一個回答這個問題。

1

return與函數(或類中的方法)一起工作。正確使用外觀和分配看起來像:

function foo() { 
    return 'bar'; 
} 

$var = $foo(); 

在你的情況,返回值視圖(HTML)是毫無意義的。如所述的Karol Horvath,改爲使用echo

相關問題