Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15

    $var1 = $var2 = 3; valid syntax?


    does that work as valid syntax? seems to work fine, but wondering if its valid syntax and good to use?
  2. #2
  3. Web Programmer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    362
    Rep Power
    17
    It is valid syntax and fine to use, although it can be considered somewhat of a bad practice because of readability. I use it occassionally but generally you do not need the same value in two variables, anyways.
    Matt Light
    Web Programmer
  4. #3
  5. This is only a test
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Off the air
    Posts
    2,884
    Rep Power
    157
    Yep, it works fine.

    Personally, I don't like it, though, because it's easy for someone (especially someone who's not familiar with the script in question) to 'lose' that second variable.
    "Not to offend our Swedish listeners ... if we have any, that isó"
    "óBut your team's rubbish."
    (Sun webcast, Sweden vs. Paraguay)

    Who needs corporate radio?
    WeFunkRadio.com | Global Pop Conspiracy | Radio Paradise | SomaFM | The Classic Soul Network | Boot Liquor | WFMU Freeform Radio
  6. #4
  7. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    ya, ive only seen a couple instances so far that id use it (mainly like setting up variables, eg:

    PHP Code:
    $var1 0;
    $var2 0;
    .
    .
    .
    $varn 0
    but outside of that, dont see where else it could be used.

    Comments on this post

    • J_Tree agrees : It is good for initalizing multiple variables to 0, null, false, etc
    • kicken agrees
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,276
    Rep Power
    4193
    Originally Posted by Matt Light
    It is valid syntax and fine to use, although it can be considered somewhat of a bad practice because of readability. I use it occassionally but generally you do not need the same value in two variables, anyways.
    I only use it when initlizing variables at the beginning of a block of code. For example, on ocasion I make loops that get data from a DB, and then the loop sorts if out into 2+ arrays.

    PHP Code:
    $files=$dirs=$links=array();
    while (
    $f readdir($dir)){
    ...

    Also, just setup intergers or booleans or whatever.

    PHP Code:
    $x=$y=$width=$height=0
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3318
    It's generally considered bad practice, but it has its uses which actually contribute to clarity. If one needs a variable (say, an index) to mark a position, but also needs to "move" the value and later reset it, the usage is clear. Rescanning content for various things is an example. $beginningPosition = $currentPosition = $tokenStart, then, later: $currentPosition = $beginningPosition. PHP has so many powerful things that the need is probably not often encountered.

    A lot of things have historical reasons for being that persist beyond their useful lifetime. The boys of C and Unix were notoriously laconic, but for good reason. Terse statements might not have resulted in reduced code footprint (witness the ternary operator), but they resulted in smaller source files, which was once a real consideration in terms of storage and access speeds. Similarly, the lean syntax of the commands (ls, cp, cat) meant reduced requirements for shell/batch files. Many of today's languages and OS's carry the genes of those progenitors for better or worse.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
  12. #7
  13. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    another question ...

    is this valid:

    PHP Code:
    if( $var1 == $var2 == $var3 )
    {
    //all are equal to each other

    ??
  14. #8
  15. This is only a test
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Off the air
    Posts
    2,884
    Rep Power
    157
    Short answer: Nope.

    Longer answer: If you can type up the example to post it here, I would suggest just running it as a test script so you can see the results for yourself. Hands on experience is the best teacher, after all.
    "Not to offend our Swedish listeners ... if we have any, that isó"
    "óBut your team's rubbish."
    (Sun webcast, Sweden vs. Paraguay)

    Who needs corporate radio?
    WeFunkRadio.com | Global Pop Conspiracy | Radio Paradise | SomaFM | The Classic Soul Network | Boot Liquor | WFMU Freeform Radio
  16. #9
  17. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    i havent coded it yet ... planning a script out right now, and thinking how to do the script ....

    i have a script that allows the user to change the order of something (in my case, pictures).

    the user uploads a bunch of pictures. but if the user wants to have the pictures displayed in an order, i let the user do that. (when the pictures are added to the database via upload, the order is automatically set, 1,2, 3, 4, etc)

    so it lists out the pictures, with a drop down menu with its order ....

    so then the person can set the order ....

    the script only prints out the order based upon number of pictures.

    so if there are 5 pictures for example, then the script prints out for each picture a drop down menu, with options 1-5 only, with the pictures selected order number (1 or 2 or 3 etc)

    so if all the picture order numbers in the drop down menu options are different, then the order has either been NOT changed, or they been changed correctly (no pictures have the same order).

    if any 2 options have the same number, then 2 of the pictures have the same order value (2 pictures have the same order value, say 1).

    thought maybe this can be easily done via if( $pic1order == pic2order == $picNorder ) ....

    now, this problem has become exponentially bigger (and longer with LOTS of pictures).

    guess when i do the actual coding/scripting (im planning ahead right now), ill post a seperate thread on how this might be done, unless you can think of one off the top of your head?
  18. #10
  19. This is only a test
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Off the air
    Posts
    2,884
    Rep Power
    157
    Think about putting all the order values into an array and using some of the array functions to make comparisons/sort.
    "Not to offend our Swedish listeners ... if we have any, that isó"
    "óBut your team's rubbish."
    (Sun webcast, Sweden vs. Paraguay)

    Who needs corporate radio?
    WeFunkRadio.com | Global Pop Conspiracy | Radio Paradise | SomaFM | The Classic Soul Network | Boot Liquor | WFMU Freeform Radio
  20. #11
  21. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    Originally Posted by Ucht
    Think about putting all the order values into an array and using some of the array functions to make comparisons/sort.
    ya, then i can just sort the array, then check if the values in the array next to each other are equal .....

    so array[0] == array[1]

    then array[1] == array[2]

    .
    .
    .

    thanks
  22. #12
  23. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,276
    Rep Power
    4193
    Originally Posted by jaeSun
    if any 2 options have the same number, then 2 of the pictures have the same order value (2 pictures have the same order value, say 1).
    if you put all the order values into an array, a quick way to see if there are any duplicates would be to use array_unique and count().

    Code:
    if (count($sortOrders) < count(array_unique($sortOrders)){
       //Something was removed by array_unique, so there must be a duplicate.
      //Figure out where the duplicate is.
    }
    The array_count_values function and the array_filter might be helpful in determining where the duplicate value is.

    Code:
    function greaterThanOne($val){
       return $val > 1;
    }
    
    $valueCount = array_count_values($sortOrders);
    $valueCount = array_filter($valueCount, "greaterThanOne");
    print_r($valueCount); //Should contain the duplicate values only.
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  24. #13
  25. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,971
    Rep Power
    4576
    Originally Posted by jaeSun
    is this valid:

    if( $var1 == $var2 == $var3 )
    If you were put put in some parenthesis, it would be valid, but not in the way you're thinking.

    if($var1 == ($var2 == $var3))

    Now you can see how PHP is evaluating this. If $var2 is equal to $var3, then that parenthesis becomes a TRUE value. Then it checks to see if $var1 is equal to TRUE.

    So, the above (my version) will return TRUE if all three variables are TRUE or if $var2 and $var3 weren't equal and $var1 was FALSE... but there are simpler ways to write it, either way.

    So, there are ways you can join multiple things into conditionals, but you've got to be aware of how PHP interprets them.

    Another thing to be aware of is that PHP will "short circuit". That is, if the first clause of an OR conditional is TRUE, it'll stop there and not even test the second condition. Makes sense since it doesn't matter, but if you were expecting both to be executed, it could confuse you.

    Say for example you're looking to execute a loop if $x is less than 100 or if a row is returned from a database result set:

    if($row = mysql_fetch_assoc($result) || $x < 100)

    if($x < 100 || $row = mysql_fetch_assoc($result))

    If you're looking to use $row within the loop, then the first one is valid while $x is less than 100. In the second condition, PHP will not get executed if $x is less than 100, so $row will not be retrieved. Can be confusing when you first come across it.

    Now thing about an AND conditional. If the first evaluates to FALSE, there's no need to test the second is there? Same deal...

    ---John Holmes...
    -- Cigars, whiskey and wild, wild women. --
  26. #14
  27. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3318
    An extra aside here. If your second conditional has side-effects don't expect to benefit from them under short-circuit conditions. Actually, it's the same thing as Sepodati's example of fetching the row, but one sometimes sees autoincrements or autodecrements that are, but shouldn't be, relied upon.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
  28. #15
  29. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    Originally Posted by kicken
    if you put all the order values into an array, a quick way to see if there are any duplicates would be to use array_unique and count().

    Code:
    if (count($sortOrders) < count(array_unique($sortOrders)){
       //Something was removed by array_unique, so there must be a duplicate.
      //Figure out where the duplicate is.
    }
    The array_count_values function and the array_filter might be helpful in determining where the duplicate value is.

    Code:
    function greaterThanOne($val){
       return $val > 1;
    }
    
    $valueCount = array_count_values($sortOrders);
    $valueCount = array_filter($valueCount, "greaterThanOne");
    print_r($valueCount); //Should contain the duplicate values only.
    Code:
    if (count($sortOrders) < count(array_unique($sortOrders)){
       //Something was removed by array_unique, so there must be a duplicate.
      //Figure out where the duplicate is.
    }
    isnt that backwards? shouldnt it be:

    Code:
    if (count(array_unique($sortOrders) < count($sortOrders) ){
       //Something was removed by array_unique, so there must be a duplicate.
      //Figure out where the duplicate is.
    }
    ??

    say 5 total numbers... so the count should be 5 .. and if there was a duplicate, the arrray_unique would be 4....

    i could do:

    Code:
    if (count(array_unique($sortOrders) != count($sortOrders) ){
       //Something was removed by array_unique, so there must be a duplicate.
      //Figure out where the duplicate is.
    }
    as a general case?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo