10

assumed i define a variable like this:

<?php
define("page", "actual-page");
?>

now i have to change this content from actual-page to second-page how can i realize that?

i try the following methods:

<?php
// method 1
    page = "second-page";

// method 2
    define("page", "second-page");

// method 3
    unset(page);
    define("page", "second-page");
?>

now my ideas goes out... what can i do else?

Alex Ruhl
  • 451
  • 2
  • 6
  • 16
  • As Juampi answered, by using `define()` you're setting a constant, 'page'. Instead, use a variable: `$page = 'actual-page';`. – Jamie Schembri May 11 '13 at 12:53
  • The definition of "constant": _[noun] A situation that does not change._ You should use a variable instead. – BadHorsie Dec 19 '19 at 14:34

4 Answers4

21

When you use PHP's define() function, you're not defining a variable, you're defining a constant. Constants can't have their value modified once the script starts executing.

Juan Pablo Rinaldi
  • 3,394
  • 1
  • 20
  • 20
6

You can, with the runkit PECL extension:

runkit_constant_remove('page');

http://php.net/runkit_constant_remove
http://github.com/zenovich/runkit

sudo pecl install https://github.com/downloads/zenovich/runkit/runkit-1.0.3.tgz

Update: This module seems to cause trouble with various other things, like the session system for example.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
4

Maybe a very late answer, but the question was "now my ideas goes out... what can i do else?"

Here is what you can do "else" - use $GLOBALS:

<?php
// method 1
    $GLOBALS['page'] = 'first-page';

// method 2
    $GLOBALS['page'] = "second-page";

// method 3
    $GLOBALS['page'] = 'third-page';
?>

I hope it helps. I use it when I do imports and I want specific events not to be fired if the import flag is on for example :)

Mihai Pop
  • 65
  • 6
-15

define has a third argument (boolean) which override the first defined constant to the second defined constant. Setting the first defined constant to true.

<?php
define("page", "actual-page", true);
// Returns page = actual-page

define("page", "second-page");
// Returns page = second-page
?>
Shudmeyer
  • 354
  • 2
  • 14