SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using the double variable in php

    I am a newbie with programming and my mind is grasping the basics of working with a php array. I found this in one of David Powers book. The code works but I can't understand how. It's a foreach loop:

    foreach ($_POST as $key => $value){
    $temp = is_array($value) ? $value : trim($value);
    echo ${$key} = $temp;
    }

    This is a quick way of storing variables for all fields in an html form. Manually the equivalent is this:
    $email = $_POST['email']

    But using the foreach method the name value pairs automatically collect themselves from html form fields. But the temp refers to inputs made by users, so how does assigning the $key variable to $temp grab the form fields in the superglobal $_POST array?

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you don't undestand in this example, the traversing of $_POST array or the assigment?

    I assume the assigment. For $key = 'email' ${$key} is equivalent of $email, php dynamically replaces $key with the value it contains.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by krzyk View Post
    I'm not sure what you don't undestand in this example, the traversing of $_POST array or the assigment?

    I assume the assigment. For $key = 'email' ${$key} is equivalent of $email, php dynamically replaces $key with the value it contains.
    Thanks krzyk for the quick reply.

    Yes. You are on the right track: what I can't understand is that when "php dynamically replaces $key with the value it contains", does the value not contain the actual user input? How does $_POST['email'] turn into $email if the $$key assignment assign it to the value(in this case $temp)? Doesn't value here mean the actual user input?

    - or does the superglobal array $_POST gram the name property of the field?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $_POST 
    = array(
        
    'name'        =>    'Anthony'
    );
    foreach(
    $_POST as $sKey => $mValue)
    {
        
    /*
        This is the same as:-
        $name = Anthony
        */
        
    $$sKey $mValue;
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $temp containst user input (trimmed), it is taken from $_POST

    when you do "foreach ($_POST as $key => $value)"

    $key contains 'email'
    and $value contains 'someone@example.com'

    then $temp is assigned $value either right away if it is an array, or trimming it if it is a string, so
    $temp will contain also 'someone@example.com'

    then $$key = $temp translates to: $email = 'someone@example.com';

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by krzyk View Post
    $temp containst user input (trimmed), it is taken from $_POST

    when you do "foreach ($_POST as $key => $value)"

    $key contains 'email'
    and $value contains 'someone@example.com'

    then $temp is assigned $value either right away if it is an array, or trimming it if it is a string, so
    $temp will contain also 'someone@example.com'

    then $$key = $temp translates to: $email = 'someone@example.com';
    I get it krzyk: I keep reading assignment as left to right.
    When you said that $temp is being assigned $value, then I got it...

    Could you recommend a good php book that does not presume too much from a beginner like me? David Powers is good but sometimes he takes these sudden leaps in logic and doesn't explain what happened?

    How long in your opinion does it take to master a language like PHP?

    Thanks a lot for taking the time out to answer my questions.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •