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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.