change or modify constant variables values

  constants, php

I am building an installer script in my application. I am declaring some constants in one file for example

define(DB, '');
define(USER, '');
define(PASS, '');
define(HOST, ''); 

Now I want to access this DB variable first and update the value of DB and so on.

I have tried so far is this

define(DB, '%dbname');
define(USER, '%dbuser');
define(PASS, '%dbpass');
define(HOST, '%dbhost'); 

$file_path = "constant.php";
$dbname = $_POST['dbname'];
$value = "%dbname";
$replace_with = $value;
$content = file_get_contents($file_path);
$content_chunks = explode($string_to_replace, $content);
$content = implode($replace_with, $content_chunks);
file_put_contents($file_path, $content);

This script works fine. But the only issue is that once its update the value it can’t update the value again. The value cannot be updated twice because the script was checking this value %dbname. Now since it has been changed to some value. I can’t be able to update the value again. I want to do something like detect the variable DB then update the value in front of it.

Source: Ask PHP

LEAVE A COMMENT