p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: colon syntax


Message #1 by "Jake" <jakech@n...> on Thu, 27 Feb 2003 08:21:56
I came across the following line of code:

($var == "abc" ? $var2 : $var);

I'm sure I read something explaining what this meant, e.g. the use of the 
colon - but I can't find it now!  I thought it was a "switch" statement 
but on further reading, no :(

Can someone tell me what the above is saying and - more importantly - 
where I can read up about this syntax.

Finally, I've also seen the use of "->" e.g. "<?=$a->score?>" but don't 
recall reading about "->".

Thanks for helping this newbie.

Jake
Message #2 by "Pedro Graca" <bzzzt@f...> on Thu, 27 Feb 2003 10:55:29 +0000
On Thu, 27 Feb 2003 08:21:56, Jake said:
> I came across the following line of code:
> 
> ($var == "abc" ? $var2 : $var);

The expression
  $one ? $two : $three
is called the "ternary conditional operator"
it takes the value of $two if $one is true (or equivalent)
or $three if $one is false ... so you can do
  $one ? $two : $three
and do to the resulting value ($two or $three) whatever you want

The example you wrote is (with extra parenthesis for legibility)
  ( $var == "abc" ) ? $var2 : $var
the colon isn't part of the expression;
it is part of the statement, which could be something like:
  echo ( $var == "abc" ) ? $var2 : $var;


> Finally, I've also seen the use of "->" e.g. "<?=$a->score?>" but don't 
> recall reading about "->".

This is the way to reference a class function or variable
<?php
  class TestClass {
    var $text;
    var $number;
    function text2number() {
      $this->number = 1.0 * $this->text;
    }
  }

  $x = new TestClass;   // new variable
  $x->text = "567";     // assign the string "567"
  $x->text2number();    // call the function
  echo $x->number;      // print the class number
?>

Hope this helps ... I'm not really used to working with classes
-- 
                                            ()    ribbon campaign     ()
                                            /\   against HTML mail    /\
Message #3 by Krister <krister@m...> on Thu, 27 Feb 2003 11:17:17 +0100
On 27/02/03 8:21, "Jake" <jakech@n...> wrote:

> I came across the following line of code:
> 
> ($var == "abc" ? $var2 : $var);
> 
> I'm sure I read something explaining what this meant, e.g. the use of the
> colon - but I can't find it now!  I thought it was a "switch" statement
> but on further reading, no :(
> 
> Can someone tell me what the above is saying and - more importantly -
> where I can read up about this syntax.
> 
> Finally, I've also seen the use of "->" e.g. "<?=$a->score?>" but don't
> recall reading about "->".
> 
> Thanks for helping this newbie.
> 
> Jake
Hi

I suppose it should go:

$var = ( $var == "abc" ) ? $var2 : $var;

And it is very same as:

if ( $var == "abc" ) {
    $var = $var2;
} else {
    $var = $var;
}

So, it's only a short way to say if something...

To read about "->" check what there is of classes.
this->$var is pointing to a variable in a function, but to use it you need
to read more of makeing classes.

Message #4 by "Nikolai Devereaux" <yomama@u...> on Thu, 27 Feb 2003 10:32:11 -0800

> $var = ( $var == "abc" ) ? $var2 : $var;
>
> And it is very same as:
>
> if ( $var == "abc" ) {
>     $var = $var2;
> } else {
>     $var = $var;
> }
>
> So, it's only a short way to say if something...


The key thing to remember is that : evaluates to a value.  If it helps, you
can think of the : operator as this function:


function colon($test, $lhs, $rhs)
{
   if($test) return $lhs;
   else      return $rhs;
}


Now, the statement


echo "Hello there, " . ($name != '')? $name : "<no name>";


is equivalent to:

echo "Hello there, " . colon($name, $name, "<no name>");



HTH,

Nik


  Return to Index