Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unset or change a defined constant PHP

Tags:

php

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?

like image 515
Alex Ruhl Avatar asked May 11 '13 12:05

Alex Ruhl


People also ask

Can you redefine a constant in PHP?

No, you cannot redefine a constant (except with runkit_constant_redefine), that's why is called CONSTANT. Short answer: No.

What is unset () in PHP?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

How do you change the value of a constant?

The point of a constant is, that they cannot be changed. You should use a true variable instead. Save this answer.

Is unset variable used in PHP?

The unset() function is an inbuilt function in PHP which is used to unset a specified variable.


1 Answers

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.

like image 125
Juan Pablo Rinaldi Avatar answered Oct 19 '22 17:10

Juan Pablo Rinaldi