SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not familiar with this syntax

    PHP Code:
    $ratio = ($width $height) ? $th_max_width/$width $th_max_height$height
    I've got a question about the syntax of this code. I'm tinkering around with resizing images. This is the code that apparently figures out how to keep the dimensions proportional.

    However, the question mark is really throwing me off.

    I'll take a stab at this one just to see if I'm right or even close.

    HTML Code:
    If ($width > $height) {$th_max_width/$width } else {$th_max_height/ $height; }
    Well, that's not it. It's clear that this is some kind of ratio (DUH!). Hmm.

    Brandon
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well,
    if width is greater than height, $ratio is assigned the value of
    $th_max_width/$width
    else $ratio is assigned the value of
    $th_max_height/ $height

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    go through this, espcially the last comment.
    http://www.php.net/manual/en/control...ive-syntax.php

  4. #4
    SitePoint Enthusiast shn's Avatar
    Join Date
    Jan 2006
    Location
    Munich DE
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Brandon,

    except from the fact, that you forgot to assign the calculated values to $ratio, you got it absolutely right. See PHP Mnual: Ternary Operator for more Information.
    Patrick

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. Makes sense now.

    I wasn't aware of this alternate syntax junk.

    Is there any benefit to using it?

    Brandon
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    except from the fact, that you forgot to assign the calculated values to $ratio, you got it absolutely right.
    Cool. I meant to put the $ratio equals in there but got in a hurry.

    This is just another thing to confuse me.

    Brandon
    Home Recording Forum -
    Make 60% Commission Pushing my new mega system
    Killer Home Recording

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's just a more compact way to write an if/else statement. It's convenient sometimes.

  8. #8
    SitePoint Addict AfroNinja's Avatar
    Join Date
    Oct 2006
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does ternary actually compute faster than an (optimally written) if/else? If anything it seems the ternary operator is used to make code more compact and fancier looking : )
    The Flash Gaming Network
    Editorial reviews for the latest flash games!
    Afro Ninja Productions
    Original flash games and content from a guy with an afro

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ternary is slower.


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
  •