SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot devil_online's Avatar
    Join Date
    Jun 2004
    Location
    Lisboa
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using variables inside a while

    Hi
    I'm trying to use this code:
    while ($resource == 1){
    $firstnames = array("Jonh", "Paul", "Bill");
    $lastnames = array("Simon", "Luck","Guy");
    $name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)];
    }

    my problem is that I must declare the variable $name outside the while because it's need for the $resourse and needed also inside the while.

    how can I do this?
    thanks
    Last edited by devil_online; Feb 16, 2006 at 19:49.

  2. #2
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can always define $name outside of the loop, like so:

    PHP Code:
    $name "somedefaultvalue"
    However, I suggest you rethink the path you're taking. What are you trying to accomplish? There might be a simpler way of doing what you need.

    By the way - that loop will never execute because you're doing an assignment to $resource, rather than an equality comparision ==, not =.

    $resource = 0 will return the value 0, and while(0) will never execute.
    This space for rent.

  3. #3
    SitePoint Zealot devil_online's Avatar
    Join Date
    Jun 2004
    Location
    Lisboa
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what i'm trying to do is:
    insert a name in the table that don't already exist.
    If already exists it loops again to creat a name (and insert it)



    You can always define $name outside of the loop, like so:

    $name = "somedefaultvalue";
    However I need that variable inside the loop also

    thanks
    Last edited by devil_online; Feb 16, 2006 at 20:39.


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
  •