What does it mean?

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
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

What does it mean?

Post by infomamun »

Hi there
Below is a php syntax from admob (one advertising network) code:

Code: Select all

$rt = $ad_mode ? ($analytics_mode ? 2 : 0) : ($analytics_mode ? 1 : -1);
  if ($rt == -1) return '' ";
Would anybody explain me what are the meanings of "?" or ":" here?
Or in another word, what the syntax is telling actually?


It will be appreciated if someone gives me the links of online articles to know the uses of "?" or ":" in php syntax.

Regards
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: What does it mean?

Post by shiznatix »

It is a simplified if{}else{} statment. Basically, this line:

Code: Select all

$rt = $ad_mode ? ($analytics_mode ? 2 : 0) : ($analytics_mode ? 1 : -1);
is exactly the same as this:

Code: Select all

if ($ad_mode)
{
    if ($analytics_mode)
    {
        $rt = 2;
    }
    else
    {
        $rt = 0;
    }
}
else
{
    if ($analytics_mode)
    {
        $rt = 1;
    }
    else
    {
        $rt = -1;
    }
}
so the first part is the condition ($something == $other) then comes if that first part evaluates to true ? so ($something == true ? 'it was true') then the : is the else part ($something == true ? 'it was true' : 'it was not true') and it returns either 'it was true' or 'it was not true' which can be then assigned to a variable like:
$was_it_true = ($something == true ? 'it was true' : 'it was not true');
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: What does it mean?

Post by twinedev »

On a technical side, it is called ternary conditional operator and in the docs, just barely mentioned on: http://php.net/manual/en/language.expressions.php

-Greg
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: What does it mean?

Post by infomamun »

Thanks a lot to both of you. Now I can understand it easily.
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: What does it mean?

Post by infomamun »

one more question. I have posted a topic separately in this forum. Address of that topic is:
viewtopic.php?f=1&t=122516

What the query was in that topic, is-
A remote website/server has applied a redirection to a page, i.e it has applied a

Code: Select all

header(Location:'/newpath');
So while trying to grab the previous page, header returns 302 redirection with the following new location of the page:

Location: http://www.example.com/pageA.php?file=onefile?230562

so in that case, is the second "?" mark is a ternary operator? I meant is the header telling that

if(user click http://www.example.com/pageA.php?file=onefile){
then the new location is http://www.example.com/pageA.php?file=230562
}

haha...it's just my speculation. Is my thought is right or wrong? :lol:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: What does it mean?

Post by s.dot »

The ternary operator is strictly a programming thing.. Not a url thing.

On another note, nested ternary operators are terribly unreadable. One at a time is great though and may improve code readability.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply