Results 1 to 9 of 9

Thread: ${finalprice.$i+1}

  1. #1
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default ${finalprice.$i+1}

    this is correct:
    Code:
    <?= ${finalprice.$i+1} ?>
    to get
    $finalprice1
    $finalprice2
    $finalprice3
    ...
    ...
    in a loop ?
    what this syntax is being called ?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Not sure if I understand you correctly, but are you asking about a for or while loop? If so, what exactly is the question?
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There are two things to point out here. The first is the <?= ?> syntax, which is now deprecated and should no longer be used. The other is the use of 'variable variables' (in answer to your question), which is very bad practice and always indicative of bad design. Avoid those, too.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Instead of

    $finalprice1
    $finalprice2
    $finalprice3

    I do either of the following:

    $finalprice[]
    $finalprice[]
    $finalprice[]

    or

    $finalprice[1]
    $finalprice[2]
    $finalprice[3]

    Then use:

    for($i=0;$i<count($finalprice);$i++);
    {
    echo "finalprice {$i}: {$finalprice[$i]}
    }

    Returns things like this:

    finalprice 1: value
    finalprice 2: value
    finalprice 3: value

    With my site's user system I recently found this out:

    $user = (object) mysql_fetch_array($sql);

    I can now pull data using $user->id or $user->username, etc. makes it look cool and I find that easier than $user['username']

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Handy tip. Yes, arrays are a much better alternative.

    Note here: one of the examples a) has syntax errors, and b) would be better expressed with a foreach loop.
    Code:
    // With proper syntax
    for ($i = 0; $i < count($finalprice); $i++)
      echo "finalprice {$i}: {$finalprice[$i]}";
    
    // With a foreach
    foreach ($finalprice as $k => $v)
      echo "finalprice $k: $v";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Quote Originally Posted by Twey View Post
    Handy tip. Yes, arrays are a much better alternative.

    Note here: one of the examples a) has syntax errors, and b) would be better expressed with a foreach loop.
    Code:
    // With proper syntax
    for ($i = 0; $i < count($finalprice); $i++)
      echo "finalprice {$i}: {$finalprice[$i]}";
    
    // With a foreach
    foreach ($finalprice as $k => $v)
      echo "finalprice $k: $v";
    I dpon't remember where but I read somewhere that you should akways use brackets in loops and that the way you have would be bad syntax, is that true?

    And also what if you need to do multiple lines in the loop? then you would need the brackets, right?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No. All flow-control statements of this kind in C-like languages are of a general format:
    Code:
    keyword (flow control specifier(s)) statement
    If you wish to execute multiple statements at once, it's possible to combine them into one statement using a block:
    Code:
    {
      theseStatements();
      willAllBe($treated) * $syntactically;
      AsOne::statement();
    }
    Certain schools of programming (not ones for which I hold a great amount of respect) hold that it is preferable to always use a block, since it makes it easier to a) see what each block is delimiting, and b) add extra lines if it becomes necessary later. However, I believe that a) can be accomplished with indentation (very successfully; indeed, this is the only way of doing it in Python), and while b) is a small plus, it's never difficult to add a block in if necessary, and one spares one's self an astonishing amount of junk by not doing so in the first place, in a big project. In addition, it helps one to remember where to create separate, more general functions -- if you have to use a block, perhaps that loop body should be generalised and moved into another function. This helps to create clean, neat, reusable code.

    Block scope is also a factor to consider in some languages, but not in PHP, which does not have block scope.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    arrays in php need to be declared first ?
    I want array(16)
    setup as
    off[1]=
    ....
    off[2]=
    ....
    off[3]=
    ....other code(all code in a block , same page)
    ....
    off[16]=

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Er, yes...
    Code:
    $off = array('item1',
                 'item2',
                 'item3');
    
    // ...
    
    $off[3] = 'item4';
    Don't forget that arrays are zero-indexed. I think that perhaps you would benefit from a PHP tutorial.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •