2

I am trying to integrate a website from a partner of my company, and I running into this line here:

header("Location: field/index.php", overwrite); 

I browsed the PHP manual of header() function, but can't find a parameter called overwrite. Can someone give me any clue?

2
  • 1
    The overwrite parameter is set to true by default, so you don't have to set it by yourself Commented Jul 22, 2013 at 23:07
  • No need to down vote this guy. Commented Jul 23, 2013 at 0:59

4 Answers 4

3

The second parameter (replace) means the same thing as overwrite. If you see the line:

header("Location: field/index.php", overwrite);

That is most likely not valid (it is technically valid if overwrite is a constant). It should be:

header("Location: field/index.php", true);
Sign up to request clarification or add additional context in comments.

3 Comments

overwrite (without a dollar sign) works (yet generates a "Notice: Use of undefined constant") because if undefined, it becomes the string 'overwrite'. This string casts to Boolean true. In any case, the default for the function is true, so it should not be necessary.
@PleaseStand really teaching, but I still do not understand why a string can cast to Boolean value.
@JiongfengLuo: This is documented. Many questionable decisions were made in the early days of PHP. Loose typing may be one of them.
1

It (overwrite) could be a constant, for example

define('overwrite', true);
header("Location: field/index.php", overwrite);

In this case overwrite represents Boolean true and which is being used to replace a previous header.

Though, constants should be declared using uppercase letters but it doesn't throw any errors if someone uses lowercase letters instead. Recommended way to define a constant is

defined('OVERWRITE') or define('OVERWRITE', TRUE);

It's worth mentioning that, any constant defined this way is available globally, throughout the script/application and basically developers define constants at the very beginning of script or an application and it could be defined in a different file (which is included/executed on start up) so, you may don't see it in the current script.

Comments

1

There are three parameters for PHP header():

  • String: ("Location: field/index.php" here)
  • http_response_code: Used to force the HTTP response code
  • Replace: A boolean option to overwrite a previous similar header

From the documentation:

The optional replace parameter indicates whether the header should replace a previous similar header [...] By default it will replace


The overwrite value in your code is invalid, because it is an unquoted string where a boolean value should be. Using header("Location: field/index.php", true); is correct, but because true is default, you only need header("Location: field/index.php").

1 Comment

I see, thanks a lot bro! This is someone else's code, and I have no idea he/she play this way, and cost me an hour googlling and reading php manual. Thanks to you guys I can understand it now.
0

https://www.php.net/manual/en/function.header.php

The php site is good for this kind of question. The second param just tells it whether or not to "overwrite" (replace) a value that has already been set.

Note that the value passed into the function doesn't necessarily have anything to do with the name of the parameter. They are simply used in order. Here, overwrite must be a variable set somewhere else although it's surprising it doesn't have the typical $ in front of it marking it as a variable in PHP. The variable overwrite is evaluated and its value is passed as the second parameter in the header function.

2 Comments

There is no such thing as a variable without a $ in PHP. Without the $ it is a constant if such a constant is defined. Otherwise it is a warning and the string literal "overwrite".
I did echo overwrite and generates "Notice: Use of undefined constant". According to PleaseStand overwrite becomes a string 'overwrite' which casts to Boolean true

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.