Quick Guide conditional if

More than once I was looking on the internet write a summary of the conditionally if. It is often useful, especially when assigning a value to a variable. In this case it is a conditional assignment of values ​​for the variable.
Quick Guide conditional if
There is not much to write, these things are best explained and illustrated with examples.

Standard Operational if:

PHP Code:

Mark


  1. if ($ Condition) {
  2. / / Do the if $ condition is true
  3. }
  4. else {
  5. / / Do the if $ condition is not satisfied
  6. }


Slightly shorter version of the manual if:

PHP Code:

Mark


  1. if ($ Condition) / / Do the if $ condition is true
  2. else / / Do the if $ condition is not satisfied


very short version of the manual if:

PHP Code:

Mark


  1. ($ Condition) ? / * Do if $ condition is true * / : / * Do if $ condition is not satisfied * /;


What, in practice, for example, looked at me like this:

PHP Code:

Mark


  1. $ Jezyk_strony = (!$ _GET['Jezyk_strony']) ? $ Cfg['Domyslny_jezyk_strony'] : $ _GET['Jezyk_strony'];

Related entries:

  • A tutorial on creating web sites for beginners - CSS, HTML, PHP, MySQL, JavaScript

    A tutorial on creating web sites for beginners - CSS, HTML, PHP, MySQL, JavaScript

    Actually, course page creation is too much said, but this article will present the basic, useful information for those wishing to get started with web development. People who design web pages relatively short, or just do not feel this good, it will find the information and sometimes do not go in the wrong direction. At the beginning we know very foundations, that is, what language to what is (or rather what should .. because you know that ...

Comments:

  1. On Monday, August 13, 2012, at 16:34 Young wrote / a:

    As a curiosity append the operator introduced in PHP Elvis and you can even shorten the record even more :) but showing an example:

    In PHP 5.2 and lower ...

    $ Jezyk_strony = ($ _GET ['jezyk_strony'])? $ _GET ['Jezyk_strony']: $ cfg ['domyslny_jezyk_strony'];

    In PHP 5.3:

    $ Jezyk_strony = ($ _GET ['jezyk_strony'])?: $ Cfg ['domyslny_jezyk_strony'];

Add a comment

* - Required field