-1

I'm curious to know what the syntax " : " mean in php I've seen it a couple of times but I can't seem to explain it to myself. Can you also use it in a sentence....or i mean, sample code?

**edit:

sorry folks, I was referring to the ternary operator. Thanks for the other entries as well. I didn't know what to call it at first, apologies.

Dan
  • 27
  • 1
  • 1
  • 6
  • 3
    That character can be used in multiple contexts. Maybe *you* should use it in a sentence so we can all know what you're really asking about. Copy and paste a couple of lines that demonstrate its use from whatever code you're trying to understand. – Rob Kennedy Dec 29 '09 at 18:24

7 Answers7

10

Perhaps you are referring to the ternary operator, which uses a ? and : as follows:

$variable = boolean_expression ? "true_value" : "false_value";

This code is shorthand for an if-else:

if (boolean_expression) {
   $variable = "true_value";
}
else {
   $variable = "false_value";
}
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
9

It's the ternary operator:

echo ($a == 1 ? "A is 1" : "A is not 1");
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
6

How about the shorthand syntax for blocks in PHP embedded in HTML? For example

<body>
   <h1>Some Header</h1>
   <?php if($somevariable == '4') : ?>
      <h2>Some other thing</h2>
      <p>Some text</p>
   <?php else: ?>
      <h3>Else!</h3>
   <?php endif; ?>
</body>

Probably doesn't necessarily count as an operator. More of a delimiter here.

Marc W
  • 19,083
  • 4
  • 59
  • 71
  • Doesn't *have* to be embedded in HTML, does it? It's simply an alternate syntax for most constructs that would otherwise use braces. – Rob Kennedy Dec 29 '09 at 23:12
  • It was created to get rid of nonsensical braces spread through PHP tags embedded through HTML. If you have a giant block of PHP in HTML (horrors of doing this aside), you wouldn't need to use them. It's just for wrapping chunks of HTML with PHP. – Marc W Dec 30 '09 at 02:45
5

It can also refer to a goto

MyGoto:
    if (DoSomething())
        goto MyGoto;

Very few circumstances warrant a goto, but that's what it can mean if not a ternary operator.

Brett Allen
  • 5,297
  • 5
  • 32
  • 62
2

The ?: operator is a ternary operator called the conditional operator.

It is conditional because the expressions expr2 and expr3 in expr1 ? expr2 : expr3 are evaluated based on the evaluated return value of expr1:

  • If expr1 evaluates to true, expr2 is evaluated and the return value of expr2 is the return value of the whole ?: operator expression;
  • otherwise expr3 is evaluated and the return value of the ?: operator expression is the return value of expr3.

Here’s an example:

echo 1 == 1 ? "true" : "false";

If 1 == 1 evaluates to true, "true" will be echoed, otherwise "false".

Note that the ?: operator is just a and not the ternary operator. The word ternary just means that there are three operands (op1 ? op2 : op3) just like a binary operator has two operands (e.g. op1 + op2, op1 / op2, op1 % op2, etc.) and unary operators just have one operand (e.g. !op, -op, ~op, etc.).

Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

Are you talking about the conditional operator?


$a = $gork === 1 ? $foo : $bar;

Check out the "Ternary Operator" section on this page: http://php.net/manual/en/language.operators.comparison.php

It's basically a short cut for an if else, the above code is the same as:


if($gork === 1)
    $a = $foo;
else
    $a = $bar;

mmattax
  • 27,172
  • 41
  • 116
  • 149
0

This is a short-form conditional expression, known in PHP as the "ternary operator." See the PHP manual for more details on its usage.

echo ($sheLovesMe ? "She loves me!" : "She loves me not!");
Karmic Coder
  • 17,569
  • 6
  • 32
  • 42