PHP & MySQL | Tutorials

David Carr

PHP's extract function and HTML arrays

I'm a huge fan of using the extract function in php its very handy it lets you extract an array of keys to variables.

For example when submitting a form normally I add each post key to a matching variable:

$postTitle = $_POST['postTitle'];
$postcat = $_POST['postcat'];
$postCont = $_POST['postCont'];

That's fine for small forms but when working on larger forms it gets very tedious quickly that's where the extract function comes in instead of having to add each post key to a variable I pass the $_POST to extract which results in the same thing:


//I can now call any key by its variable name
echo $postTitle;

This is really convenient, recently I've discovered one drawback when working with HTML data in a form and using extract and closing slashes are removed from the tags take this:

$postCont =" <h1>My Title</h1><p>some content etc</p>"; 

When passed to extract the result ends up missing the h1 tags

$postCont =" <h1>My Title<h1><p>some content etc<p>"; 

That's not good at all! I've experimented a lot there doesn't seem to be a workaround so when working with HTML in your forms bind the POST item to a variable as you would normally do, it will avoid the slashes being removed.

Domains are often purchased from multiple providers, keeping track of where a domain is and its DNS settings can be tricky. Domain Mapper solves this by listing all your domains in one place. View your DNS settings and receive reminders to renew your domains. Try it today.

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2021 DC Blog. All code MIT license. All rights reserved.