如何在href中传递多个变量?

时间:2020-08-09 18:31:46

标签: php wordpress variables

我根据类似的问题尝试了一些代码,但一直出错。基于this,我尝试了类似的问题:

$step1 = 'my_shop';
$step2 = 'https://my-website.com/';
echo '<a href="' .$step2. .$step1. '">';

按照this类似问题的逻辑,我也尝试过:

$step1 = 'my_shop';
$step2 = 'https://my-website.com/';
echo '<a href="' .$step2&$step1. '">';

1 个答案:

答案 0 :(得分:2)

您应该使用http_build_query()函数,因为它是为此设计的:

$data = array(
    'foo' => 'bar',
    'baz' => 'boom',
    'cow' => 'milk',
    'php' => 'hypertext processor'
);

然后:

$base_url = 'https://my-website.com/';
$query_params = http_build_query($data);
$link = $base_url . '?' . $query_params;

echo '<a href = "' . $link . '">link text</a>';

结果:

<a href = "https://my-website.com/?foo=bar&baz=boom&cow=milk&php=hypertext+processor">link text</a>