单引号内的PHP函数显示为文本 [英] PHP function inside single quotes display is as text

查看:125
本文介绍了单引号内的PHP函数显示为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示用户列表的函数,它是 displayUsers()



基本上而不是每个页面有1个不同的php文件,我使用了一个开关函数来加载内容。

  switch($ _ GET ['page']){

case'#userlist':$ page ='
< div class =innerbox>
displayUsers();
< / div> ;打破;

$ / code>

正如你所看到的,它应该显示 div 标记并加载函数,但不是加载函数,而是显示文本displayUsers()。我明白为什么它这样做,但我不知道如何改变它,所以实际上运行的功能,而不是显示它。任何帮助将不胜感激。



问候

解决方案

displayUsers()位于引号内。它被解释为一个字符串。你想

  switch($ _ GET ['page']){

case'#userlist' :$ page ='< div class =innerbox>'。 displayUsers()。 < / DIV>;打破;




$ b $ p
$ b

这将字符串中的displayUsers()的返回值连接起来,假设displayUsers()返回代码。
我认为你很混淆此功能,其中双引号字符串中的变量将被解析。这不适用于函数。



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



a href =http://php.net/manual/en/function.sprintf.php =nofollow> http://php.net/manual/en/function.sprintf.php






如果 displayUsers()实际输出代码本身,可以使用

  switch($ _ GET ['page']){

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

}

会发生什么情况是打印标签,然后调用函数(打印代码),然后按顺序打印结束标签。 / p>




如果您得到函数not defined(),那么它就是正是它听起来像。功能未在当前脚本中定义。



PHP的工作原理
每次请求页面时(有人转到foo php),您的服务器和PHP引擎从某个目录获取文件(foo.php),解析它,并且(通常)以HTML文档的形式将某些内容发回给用户。每个页面就像一个脚本,就像一个迷你应用程序,它被编译,执行一次,被遗忘

在您的主页上您定义了 displayUsers()

  // index .php 

函数displayUsers(){
//一些代码...
}

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

当有人访问您的主页时, displayUsers()被定义了,耶!但是,之后它被遗忘了 - PHP引擎在加载并完成后丢弃了脚本,并且当您到达<$时肯定不知道 displayUsers()是什么c $ c> foo.php






你可以做什么?
如果您只在一个页面上使用该功能,请在该页面上声明。请记住,每个文件都被认为是一个脚本。 PHP引擎不会跟踪用户会话中的函数。



如果您在多个脚本中使用该函数,请将函数包含在某个中央文件中和包含它在任何你需要的地方。我真的建议你阅读更多的PHP和 include ;我不打算解释PHP的基础知识;你需要做更多的功课。


I have a function that display the list of users which is displayUsers().

Basically instead of having 1 different php file for each page, I have used a switch function to load the content like. 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()". 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. 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. 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;

Read about sprintf here: http://php.net/manual/en/function.sprintf.php


If displayUsers() actually outputs the code itself, then you can use

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. 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. 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.

//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! 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.


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.

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; I'm not going to explain the basics of PHP; you need to do some more homework.

这篇关于单引号内的PHP函数显示为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆