简体   繁体   English

页面名称作为php变量

[英]Page name as a php variable

I am working on a project that requires me to redirect the user to a php page. 我正在做一个需要我将用户重定向到php页面的项目。 And the name of the page to be redirected to is stored as a php variable.This is what I tried. 并且要重定向到的页面名称存储为php变量。这就是我尝试过的方法。 Suppose $var is the name of the php file. 假设$var是php文件的名称。 I want to do something like this, 我想做这样的事情

if(condition)
{
header("Location: '$var'.php"); 
}

How do I do this? 我该怎么做呢?

You simply need to follow string concatenation here. 您只需要在此处遵循字符串连接。 Try this: 尝试这个:

if (condition) {
    header("Location: ".$var.".php"); 
}

Refer to String Operators 请参阅字符串运算符

You have to concatenate the string which you have stored the page name into the header location tag so that then only it will read the name that you have stored. 您必须将已存储页面名称的字符串连接到标题位置标签中,以便只有它才能读取已存储的名称。

More Clear Explanation: You have used double quoted string over to the header location and hence you need to close of with the double quoted string and then reopen again with the double quoted string alone. 更清楚的说明:您已经在标题位置使用了双引号字符串,因此您需要使用双引号字符串将其关闭,然后仅使用双引号字符串再次重新打开。 This might be the correct procedure for appending or concatenating it into the variables. 这可能是将其附加或连接到变量中的正确过程。

Example: 例:

<?php
$page='index';
$page_one ='about';
?>

Hence you need to concatenate the variable as follows into the PHP tags. 因此,您需要按如下所示将变量连接到PHP标记中。

<?php
if (condition) {
    header("Location: ".$page.".php"); 
}
?>

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

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