简体   繁体   English

为什么 PHP echo 返回不完整的链接?

[英]Why does PHP echo return an incomplete link?

So I have the following code:所以我有以下代码:

echo '<li><a href="'.$Website.$Elements[2].'/'.$Row["povezava"].'" class="leftMenuArticlesInactive"></a><li>';
  1. $Website contains: http://www.somesite.com/test/ $网站包含: http : //www.somesite.com/test/
  2. $Elements[2] contains: sometext $Elements[2] 包含:sometext
  3. $Row["povezava"] contains: sometext2 $Row["povezava"] 包含:sometext2

All variables have nothing but letters in them.所有的变量都只有字母。

Now my problem.现在我的问题。 If I try to echo the following I get:如果我尝试回应以下内容,我会得到:

// I'm missing "sometext":
http://www.somesite.com/test/sometext2
// Should be like this:
http://www.somesite.com/test/sometext/sometext2

If I do print_r($Elements) I can see that $Elements[2] is "sometext".如果我执行 print_r($Elements) 我可以看到 $Elements[2] 是“sometext”。

Why can't I echo $Elements[2] along with a few other variables?为什么我不能将 $Elements[2] 与其他一些变量一起回显?

edit: added more code编辑:添加了更多代码

$Website = 'http://' . $_SERVER["SERVER_NAME"] . '/' . $Elements[1] . '/';

function ShowMenuOfParent($Parent, $Spacing) {
    $Query = mysql_query("SELECT * FROM `izdelki_meni` WHERE `stars` = '$Parent';");
    if(mysql_num_rows($Query) != 0) {
        while($Row = mysql_fetch_array($Query)) {
            $s = '<li><a href="'.$Website.$Elements[2].'/'.$Row["povezava"].'" class="leftMenuArticlesInactive">';
            for($i = 0; $i < $Spacing;$i++) {
                $s .= '&nbsp;';
            }
            $s .= $Row["ime"].'</a></li>';
            echo $s;
            ShowMenuOfParent($Row["id"], $Spacing + 8);
        }
    }
}

$Query = mysql_query("SELECT * FROM `izdelki_meni` WHERE `stars` = 0;");
if(mysql_num_rows($Query) != 0) {
    echo '<ul>';
    while($Row = mysql_fetch_array($Query)) {
        echo '<li><a href="'.$Website.$Row["povezava"].'" class="leftMenuArticlesActive">'.$Row["ime"].'</a></li>';
        if($Elements[2] == $Row["povezava"]) {
            ShowMenuOfParent($Row["id"], 8);
        }
    }
    echo '</ul>';
}

Why can't I echo $Elements[2] along with a few other variables?为什么我不能将 $Elements[2] 与其他一些变量一起回显?

Actually, echo can do that, so only this part of your question remains unresolved:实际上, echo可以做到这一点,所以只有这部分问题仍未解决:

Why can't I?为什么我不能?

What you can and what you can not often depends on what you've learned so far to do.你能做什么和不能做什么通常取决于你到目前为止学到了什么。 As learning is subjective, it's hard to answer that question by somebody else than you.由于学习是主观的,因此除了您之外的其他人很难回答这个问题。 But probably you can add more information what especially hinders you to understand your actual problem so others can better assist you.但也许您可以添加更多信息,特别是阻碍您了解实际问题的信息,以便其他人可以更好地帮助您。


The information you provide with your question is factually wrong.您在问题中提供的信息实际上是错误的。 I can understand you have a problem and you would like to fix it, but what you actually do is that you make your problem bigger than it is by posting it here as an invitation for others to even do more guessing.我可以理解你有一个问题,你想解决它,但你实际上做的是把你的问题张贴在这里,作为邀请其他人做更多猜测的邀请。 I mean 10 persons are able to guess more than one person.我的意思是 10 个人能猜出不止一个人。 So the way you ask is actually counter-productive and not helpful.所以你问的方式实际上适得其反,没有帮助。 Both for yourself as anybody else.既为自己也为其他人。

Instead of guessing you need to start to actually proof things step by step.你需要开始一步一步地实际证明事情,而不是猜测。 Start with your own expectation:从你自己的期望开始:

Now my problem.现在我的问题。 If I try to echo the following I get:如果我尝试回应以下内容,我会得到:

// I'm missing "sometext":
http://www.somesite.com/test/sometext2
// Should be like this:
http://www.somesite.com/test/sometext/sometext2

And the code you've posted that is related to the output is:您发布的与输出相关的代码是:

echo '<li><a href="'.$Website.$Elements[2].'/'.$Row["povezava"].'" class="leftMenuArticlesInactive"></a><li>';

You write that $Website ends with /test/ , and additionally there is a slash in the output string ( .'/'. ), however the output itself does not even contain that extra string according to you:您写道$Website/test/结尾,并且输出字符串( .'/'. )中还有一个斜杠,但是根据您的说法,输出本身甚至不包含该额外字符串:

http://www.somesite.com/test/sometext2

And that is already regardless what $Elements or $Row contains.这已经与$Elements$Row包含的内容无关。 Start thinking first, you need to analyze why that happens, and others can not help your with that, because we don't have your software and your data to debug it.首先开始思考,需要分析为什么会发生这种情况,其他人无法帮助你,因为我们没有你的软件和数据来调试它。 With some time you will learn your own best ways to find the mistakes you do.一段时间后,您将学会自己找出错误的最佳方法。

Take the time to setup yourself the xdebug debugger and step through your code then.花点时间为自己设置 xdebug 调试器,然后逐步执行代码。 It helps you to locate errors very quickly.它可以帮助您非常快速地定位错误。 And not only this time but also in the future.不仅这次,而且在未来。

You seem to be printing unescaped text inside an HTML attribute that's part of a URL.您似乎在作为 URL 一部分的 HTML 属性中打印未转义的文本。 My educated guess is that you are inspecting the scripts's output as the browser renders it, eg by hovering the mouse and reading the tooltip.我有根据的猜测是您正在检查浏览器呈现脚本的输出,例如通过悬停鼠标并阅读工具提示。 Instead, use your browser's "View Source" feature to see the exact output.相反,请使用浏览器的“查看源代码”功能查看确切的输出。

Additional tips:附加提示:

  1. var_dump() is by far the best built-in function for debugging purposes. var_dump()是迄今为止最好的用于调试的内置函数。 All other functions either cast variables to strings or can hide some important information.所有其他函数要么将变量转换为字符串,要么可以隐藏一些重要信息。

  2. Abort the script with die() or exit() right after the offending line to ensure the output is not altered later.在有问题的行之后立即使用die()exit()中止脚本,以确保以后不会更改输出。

PHP sometimes has a hard time with concatenation on multiple variables in a line (back to back). PHP 有时很难在一行中(背对背)连接多个变量。 This depends on the server setup.这取决于服务器设置。 You could try echoing using one of the following:您可以尝试使用以下方法之一进行回显:

echo '<li><a href="'.$Website.''.$Elements[2].'/'.$Row["povezava"].'" class="leftMenuArticlesInactive"></a><li>';

echo '<li><a href="'.$Website;
echo $Elements[2].'/'.$Row["povezava"].'" class="leftMenuArticlesInactive"></a><li>';

echo '<li><a href="',$Website,$Elements[2],'/',$Row["povezava"],'" class="leftMenuArticlesInactive"></a><li>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM