-1

EDIT FOR THE ADMINS: IT IS NOT THE SAME QUESTION AS THE ONE ALREADY ASKED, SINCE THE ORIGIN OF THE ISSUE IS DIFFERENT!!!

I am trying to display the current language selected, which is saved in the sessions table. What I did first was the simple statement:

<?php echo $this->session->userdata("language"); ?>

which works quite well. The problem here is, that the language is saved into the session table in English and lower case, means: "english", "german", "spanish", etc

I then tried to resolve this using an if statement as follows:

<?php if ($this->session->userdata("language") = spanish) {  echo 'Español'; } else if ($this->session->userdata("language") = english) {  echo 'English'; } else echo 'Deutsch'; ?>

unfortunately, this returns:

Fatal error: Can't use method return value in write context in /home/.../.../.../app/views/header.php on line 270

Any hint on what I am doing wrong? Thanks for your quick help ;)

Ben
  • 83
  • 1
  • 12

4 Answers4

2

You need to use a comparison operator == (I'm sure your = is just the usual common typo), since you can't assign a value (write) to the $this->session->userdata('anything') (method return), i.e.

Can't use method return value in write context

 <?php if ($this->session->userdata("language") == 'spanish') {  
          echo 'Español'; 
        } 
       elseif($this->session->userdata("language") == 'english') { 
          echo 'English'; 
      } 
      else echo 'Deutsch'; 
    ?>
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • argh... its always the same when someone asks you to "quickly" code something lol Thanks so much, great help!!! – Ben Oct 24 '13 at 18:09
1

== operator and quoted strings should solve it:

<?php 
if ($this->session->userdata("language") == 'spanish') {  echo 'Español'; } 
else if ($this->session->userdata("language") == 'english') {  echo 'English'; } 
else echo 'Deutsch'; 
?>
Shomz
  • 37,421
  • 4
  • 57
  • 85
0

Make use of a SWITCH Statement to achieve this [Code will really look readable]

<?php 

$language=$this->session->userdata("language");

switch($language)
{
    case "spanish":
        echo 'Español';
        break;

    case "english":
        echo 'English';
        break;

    //.... goes on



}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

To compare two things you should be using the == Operator. Also string values should be wrapped into quotes "myStringValue".