Need explain please


#1

hello there,

I’m going through ready examples and codes as part of my research in PHP, i’m still learning. I have this block of code, i kind of understand 70% of what it does. Would someone tell me what it does exactly i would be very greatful.

[php]

IF($_GET[‘month’] == 13){
$next_month = 2;
$next_year = $_GET[‘year’] + 1;
} ELSE {
$next_month = $_GET[‘month’] + 1;
$next_year = $_GET[‘year’];
}

IF($_GET[‘month’] == 2){
$prev_month = 13;
$prev_year = $_GET[‘year’] - 1;
} ELSE {
$prev_month = $_GET[‘month’] - 1;
$prev_year = $_GET[‘year’];
}

[/php]

Thank you
Regards,
Multi


#2

Do you want a literal translation or just an overview? Overview - at first I thought it was a leap year handling routine but now I’m not so sure.

Literally:

// if the user's input for month is equal to 13 do this
IF($_GET['month'] == 13){
    // set next_month equal to 2
    $next_month = 2;
    // set next_year equal to the user's input plus 1 (increment year)
    $next_year = $_GET['year'] + 1;
}
//if the user's input for month is NOT equal to 13 do this 
ELSE {
    // add 1 to the user's input for month (increment month)
    $next_month = $_GET['month'] + 1;
    // set next_year to the user's input
    $next_year = $_GET['year'];
}

// if the user's input for month is equal to 2 do this 
IF($_GET['month'] == 2){
    // set next_month equal to 13
    $prev_month = 13;
    // set next_year equal to the user's input minus 1 (decrement year)
    $prev_year = $_GET['year'] - 1;
} 
// if the user's input for month is NOT equal to 2 do this 
ELSE {
    // minus 1 to the user's input for month (decrement month)
    $prev_month = $_GET['month'] - 1;
    // set next_year equal to the user's input 
    $prev_year = $_GET['year'];
} 

Hope that gives you what you are looking for.


#3

[quote=“Multi”]hello there,

I’m going through ready examples and codes as part of my research in PHP, i’m still learning. I have this block of code, i kind of understand 70% of what it does. Would someone tell me what it does exactly i would be very greatful.

[php]

// Checks for a variable passed in the URL of month
// i.e. http://domain.com/index.php?month=13&year=2004

// This portion is if it’s at the end of a year
IF($_GET[‘month’] == 13){
// Presumably there is code that says CUR_MONT= 1
// Thus next month is CUR_MONTH +1 or 2
$next_month = 2;
// Just updating the year for NEXT year since we have moved
// to a new year (when getting to the end of the year)
$next_year = $_GET[‘year’] + 1;

} ELSE {
// So if the month is not 13 (i.e. LESS than 13)
// Next month is the current month PLUS 1
$next_month = $_GET[‘month’] + 1;
// I guess this is the next year being passed in the URL
$next_year = $_GET[‘year’];
}

// this portion is if it’s at the beginning of the year
// And is very similar to the portion above.
IF($_GET[‘month’] == 2){
$prev_month = 13;
$prev_year = $_GET[‘year’] - 1;
} ELSE {
$prev_month = $_GET[‘month’] - 1;
$prev_year = $_GET[‘year’];
}

[/php]

As I was looking through this I thought to myself that this was a horrible way to do this, but hind sight, It’s tough to say without seeing the entire code.

Any way… Hope this helps