Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Thursday, April 12, 2012
What is the PHP ? : operator called and what does it do?
Can someone please explain what the "?" and ":" operators are in PHP?
means "if $x is true, then use $y; otherwise use $z".
People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.
This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.
Conditional operator ?: is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:
variable = condition ? expression1 : expression2;
It works as follows...
Firstly, condition is evaluated. If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable. If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.
For example:
x = (a>b) ? 5 : 9
In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).
Ternary Operator
Sometimes conditional operator ?: is also called a ternary operator. This is so because it involves three operands. For example:
x ? y : z
Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.
As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;
This is the conditional operator.
ReplyDelete$x ? $y : $z
means "if $x is true, then use $y; otherwise use $z".
People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.
It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.
ReplyDeleteIt's basically a shorthand if statement, the above code could also be rewritten as follows:
if ($request_type == 'SSL') {
HTTPS_SERVER;
} else {
HTTP_SERVER;
}
This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.
ReplyDelete$hello = $something ? "Yes, it's true" : "No, it's false";
This is a short way of writting IF sentences. It is also used in other languages like Java, Javascript and others.
ReplyDeleteYour code:
$protocol = $request_type == 'SSL' ? HTTPS_SERVER : HTTP_SERVER;
can be written like this:
if($request_type == 'SSL')
$protocol = HTTPS_SERVER;
else
$protocol = HTTP_SERVER;
Conditional operator ?: is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:
ReplyDeletevariable = condition ? expression1 : expression2;
It works as follows...
Firstly, condition is evaluated.
If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable.
If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.
For example:
x = (a>b) ? 5 : 9
In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).
Ternary Operator
Sometimes conditional operator ?: is also called a ternary operator. This is so because it involves three operands. For example:
x ? y : z
Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.
That's basically a fancy way if writing an if else statement.
ReplyDeleteSome say its easier to read, some say not.
ternary operator at wikipedia
As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;
ReplyDeleteif($request_type == 'SSL')
{
HTTPS_SERVER;
}
else
{
HTTP_SERVER;
}
That is a one line if statement:
ReplyDeletecondition ? true : false
Translated to an ordinary if statement in your case, that would be:
if($request_type == 'SSL') HTTPS_SERVER;
else HTTP_SERVER;