Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. How to know if a variable has been initiated or not [modified]

How to know if a variable has been initiated or not [modified]

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phpcomtutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joan M
    wrote on last edited by
    #1

    Hello again, I'm trying to handle the language setting from the user. For that I've created a variable that I'm using in all the PHP files I have: $MyLangVar. I would like to assign a default value to that variable to i.e. english. So I've done this in my initialize file (a file that is being included by all the other files in the web site):

    <?php
    include($_SERVER['DOCUMENT_ROOT'].'/important_file_for_all.php'); // that file is not using the $MyLangVar variable.

    // first attempt.
    /*
    if (!isset($MyLangVar))
    {
    $MyLangVar= "English";
    }
    */
    // second attempt
    if (!isset($GLOBALS['MyLangVar'])
    {
    $MyLangVar = "English";
    }
    ?>

    Both attempts have not worked properly IN THE 404.PHP PAGE. At the beginning of each file I have this line: $MyLangVar = $GLOBALS['MyLangVar']; How should I do this? Thank you in advance.

    [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

    modified on Wednesday, March 30, 2011 2:16 PM

    https://www.robotecnik.com freelance robots, PLC and CNC programmer.

    N 1 Reply Last reply
    0
    • J Joan M

      Hello again, I'm trying to handle the language setting from the user. For that I've created a variable that I'm using in all the PHP files I have: $MyLangVar. I would like to assign a default value to that variable to i.e. english. So I've done this in my initialize file (a file that is being included by all the other files in the web site):

      <?php
      include($_SERVER['DOCUMENT_ROOT'].'/important_file_for_all.php'); // that file is not using the $MyLangVar variable.

      // first attempt.
      /*
      if (!isset($MyLangVar))
      {
      $MyLangVar= "English";
      }
      */
      // second attempt
      if (!isset($GLOBALS['MyLangVar'])
      {
      $MyLangVar = "English";
      }
      ?>

      Both attempts have not worked properly IN THE 404.PHP PAGE. At the beginning of each file I have this line: $MyLangVar = $GLOBALS['MyLangVar']; How should I do this? Thank you in advance.

      [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

      modified on Wednesday, March 30, 2011 2:16 PM

      N Offline
      N Offline
      nickmaroulis
      wrote on last edited by
      #2

      Where is the $MyLangVar declared? if it is inside the if statement it could be lost after the if statement is completed.

      J 1 Reply Last reply
      0
      • N nickmaroulis

        Where is the $MyLangVar declared? if it is inside the if statement it could be lost after the if statement is completed.

        J Offline
        J Offline
        Joan M
        wrote on last edited by
        #3

        Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:

        <?php
        include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
        $MyLangVar = $GLOBALS['MyLangVar'];
        ?>

        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <h

        Then, inside the "file_that_must_be_included.php" I've done this:

        <?php
        include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');

        if (!isset($MyLangVar))
        {
        $MyLangVar = "English";
        }
        ?>

        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        //defining styles + favicon...

        Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:

        [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

        https://www.robotecnik.com freelance robots, PLC and CNC programmer.

        N 2 Replies Last reply
        0
        • J Joan M

          Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:

          <?php
          include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
          $MyLangVar = $GLOBALS['MyLangVar'];
          ?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <h

          Then, inside the "file_that_must_be_included.php" I've done this:

          <?php
          include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');

          if (!isset($MyLangVar))
          {
          $MyLangVar = "English";
          }
          ?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          //defining styles + favicon...

          Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:

          [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

          N Offline
          N Offline
          nickmaroulis
          wrote on last edited by
          #4

          If the variable is already declared it will stay global. Undefined index means that element of the array globals is not set. The most useful function in php is var_dump()! It usually tells you what you need to know. I would var_dump( $GLOBALS ) and see what you get. feel free to contact me directly if you are still having dramas.

          1 Reply Last reply
          0
          • J Joan M

            Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:

            <?php
            include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
            $MyLangVar = $GLOBALS['MyLangVar'];
            ?>

            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <h

            Then, inside the "file_that_must_be_included.php" I've done this:

            <?php
            include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');

            if (!isset($MyLangVar))
            {
            $MyLangVar = "English";
            }
            ?>

            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <html>
            //defining styles + favicon...

            Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:

            [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

            N Offline
            N Offline
            nickmaroulis
            wrote on last edited by
            #5

            come to think of it $GLOBALS will not be storing the values once the pages when another page is loaded. What you want is

            J 1 Reply Last reply
            0
            • N nickmaroulis

              come to think of it $GLOBALS will not be storing the values once the pages when another page is loaded. What you want is

              J Offline
              J Offline
              Joan M
              wrote on last edited by
              #6

              You are my idol for today! :thumbsup: Thank you very much for your help! $_SESSION has made it! :-D

              [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

              https://www.robotecnik.com freelance robots, PLC and CNC programmer.

              L 1 Reply Last reply
              0
              • J Joan M

                You are my idol for today! :thumbsup: Thank you very much for your help! $_SESSION has made it! :-D

                [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Joan, I'm curious, which PHP framework do you use?

                J 1 Reply Last reply
                0
                • L Lost User

                  Joan, I'm curious, which PHP framework do you use?

                  J Offline
                  J Offline
                  Joan M
                  wrote on last edited by
                  #8

                  Now I'm using EasyPHP 5.3.5.0.

                  [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

                  https://www.robotecnik.com freelance robots, PLC and CNC programmer.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups