将返回值(值)分配给新变量

时间:2012-09-18 07:23:13

标签: php

感谢您的快速回复!虽然下面的代码看起来像我想打印要在屏幕上查看的值,但我需要值,例如。 $ 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 ?>

3 个答案:

答案 0 :(得分:2)

你说你想把它分配给一个变量

$var = $user_profile['email'];

但在您的示例中,您似乎要打印它:

echo $user_profile['email'];

或启用了短标记:<?= $user_profile['email']; ?>

答案 1 :(得分: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在这个问题的另一个答案中很好地描述了。

答案 2 :(得分:1)

return适用于函数(或类中的方法)。正确使用外观和分配看起来像:

function foo() {
  return 'bar';
}

$var = $foo();

在您的情况下,返回视图(HTML)的值是没有意义的。如Karol Horvath所述,请改用echo

相关问题