php在字符串连接中调用函数

时间:2014-02-13 12:26:51

标签: php

我在字符串连接中调用函数时遇到问题

我有这个功能:

function  site_url() {
    include("../connect.php");
    $title= mysql_query("select * from shop_option where shop_key='site_url' ");
    $row_title= mysql_fetch_array($title);
    print ''.$row_title['shop_feild'].'';
}

此功能会告诉网站网址例如(http://google.com

我希望在字符串连接中调用此函数

echo '<script language="javascript">'.'window.location =
    "'.site_url().'";'.'</script>';

我也把函数称为变量!测试,但它不起作用

$page = site_url();
echo '<script language="javascript">'.'window.location = "'.$page.'";'.'</script>';

那么当我想要串联连接时如何调用函数?

4 个答案:

答案 0 :(得分:3)

您需要返回site_url的结果,而不是立即打印文本。

function  site_url() {
    include("../connect.php");
    $title= mysql_query("select * from shop_option where shop_key='site_url' ");
    $row_title= mysql_fetch_array($title);
    return $row_title['shop_feild'];
}

答案 1 :(得分:1)

您的功能不应该打印,但会返回值:

function  site_url() {
    include("../connect.php");
    $title= mysql_query("select * from shop_option where shop_key='site_url' ");
    $row_title= mysql_fetch_array($title);
    return''.$row_title['shop_field'].'';
}

以便将其传递给调用者,然后调用者打印返回的值。

答案 2 :(得分:0)

使用return:

而不是打印
function  site_url() {
    include("../connect.php");
    $title= mysql_query("select * from shop_option where shop_key='site_url' ");
    $row_title= mysql_fetch_array($title);
    return $row_title['shop_field'];
}

答案 3 :(得分:0)

您的函数site_url()不会返回所选的值。

function  site_url() {
  include("../connect.php");
  $title= mysql_query("select * from shop_option where shop_key='site_url'");
  $row_title = mysql_fetch_array($title);
  return $row_title['shop_feild'];
}