2

A.php

define("_a_","hey!");

B.php

define("_a_","wow!");

index.php

function getFile($type) {
  include_once($type . ".php");
  echo _a_;
}

Calling getFile

getFile(A); //--> Output "hey!"
getFile(B); //--> Output "hey!" HERE IS THE PROBLEM, SHOULD ECHO "wow!"

Then I exchanged the order of calling functions

getFile(B); //--> Output "wow!"
getFile(A); //--> Output "wow!" HERE IS THE PROBLEM, SHOULD ECHO "hey!"

Thanks for all helps

Krst
  • 31
  • 7
  • Check out the answer, and if it was helpfull check it as Accepted - StackOF Community will thank you – Toumash Jul 02 '15 at 10:57

1 Answers1

7

From the PHP official documentation:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )  

Defines a named CONSTANT at runtime.

So if it defines not changable value, that means you cannot change the value at runtime.

EDIT: You can use variables to get data from included files. When you are including scripts you are basically creating new script with copy & pasted content from included file and executing that new script. Passing a variable from other script - PHP

Community
  • 1
  • 1
Toumash
  • 1,077
  • 11
  • 27
  • That's true I'm okay with that but it shouldn't be problem because i'm include the file dynamicly in function. I mean when passing "A", getFile function include A.php and if I pass "B" then getFile should include only B.php – Krst Jun 30 '15 at 19:51
  • 2
    Just to expand on [what is a constant](https://en.wikipedia.org/wiki/Constant_%28computer_programming%29), `a constant is an identifier with an associated value which cannot be altered by the program during normal execution`. You can read more about [php's implementation here](http://php.net/manual/en/language.constants.php). – Jonathan Kuhn Jun 30 '15 at 19:52
  • 2
    @user3645649 On the php constant page: `Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope.` Defining a constant in a function is still defined in global scope. – Jonathan Kuhn Jun 30 '15 at 19:54
  • thanks @JonathanKuhn. I need to do this, how can i do that ? – Krst Jun 30 '15 at 19:56
  • what about creating variables? – Toumash Jun 30 '15 at 19:58
  • unfortunately must be constant and i need to do something inside getFile function. Is it possible to remove included file or constants when the function is die ? – Krst Jun 30 '15 at 20:03
  • You can still use them outside the file with var declarations. When you are including- you are basically creating new script with copy & pasted content from inculuded file. http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not – Toumash Jun 30 '15 at 20:05
  • @Toumash thank you, I handled with creating variables – Krst Jun 30 '15 at 20:15