Wrox Programmer Forums
| 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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 24th, 2004, 02:02 PM
Authorized User
 
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.
  #2 (permalink)  
Old May 24th, 2004, 02:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 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

<><
  #3 (permalink)  
Old May 24th, 2004, 02:59 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
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
::::::::::::::::::::::::::::::::::::::::::


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use like operator thillaiarasu SQL Server 2000 4 April 23rd, 2010 09:47 AM
=== 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 04:51 AM
SQL 'Between' operator daveyh BOOK: Beginning SQL 2 April 4th, 2007 03:25 PM
Use of return operator. PC BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 July 1st, 2005 09:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.