General syntax question

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
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

General syntax question

Post by taldos »

the following is a code snippet from a website which I am currently working on. My confusion lies in the use of "?" and ":".

Code: Select all

$phpExt = array('Enroll','Login','ContactUs','sendPW','ProviderNetwork','ResellerEnrollment','ResellerTest');
$extention = (in_array($_GET['pg'],$phpExt)) ? 'php' : 'html';
In this particular case, I'm assuming it means get the file with ".php" extension. if that doesn't work get the file with ".html" extension.

Cheers
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

It is a ternary operator.

It's like a shorthand if statement for declaring variables.

$var = condition ? then : else;

(i think)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: General syntax question

Post by aerodromoi »

taldos wrote:the following is a code snippet from a website which I am currently working on. My confusion lies in the use of "?" and ":".

Code: Select all

$phpExt = array('Enroll','Login','ContactUs','sendPW','ProviderNetwork','ResellerEnrollment','ResellerTest');
$extention = (in_array($_GET['pg'],$phpExt)) ? 'php' : 'html';
In this particular case, I'm assuming it means get the file with ".php" extension. if that doesn't work get the file with ".html" extension.

Cheers
That's a short conditional statement.

Code: Select all

expression ? true-statement: false-statement
So in this case, the script will choose the php extension if $_GET['pg'] is in the array,.

aerodromoi
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post by taldos »

thx guys. :)
Post Reply