JPark Posted October 22, 2009 Share Posted October 22, 2009 I am trying to rework a page written by someone else and there is a function function generate_click_box($row) { $html = <<<HTML <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background:#036; overflow:hidden;"></div> <div id="%1\$s_toggle" style="height:45px; width:191px; margin:0 auto; background:#036; color:#fff; cursor:pointer; font-size:12px; font-weight:bold; text-align:center; padding:0; margin:0;"><div style="padding:2px; vertical-align:middle;">%2\$s</div></div> <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background:#036; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background:#036; overflow:hidden;"></div> HTML ; $html_disabled = <<<HTML <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:45px; width:191px; margin:0 auto; background: #cccccc; color:#036; font-size:12px; font-weight:bold; text-align:center; padding:0; margin:0;"><div style="padding:2px; vertical-align:middle;"> <a href="http://factfinder.census.gov/servlet/EconSectorServlet?caller=dataset&sv_name=*&_SectorId=%1\$s&ds_name=EC0700A1&_lang=en&_ts=272288383987" target="_blank" alt="Click for %2\$s data">%2\$s<br />COMPLETED</a><br /></div></div> <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background: #cccccc; overflow:hidden;"></div> HTML ; return ($row[4]) ? sprintf($html, $row[3], htmlentities( $row[1] ) ) : sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) ); } Can someone translate (put into pseudo code) the following piece return ($row[4]) ? sprintf($html, $row[3], htmlentities( $row[1] ) ) : sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) ); Thanks! Joe Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/ Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 That's called a ternary statement. So if you have something of the form cond ? ifTrue : ifFalse then ifTrue will be returned if cond evalutes to true, and ifFalse will be returned otherwise. You could rewrite it to the following: if ($row[4]) { return sprintf($html, $row[3], htmlentities( $row[1] ) ); } else { return sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) ); } Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942018 Share on other sites More sharing options...
JPark Posted October 22, 2009 Author Share Posted October 22, 2009 Ok. Makes a bit of sense... So, why does my page crap out (nothing at all displays) if I change it to return ($row[4]) ? sprintf($html, $row[3], htmlentities( $row[1] ) ) : echo "hello"; Isn't this saying that $row[4] is true then do sprintf($html, $row[3], htmlentities( $row[1] ) ) but if $row[4] is false then echo "hello"? Joe Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942028 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Because echo doesn't return any value. You'd just want to do this: return ($row[4]) ? sprintf($html, $row[3], htmlentities( $row[1] ) ) : "hello"; Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942034 Share on other sites More sharing options...
ILMV Posted October 22, 2009 Share Posted October 22, 2009 Well, it is returning a value and thus exiting that sequence of code before it gets to the condition statement... can someone else confirm this? Edit: Hmmm, maybe I am ready the code wrong :/ Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942035 Share on other sites More sharing options...
JPark Posted October 22, 2009 Author Share Posted October 22, 2009 Because echo doesn't return any value. ok... : "hello"; does what I thought it should do to begin with but I don't quite understand what you wrote... Is the problem from the return ($row[4])? Is that why the page is looking for something to be returned? Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942039 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Hmm... well, do you understand what it means to return a value from a function? The ternary statement will return something based on the truth value of $row[4], and the function will then return whatever the ternary statement returns. Quote Link to comment https://forums.phpfreaks.com/topic/178608-pseudo-code/#findComment-942042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.