megacolorboy

Back

Published on March 12th, 2021

PHP

Convert string to variable in PHP

I read about variable variables in PHP's official documentation.Here's a sample:Not sure if this is a good practice but it sure gets the job done!

1 minute read

I read about variable variables in PHP's official documentation.

Here's a sample:


<php
    $a = "hello";
    // Remove special characters and tags to prevent it from crashing.
    $foo = preg_replace('/[^a-zA-Z0-9\s]/', '', $$a);
    echo $foo;
?>

Not sure if this is a good practice but it sure gets the job done!