Results 1 to 9 of 9

Thread: funciton returs 0

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Resolved funciton returs 0

    first off....php noob...enough said
    I created this function to build the items to go in a list:
    Code:
      function get_lang_menu()
      {
        $lang = get_text("en", "ja", "zh", "de");
        $temp = '';
        if ($lang=="en")  {
          $temp += '<li>English</li>';
        }  else  {
          $temp += '<li><a href="../index.php?lang=en">English</a></li>';
        }
        if ($lang=="ja")  {
          $temp += '<li>Japanese</li>';
        }  else  {
          $temp += '<li><a href="../index.php?lang=ja">Japanese</a></li>';
        }
        if ($lang=="zh")  {
          $temp += '<li>Chinese</li>';
        }  else  {
          $temp += '<li><a href="../index.php?lang=zh">Chinese</a></li>';
        }
        if ($lang=="de")  {
          $temp += '<li>German</li>';
        }  else  {
          $temp += '<li><a href="../index.php?lang=zh">German</a></li>';
        }
        return $temp;
        //return '<li><a href="../index.php?lang=en">English</a></li><li><a href="../index.php?lang=ja">Japanese</a></li><li><a href="../index.php?lang=zh">Chinese</a></li><li><a href="../index.php?lang=de">German</a></li>';
      }
    The "get_text" call does return a value. I checked. But when I call this function like this:
    Code:
    <?php echo(get_lang_menu()) ?>
    , a 0 get printed out.
    If i switch out the last two return lines (comment out one) it works fine with the second hard coded string.
    So I assume my if statement is not what it should be.......right?
    Last edited by StrangerInBeijing; May 22nd, 2007 at 03:06 AM. Reason: Resolved

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: funciton returs 0

    Why do you have to every else statements while the user "select" a language that is associated with that language link? You can't have 2 return statements like that. The last part can just go into your else statement if all the above If statements is not met.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: funciton returs 0

    Like vbbit said, you can only have one return statement in a function within the same scope. If you have more than one, it will simply exit the function upon encountering the first.

    Can you explain to me what the get_text function does? Closest I can find it (http://uk3.php.net/manual/en/function.gettext.php), which is actually get_text, so your code should actually die from a fatal error, assuming you haven't got it defined anywhere else.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: funciton returs 0

    I am putting together 4 list items, each a link to the link to the index.php page, passing a language as parameter. Howerver, the list item listing the current language, should not be a link.
    I'm not having 2 returns. The last one are commented out. I just wanted to demonstrate that I can return a html string of listitems.
    get_text is my own function like this:
    Code:
    	//Select on string out of the passed strings, depending on the user's current language
      function get_text($english, $japanese, $chinese, $german)  {
        //make sure that we have a language selected by using session .. 
        if (!isset($_SESSION['lang'])) { 
          $lang = 'en'; 
        } else { 
          $lang = $_SESSION['lang']; 
        }
        if ($lang=='ja') {
          return $japanese;
        } else if ($lang=='zh') {
          return $chinese;
        }  else if ($lang=='de') {
          return $german;
        }
        else{
          return $english; 
        }   
      }

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: funciton returs 0

    The concatenation operator is . not +, so it should be .=

  6. #6

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: funciton returs 0

    Simple as that. Thanks!

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: funciton returs 0

    HAHA, didn't notice that one.

    += is the concatenation operator in Javascript, Java and I believe C# too. In PHP it simply adds the two values so it was casting the strings to numeric values.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: funciton returs 0

    yep.....C# too. reason for my mistake.

    While I'm at it (and seeing I posted the code here allready) (pls remember..noob!):

    Let's say you want a little menu with menu items depending if the user is logged in or not (Register | Login // Welcome Stranger | My Account | Log out).
    To create a menu I would use a list and style it with css.

    But to build the menu, depending whether the user are logged in or not, is it ok to build up a string in a php function (which will see if the user is logged in, etc) and then echo that string inside the list like this:
    Code:
    <ul id="mnuUser"><?php echo(get_user_menu()) ?></ul>
    I'm just curios if my self invented way of doing it is "normal"

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: funciton returs 0

    It's normal, but not advisable (remember, 90% of code "out there" is crap).

    If you do that and later decide that you wish to change the structure of the HTML, you not only need to alter the page where you call get_user_menu(), but you also need to hunt through the rest of the script file(s) in order to find where the HTML is being produced so that you can change it in those places as well. This scattered design results in many out-of-context fragments of HTML, which makes comprehension and maintenance difficult and invariably results in invalid markup when you make a mistake somewhere (and you will).

    So, to avoid this mess, it is best to always keep the HTML and the presentation logic as separate as possible, and both also separate from the application logic. This separation results in what is known as the Model-View-Controller (MVC) architecture and if you search for that term you can find more articles on its use. It is best observed in complex applications, but its principles can be applied to even the simplest (non-trivial) projects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width