Jump to content


Photo

Question: about ? and :


  • Please log in to reply
10 replies to this topic

#1 PureEvil

PureEvil
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 July 2006 - 03:04 PM

I've pretty much learned all of my PHP from a few books I'ver purchased and of course this kick ass web site.

The question is this. I keep seeing code like the following
$row_color = ($Rcount % 2) ? $Talt_color1 : $Talt_color2;

What does the ? and the : mean in that string? I've been unable to find these in my books.

Thanks in advance.

#2 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 20 July 2006 - 03:13 PM

I'm in the same boat.

every book or CD that i have used dosen't explain ? or :

Can anyone explin exactly what this line of code does?
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}

Cheers
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 03:16 PM

It's an if/else statement I believe.

(if) ? true : false (but I might be wrong on the order.)

#4 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 03:18 PM

Aha! From the manual:
Left associativity means that the expression is evaluated from left to right, right associativity means the opposite. Example 15-1. Associativity

<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
?>  
 
Use parentheses to increase readability of the code. 


http://us2.php.net/m...e.operators.php

#5 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 20 July 2006 - 03:24 PM

cheers,

I'll try and get my head around it.
;D
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#6 PureEvil

PureEvil
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 July 2006 - 04:49 PM

It's an if/else statement I believe.

(if) ? true : false (but I might be wrong on the order.)


Ok I think I'm getting it maybe.

Basically in a nutshell your saying that if something is true or false do whats true or false.

Example:
$i=1;
$Var1 = "i is 1";
$Var2 = "i is not 1";
$Var3 = ( $i == 1 ) ? $Var1 : $Var2;
echo("$Var3"); // should produce "i is 1"

Is that correct?

#7 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 04:50 PM

I'm not sure really.  I'm at work, so I cant try, but if someone could try/confirm this, I'd be interested to know.

#8 PureEvil

PureEvil
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 July 2006 - 04:54 PM

I'm not sure really.  I'm at work, so I cant try, but if someone could try/confirm this, I'd be interested to know.


Seems to work... That is awesome and will come in extra handy. Honest to god I have four php books I pretty much live in while I'm trying to write my scripts, and not one of them said anything about this. Very handy...

#9 scliburn

scliburn
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 20 July 2006 - 04:55 PM

yes you are correct, it simplifies an if/else alot like the switch statement

$thefunc = ((isset($var)) && ($var != '')) ? _function ( $var, $var2 ) : $content = 'No Word Entered<br /><br />';

you can use it call and pass vars through a function and then $thefunc could have the value returned from your functions.. just an example

#10 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 04:56 PM

It can also be shortened to:

echo $i == 1 ? $Var1 : $Var2;

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#11 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 05:07 PM

it's a "ternary" operator that's adopted from C like languages. It's really sad to see php books don't cover this operator much. This is a really handy operator. You can even use it in a concatenation.

echo 'This is a '.( $good ? 'good' : 'bad'). 'book';

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users