Question

I've got this snippet of code that I am trying to understand what it does.. Any help would be awesome

$returnValue= 0;

if (is_int($bottleNumber/12)){
    $returnValue=1;
}

echo $_GET['callback']. '('. json_encode($returnValue) . ')'; 
Was it helpful?

Solution

It prints out Javascript code to call the function named in $_GET['callback'].

If the URL is mypage.php?callback=alert, it will check to see if $bottleNumber is a multiple of 12, then write

alert(1)

if it was (or alert(0) if it wasn't).

The callback parameter could be anything, so you can change the job of the script by changing the one parameter.

OTHER TIPS

First, you see that it is setting up everything as false.

$returnValue= 0;

This is not necessary, but good practice to ensure that you get proper results.

if (is_int($bottleNumber/12)){
    $returnValue=1;
}

Here, $bottleNumber is divided by 12. If that number is an integer (no remainder), then we know it is divisible by 12. Order of operations much like normal math here. If it is cleanly divisible by 12, then we take note of it and hit our switch.

Now, we return either 1 or 0. This is why we set $returnvalue to 0 originally. It's a 'fallback' value.

This checks if a number is divisible by 12 and returns an appropriate JSON response

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top