Skip to main content

What is the PHP ? : operator called and what does it do?



Can someone please explain what the "?" and ":" operators are in PHP?





e.g.:







(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)





Source: Tips4all

Comments

  1. This is the conditional operator.

    $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.

    ReplyDelete
  2. It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.

    It's basically a shorthand if statement, the above code could also be rewritten as follows:

    if ($request_type == 'SSL') {
    HTTPS_SERVER;
    } else {
    HTTP_SERVER;
    }

    ReplyDelete
  3. 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.

    $hello = $something ? "Yes, it's true" : "No, it's false";

    ReplyDelete
  4. This is a short way of writting IF sentences. It is also used in other languages like Java, Javascript and others.

    Your 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;

    ReplyDelete
  5. 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.

    ReplyDelete
  6. That's basically a fancy way if writing an if else statement.

    Some say its easier to read, some say not.

    ternary operator at wikipedia

    ReplyDelete
  7. 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;

    if($request_type == 'SSL')
    {
    HTTPS_SERVER;
    }
    else
    {
    HTTP_SERVER;
    }

    ReplyDelete
  8. That is a one line if statement:

    condition ? true : false


    Translated to an ordinary if statement in your case, that would be:

    if($request_type == 'SSL') HTTPS_SERVER;
    else HTTP_SERVER;

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex