/    Sign up×
Community /Pin to ProfileBookmark

PHP syntex confusion

Hey guys , My names gautam . and i recently started learning PHP because i had to build a small app in Php using the facebook graph API . below is a code snippet that communicates with the fb api to get a list of friends .
[syntax=php]
<?php
require_once(‘config.php’);

$offset = ($_GET[‘offset’])?$_GET[‘offset’]:0;
$limit = 100;
$friends = $facebook->api(‘/me/friends?offset=’ .
$offset . ‘&limit=’ . $limit);
print_r($friends);

if($friends[‘paging’][‘previous’]) {
echo ‘<br><a target=”_top” href=”http://apps.facebook.com/
[your_application_url]/action_paging.php?offset=’ .
($offset-$limit) . ‘”>Previous</a>’;
}

if($friends[‘paging’][‘next’]) {
echo ‘ <a target=”_top” href=”http://apps.facebook.com/
[your_application_url]/action_paging.php?offset=
‘.($offset+$limit).'”>Next</a>’;
}

?>
[/syntax]

can somebody explain to me this line in the code .

[syntax=php]
$offset = ($_GET[‘offset’])?$_GET[‘offset’]:0;
[/syntax]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@rootSep 19.2014 — Thats a ternary

value = (condition) ? if_true_then_this : if_false_then_this ;

you can use it a number of ways, the most common is to be used to set or return a value based on a conditional statement that evaluates to true or false.

so...

[code=php]$offset = ($_GET['offset'])?$_GET['offset']:0;[/code]

could be seen as

[code=php]if($_GET['offset']){
$offset = $_GET['offset']
}else{
$offset = 0;
}[/code]


It sets the value of $offset to 0 if $_GET conditional is false.

also

[B][syntax][/B], don't you mean [B][P[/B][B]HP[/B][B]][/B]
Copy linkTweet thisAlerts:
@NogDogSep 19.2014 — Just want to add link to the official manual page, as it's always a good idea to get the official scoop with all the details, while learning how to use/understand the manual. ?

http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Copy linkTweet thisAlerts:
@gautamz07authorSep 19.2014 — Just want to add link to the official manual page, as it's always a good idea to get the official scoop with all the details, while learning how to use/understand the manual. ?

http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary[/QUOTE]



@127 and thanks @nog thanks , that was helpful , i browesed a little and well it was't as complicated as i taught it would be . ?
Copy linkTweet thisAlerts:
@NogDogSep 19.2014 — Great: we love to see people who can take a little guidance, add some work of their own, and grow with it. ?
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...