Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

PHP's extract function and HTML arrays

David Carr

PHP & MySQL Tutorials

    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.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

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