www.webdeveloper.com
Results 1 to 7 of 7

Thread: Foreach as Variable

  1. #1
    Join Date
    Feb 2005
    Posts
    97

    Foreach as Variable

    Can a foreach loop be set as a variable? My script is populating the body of an email and I'd like to capture all the output from the loop into one variable so that I can later reference it in the HTML body.

    Each time I attempt to set as a variable I get an error. ie.
    Code:
    $MyVar=foreach([$array] as $key=>$value).....
    Any clues?

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    No, a function can't be assigned to a variable in PHP. Want you want can be done in a number of ways, for example adding stuff to a variable on each iteration:

    PHP Code:
    $output '';
    foreach (
    $foo as $bar) {
        
    $output .= $bar;

    However in a case as simple as the above you can just use implode:

    PHP Code:
    $output implode($foo); 

  3. #3
    Join Date
    Feb 2005
    Posts
    97
    Thanks Mindzai!! Looks like
    $output = '';
    was the differerence.

    Stupid ?:
    .=
    I am still learning all the . syntax and I am having difficulty calling 2 variables in my string.



    Code:
    $Files = '';
    foreach ($_SESSION[file_array] as $value) {
        $Files .= "<a href='http://www.mydomain/uploads/files/'$AcctNo'/'$value>Uploaded File</a><br />";
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    There are a couple of issues with your code.

    First the key used with $_SESSION should be in quotes:

    PHP Code:
    $_SESSION['file_array'
    Also the string you are building will have errors in it. You need to keep an eye on the quotes. I would do it like this:

    PHP Code:
    $files '';
    foreach (
    $_SESSION['file_array'] as $value) {
        
    $files .= "<a href=\"http://www.mydomain/uploads/files/$AcctNo/$value\">Uploaded File</a><br />";

    You haven't said what the actual problem you are having is though so I don't know if that fixes your issue. If not please provide some detail on what is going wrong.

  5. #5
    Join Date
    Feb 2005
    Posts
    97
    Problem solved!!!! Thanks so much!! It was a syntax error. But I'm learning so much.

    It looks as if you have to set the variable before you can set it in the loop?
    $files = '';
    I've never seen this
    .=
    Please explain when I should use it.

    Thanks for all!

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    It's good practice to initialise variables in this sort of situation yes.

    .= is a shortcut for appending a string to itself. For example:

    PHP Code:
    $something $something "foo";
    // is the same as
    $something .= "foo"

  7. #7
    Join Date
    Feb 2005
    Posts
    97
    Great info. Thanks!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles