How to create a php variable using WordPress the_title() function

Indeed, the the_title function echoes the title, unless you set the third parameter to false (default true). So you start with this;

$title = the_title ('','',false);

Now, $title may contain all kinds of characters that cannot be used in PHP variable names, so we need some sanitization:

$title = preg_replace('/[^A-Za-z0-9\_]/', '', $title); // Removes special chars except underscore.

Then you use PHP variable variables to turn the content of $title into a variable name:

$$title="something";

So, if the_title gives the value “hello world”, the $title variable after sanitizing has the value “helloworld” and $helloworld has the value “something”.