[英]php function to get page title
I want to get my website page title through a function 我想通过一个函数获取我的网站页面标题
First I get the Page Name from a function... 首先,我从函数获取页面名称...
function pagetitle() {
global $th, $ta, $tp, $ts, $tc; // <---- Getting the variable value from outside function
global $bl, $qt;
$tit = ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME));
if($tit == "Index") {
echo $th;
}
elseif($tit == "About-us"){
echo $ta;
}
elseif($tit == "Projects"){
echo $tp;
}
elseif($tit == "Services"){
echo $ts;
}
elseif($tit == "Contact-us"){
echo $tc;
}
elseif($tit == "Blog"){
echo $bl;
}
elseif($tit == "Quotation"){
echo $qt;
}
} }
Then I tried to get the page title in the format of PageName | 然后,我尝试以PageName | Format的格式获取页面标题。 PageTitle or PageTitle | PageTitle或PageTitle | PageName... PageName ...
function getOrg_title(){
$tit = "";
$block = " | ";
global $sl;
$org = $sl['app_org'];
if($sl['title_before'] === 'yes'){
$tit = $org.$block.\pagetitle(); //<----- This should be like "Raj | This is my website"
} else {
$tit = \pagetitle().$block.$org; //<----- This should be like "This is my website | Raj"
}
echo $tit;
}
But not happening... 但是没有发生...
$tit = $org.$block.\pagetitle(); //<----- Output is "This is my websiteRaj |"
And 和
$tit = \pagetitle().$block.$org; //<----- Output is "This is my website | Raj"
What should I do ? 我该怎么办 ?
You expect pagetitle()
to return
the page's title, but instead in your code you use echo
. 您期望pagetitle()
return
页面的标题,但是您在代码中使用echo
。
echo
will print out the title to the client rather than return it to the caller. echo
会将标题显示给客户端,而不是返回给调用方。
So change you echo $...;
所以改变你echo $...;
to return $...;
return $...;
and do echo pagetitle();
并做echo pagetitle();
whereever you use it directly. 无论您直接使用它。
If this can not be done, you could extend your function to 如果无法做到这一点,则可以将功能扩展到
pagetitle($echo = true)
{
// ...
if(/* ... */) {
$ret = $th;
} elseif(/* ... */) {
$ret = ...;
} elseif(/* ... */) {
$ret = ...;
}
// And so on ...
if($echo)
echo $ret;
return $ret;
}
Now in your wrapper function call pagetitle(false)
to prevent the echo
and still get the title from it. 现在,在包装函数中调用pagetitle(false)
来防止echo
并仍然从中获取标题。
About the backslash: \\pagetitle()
关于反斜杠: \\pagetitle()
The backslash is the namespace separator . 反斜杠是名称空间分隔符 。 Since you are not using namespacing here, all functions will be on the root namespace . 由于此处未使用命名空间,因此所有功能都将位于根名称空间上 。
In this case - if you are using PHP 5.3 or above - you can use it or leave it out. 在这种情况下-如果您使用的是PHP 5.3或更高版本-您可以使用它,也可以不使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.