0

Oké. I saw this piece of code and I was curious about how it works and what it does. Can anyone explain this to me? Thanks!

$_SESSION['langtype']
   = (empty($_SESSION['langtype'])) ? 'false' : $_SESSION['langtype'];
Alex
  • 32,506
  • 16
  • 106
  • 171
Obe
  • 21
  • 3
  • 2
    it sets $_SESSION['langtype'] to false if it's not set. Otherwise it just sets it again with the current value – PoeHaH Oct 17 '12 at 12:55
  • 2
    its a Ternary Operator view this link http://php.net/manual/en/language.operators.comparison.php – pkachhia Oct 17 '12 at 12:56
  • 1
    Shouldn't you use 'isset', if you're not sure if the array element is set? Because you see, if langtype was '0', it'd also set it to 'false'. – ATaylor Oct 17 '12 at 12:58
  • 1
    you might find this reference helpful: [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/q/3737139/367456) – hakre Oct 17 '12 at 13:05
  • it's an inline if: `(expression) ? : ;` – Sven van Zoelen Oct 17 '12 at 13:10

7 Answers7

3

It puts false in $_SESSION['langtype'] is the value is not set, otherwise it keeps the current value.

See also http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

Alex
  • 32,506
  • 16
  • 106
  • 171
1

It puts "false" in $_SESSION['langtype'] (as a STRING, not BOOL VALUE) in case $_SESSION['langtype'] is empty (or value with key langtype does not exist), otherwise keeps the same value.

It's probably bad idea to put "false" as a string. For example if the author of this masterpiece decides to do a check if ($_SESSION['langtype']) { }, it will return true in any case. I'm curious in which case this solution should be reasonable.

ddinchev
  • 33,683
  • 28
  • 88
  • 133
1

Its a short hand for if-else statement. if (empty($_SESSION['langtype'])) then $_SESSION['langtype'] = false else $_SESSION['langtype'] = $_SESSION['langtype']

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
1

This is ternary Operator used in this Statement

$_SESSION['langtype'] = (empty($_SESSION['langtype'])) ? 'false' : $_SESSION['langtype'];

It means If Session variable named langtype is empty then return false, Otherwise use langtype same as defined

The portion after ? represent the value if function

empty($_SESSION['langtype'])
returns true(Mean if it is empty then set it false or un-define that variable) i.e
$_SESSION['langtype'] = false; 

and portion after : represent else statement that is if langtype is not empty then keep it as it is(Equal to Defined Value) as

$_SESSION['langtype']=$_SESSION['langtype']; 
Umair Aziz
  • 1,518
  • 1
  • 19
  • 29
1

It is called a ternary operator. it consists of condition expression, and return values for both evaluations of the condition.

// if expression evaluates to true first value will be returned,
// otherwise it will the second 
$variable = (expression) ? 'value if true' : 'value if false';
Headshota
  • 21,021
  • 11
  • 61
  • 82
0

if it is not set(true) then it will be false else if its (false) it ill print the session value

0
$_SESSION['langtype'] = (empty($_SESSION['langtype'])) ? 'false' : $_SESSION['langtype'];

For a beginner the above code is the same as the long version which you might know

<?php
     if(empty($_SESSION['langtype'])) {
           $_SESSION['langtype'] = 'false';
     } else {
          $_SESSION['langtype'] =  $_SESSION['langtype'];
     }
 ?>
donald123
  • 5,638
  • 3
  • 26
  • 23