hi,
i have a configuration file with this content for example:

test = "100";
teste2 = "300";

now need change the test from 100 to 300,
i just want use something like this: function ( "test" , 300 );
how can i do that?
thanks for help

Dani AI

Generated

A quick summary and a safe way to do this in PHP.

showed a simple key/value file and asked for a small function to change one value. and pointed out you can edit the file directly; that works for one-off edits but can break under concurrent writes, wrong permissions, or when you want to automate updates. asked for more detail — below is a robust, repeatable approach.

If the file follows an INI-like key/value format, read it into an array, change the key, then write a new file atomically while holding a lock. This avoids corrupting the config during concurrent writes. Use PHP's parse_ini_file to read and flock when writing.

function updateConfigValue($filePath, $key, $value) {
    if (!is_readable($filePath)) return false;
    $data = parse_ini_file($filePath, false, INI_SCANNER_RAW);
    $data[$key] = (string)$value;

    $tmp = tempnam(sys_get_temp_dir(), 'cfg');
    $fh = fopen($tmp, 'w');
    if ($fh === false) return false;
    if (!flock($fh, LOCK_EX)) { fclose($fh); unlink($tmp); return false; }

    foreach ($data as $k => $v) {
        fwrite($fh, $k . ' = "' . str_replace('"', '\\"', $v) . '"' . PHP_EOL);
    }

    fflush($fh);
    flock($fh, LOCK_UN);
    fclose($fh);

    if (!rename($tmp, $filePath)) { unlink($tmp); return false; }
    return true;
}

Notes and cautions: this method rewrites the file (comments and original ordering are lost). If preserving comments/formatting is required, do a careful regex replace of the single key line (watch escaping and edge cases). Always backup the config and verify file permissions before automating replacements.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589
$test = 100;

then

$test = 300;

Just like that.

I've no idea, how you're going to be complex to the easy one. If you've configuration like such. Then, you can simply change their value directly in that file as @ardav mentioned.

Can you please explain it more?, i'm kinda confuse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.