Jump to content

[SOLVED] What does this code do?


oskom

Recommended Posts

Hello all...again,

forum helper, rab, was kind enough to provide me with some useful code:

<?php
$_REQUEST['contentID'] = (int)$_REQUEST['contentID'] < 0 ? 0 : (int)$_REQUEST['contentID'];

if( $_REQUEST['contentID'] > 0 ) {
   $result = mysql_query("SELECT * FROM content WHERE contentID = '{$_REQUEST['contentID']}'");
   if(!$result) {
      // ERROR
   }
} else {
   // Need an ID
}
?>

 

Problem is...I'm not totally clear on what the first line is doing(please forgive the novice smell). In other words, I know what this part is doing...

$_REQUEST['contentID'] = (int)$_REQUEST['contentID'] 

...but not this part...

 < 0 ? 0 : (int)$_REQUEST['contentID']

I'm thinking it's some kind of short-form if/else statement, but...

 

Enlightenment?

Link to comment
https://forums.phpfreaks.com/topic/80200-solved-what-does-this-code-do/
Share on other sites

Yes it's a short and simplified form of if-then-else.  You use it where you want to use either one value or another value.  For example, if you are generating html for a checkbox that may or may not be checked

 

$checked_text = ($box_is_checked ? " CHECKED " : "");

 

Or if you want to set a number to 0 if it's less than 0

 

$num = ($num < 0 ? 0 : $num);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.