简体   繁体   English

单引号内的PHP函数显示为文本

[英]PHP function inside single quotes display is as text

I have a function that display the list of users which is displayUsers() . 我有一个显示用户列表的函数displayUsers()

Basically instead of having 1 different php file for each page, I have used a switch function to load the content like. 基本上,而不是每个页面都有1个不同的php文件,我使用了switch函数来加载类似的内容。 This is an example of 1 of the pages. 这是页面之一的示例。

switch($_GET['page'])  {

   case '#userlist' : $page = '
       <div class="innerbox">
            displayUsers();
       </div> '; break;
}

As you can see it is supposed to show the div tag and load the function, however instead of loading the function, is just displaying the text "displayUsers()". 如您所见,它应该显示div标签并加载该函数,但是,而不是加载该函数,只是显示了文本“ displayUsers()”。 I understand why it is doing this however I'm not sure how to change it so that is actually runs the function rather that displaying it. 我知道为什么这样做,但是我不确定如何更改它,因此实际上是在运行该功能而不是显示它。 Any help would be much appreciated. 任何帮助将非常感激。

Regards 问候

displayUsers() is inside quotes. displayUsers()在引号内。 It's interpreted as a string. 它被解释为字符串。 You want 你要

switch($_GET['page'])  {

   case '#userlist': $page = '<div class="innerbox">' . displayUsers() . '</div>'; break;

}

This concatenates the return value of displayUsers() inside the string, assuming displayUsers() returns the code. 假设displayUsers()返回代码,这会将字符串中的displayUsers()的返回值串联起来。 I think you were confusing this feature where variables inside double quoted strings are parsed. 我想你混淆了此功能 ,其中双引号内的字符串变量的分析。 This does not work for functions. 这不适用于功能。

You could also use case '#userlist': $page = sprintf('<div class="innerbox">%s</div>', displayUsers(); break; 您还可以使用case '#userlist': $page = sprintf('<div class="innerbox">%s</div>', displayUsers(); break;

Read about sprintf here: http://php.net/manual/en/function.sprintf.php 在此处阅读有关sprintf的信息: http : //php.net/manual/en/function.sprintf.php


If displayUsers() actually outputs the code itself, then you can use 如果displayUsers()实际输出代码本身,则可以使用

switch($_GET['page'])  {

   case '#userlist': echo '<div class="innerbox">'; displayUsers(); echo '</div>'; break;

}

What happens is the opening tag is printed, then the function is called (printing the code), then the closing tag is printed, in that order. 发生的情况是先打印开始标签,然后调用该函数(打印代码),然后依次打印结束标签。


If you're getting function not defined() then it's exactly what it sounds like. 如果您要获取的function not defined()那么听起来就是这样。 The function has not been defined in the current script. 该函数尚未在当前脚本中定义。

How PHP works Each time a page is requested (someone goes to "foo.php"), your server and PHP engine gets the file (foo.php) from some directory, parses it, and (usually) sends something back to the user in the form of an HTML document. PHP的工作原理每次请求页面时(有人进入“ foo.php”),您的服务器和PHP引擎都会从某个目录中获取文件(foo.php),进行解析,然后(通常)将某些内容发送回用户以HTML文档的形式。 Each page is like a "script", kind of like a mini application that is compiled, executed once, and forgotten . 每个页面就像一个“脚本”,有点像一个微型应用程序,该应用程序被编译,执行一次并被遗忘

So let's say on your homepage you have displayUsers() defined. 因此,假设您在首页上定义了displayUsers()

//index.php

function displayUsers() {
    //some code...
}

echo "Welcome to my website!", "<br />", 'Click <a href="foo.php">Here</a>.';

When someone goes to your homepage, displayUsers() is defined, yay! 当有人访问您的主页时,会定义displayUsers() However, it's forgotten right afterwards - the PHP engine has discarded the script after it's loaded and finished, and certainly does not know what displayUsers() is by the time you reach foo.php . 但是,此后便被遗忘了displayUsers()引擎在加载和完成脚本后就将其丢弃,并且在到达foo.php时肯定不知道displayUsers()是什么。


So what can you do? 所以,你可以做什么? If you're only using the function on one page, declare it on that page. 如果仅在一页上使用该函数,则在该页上声明它。 Remember, each file is thought of as a script. 请记住,每个文件都被视为脚本。 The PHP engine doesn't keep track of functions over the user's session. PHP引擎无法跟踪用户会话中的功能。

If you're using the function in multiple scripts, include your functions in some "central" file and include it wherever you need it. 如果要在多个脚本中使用该函数,请将函数包含在某个“中央”文件中,并在需要的地方包含它。 I really suggest you read more into PHP and include ; 我真的建议您阅读更多有关PHP的内容并include I'm not going to explain the basics of PHP; 我将不解释PHP的基础知识。 you need to do some more homework. 您需要做更多的作业。

You need to use the . 您需要使用。 (dot) to append strings when using singleqotes: (点)在使用单引号时附加字符串:

$page = '<div class="innerbox">'.displayUsers().'</div>';

try this 尝试这个

case '#userlist' : $page = '
           <div class="innerbox">'
                .displayUsers().
           '</div> '; break;

Use the concatenation . 使用串联. operator this way: 这种方式的运算符:

switch($_GET['page'])  {

   case '#userlist':
       $page = '
       <div class="innerbox">'
       . displayUsers() .
       '</div> ';
       break;
}

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

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