PHP syntex confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

PHP syntex confusion

Post by gautamz07 »

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 .

Code: Select all

<?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>';
	}
?>


can somebody explain to me this line in the code .

Code: Select all

$offset = ($_GET['offset'])?$_GET['offset']:0;
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP syntex confusion

Post by requinix »

That is the ternary operator. It's like an if statement but it "returns" a value instead of executing statements.
Set $offset to (if $_GET['offset'] is truth-y then use $_GET['offset'] else use 0).

Code: Select all

if ($_GET['offset']) {
    $offset = $_GET['offset'];
} else {
    $offset = 0;
}
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: PHP syntex confusion

Post by gautamz07 »

thanks
Post Reply