0

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?

1

2 Answers 2

2

You simply need to follow string concatenation here. Try this:

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

Refer to String Operators

Sign up to request clarification or add additional context in comments.

Comments

1

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"); 
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.