简体   繁体   中英

Page name as a php variable

I am working on a project that requires me to redirect the user to a php page. And the name of the page to be redirected to is stored as a php variable.This is what I tried. Suppose $var is the name of the php file. 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
if (condition) {
    header("Location: ".$page.".php"); 
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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