如何在PHP中的数字和字符中使用base URL?

How to use base URL inside number and character in PHP?

我的基数 URL 是 http://localhost/test/。我有一个名为 Click me 的锚标记。我检查了查看源它显示为

`<a href='http://localhost/test/abdah.0`p)dp'>Click me</a>` 

应该是这样显示的

`<a href='http://localhost/test/index.php/example/index/4&action=print'>Click me</a>`

我试过代码,但它不起作用。

<?php
      $session_stud_id=4;
      $bg_url='index.php/example/index/'.$session_stud_id;
      $action_url="action=print"; 
      echo $main_url=$bg_url&$action_url;//output abdah.0`p)dp 
      ?>
  <a href="<?php echo base_url($main_url);?>" >Click me</a>

我得到了一些像这样的垃圾值 abdah.0p)dp`。 你能帮我解决这个问题吗?

您必须使用 . 连接多个字符串 将您的 main_url 更改为:

echo $main_url=$bg_url."&".$action_url;

您没有在此行正确连接字符串

$main_url=$bg_url&$action_url;

应该是这样的

$main_url=$bg_url . "&" . $action_url;

&就是PHP bitwise AND operator。根据该手册页,当您将其与字符串结合使用时:

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.

所以你的两个字符串 index.php/example/index/action=print 正在像这样逐个字符地计算。对于第一个字符:

Char Ascii Binary
a    97    1100001
i    105   1101001

这些二进制值是这样计算的:

  1100001
  1101001
& 1100001

在这种情况下,我们最终返回值 97,这等同于 a,显示在您的最终字符串中。对于较短字符串长度之后的值,将根据零计算这些值,这会导致不添加任何字符。

您可能想使用

$main_url = "{$bg_url}?{$action_url}";
echo $main_url;

(其他答案提到只是与 & 连接,但由于您的 $bg_url 没有现有的查询字符串,我认为您可能需要使用 ?