p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > PHP/MySQL > Beginning PHP
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 24th, 2004, 03:02 PM
Authorized User
Points: 140, Level: 2
Points: 140, Level: 2 Points: 140, Level: 2 Points: 140, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Coeur d\'Alene, ID, USA.
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default ? operator in PHP

http://php.planetmirror.com/manual/e....operators.php

shows the ? operator but I can't seem to find a description of what it means or what it does. Does anyone know?

__________________
An overworked Web Developer who\'s expected to know everything yet given time to study nothing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 24th, 2004, 03:06 PM
Friend of Wrox
Points: 2,801, Level: 22
Points: 2,801, Level: 22 Points: 2,801, Level: 22 Points: 2,801, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a brief description:

The '?' operator is like a shortcut for the if() function.

This:

if($variable === TRUE) doSomething();
else doSomethingElse();

Is equivalent to this:

($variable === TRUE) ? doSomething() : doSomethingElse();

HTH,

Snib

<><
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old May 24th, 2004, 03:59 PM
richard.york's Avatar
Wrox Author
Points: 5,386, Level: 31
Points: 5,386, Level: 31 Points: 5,386, Level: 31 Points: 5,386, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,697
Thanks: 0
Thanked 1 Time in 1 Post
Default

It's called the ternary operator, and its an expression, if / else if / else are control structures, so technically it isn't just a shortcut for if/else

The manual shows an example like this:

<?php
$first ? $second : $third;
?>

Each of the variables represents a sub expression,
If the value $first subexpression evaluates to TRUE, the code contained in $second subexpression is evaluated if the $first subexpression evaluates as FALSE the $third subexpression is evaluated. Commonly you'll find this in a use that is representable by a simple if/else structure.

if ($foo == TRUE) {

    $bar = 1;
} else {

    $bar = 0;
}

Is the same thing as saying:

$bar = ($foo == TRUE)? 1 : 0;

You can find it in the PHP manual here:
http://www.php.net/manual/en/language.expressions.php

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
=== operator in c# surendraparashar C# 2005 8 November 8th, 2007 05:14 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 05:51 AM
how to use like operator thillaiarasu SQL Server 2000 3 April 25th, 2007 09:53 AM
SQL 'Between' operator daveyh BOOK: Beginning SQL 2 April 4th, 2007 04:25 PM
Use of return operator. PC BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 July 1st, 2005 10:54 AM



All times are GMT -4. The time now is 08:01 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc