Page 1 of 1

Need Help With Array and Why My Code is Not Working

Posted: Tue Nov 18, 2014 12:54 pm
by PHPHelpNeeded
I have basically just a few days reading PHP, and now I have started coding.

The rest of the code of my program is fine.

But I have created an array, which I want to access.

The function and how I intend to access the array to me seems fine, but I keep returning false, when the code is supposed to return true, when testing the value of the array.

Here is the code:

Code: Select all

<?php
           $__HTML_LANGUAGE_VERSION = array('HTML' => array("2", "3.2", "4.01", "5"), 'XHTML' => array("1.0", "1.1", "2.0"));

           function convertToTagLanguage($tagLanguage, $tagVersion){
	           if($__HTML_LANGUAGE_VERSION[$tagLanguage]!=NULL){
                             $versions = $__HTML_LANGUAGE_VERSION[$tagLanguage];
                             if(is_array($versions) == true){
                                    for($i = 0; $i < count($versions); $i++){
                                             if($versions[$i] == $tagVersion){
                                                   return true;
                                            }
                                    }
                              }
                              return false;
                     } else return false;
           }


           if(convertToTagLanguage("XHTML", "1.1") == true){
                echo "conversion was possible";
            } else {
                  echo "conversion NOT possible";
            }

?>
Can someone tell me why am I keep outputting FALSE, when the array version should match?

Re: Need Help With Array and Why My Code is Not Working

Posted: Tue Nov 18, 2014 1:17 pm
by Celauran

Code: Select all

if($__HTML_LANGUAGE_VERSION[$tagLanguage]!=NULL){
This condition fails because there is no such variable within the scope of this function.

Re: Need Help With Array and Why My Code is Not Working

Posted: Tue Nov 18, 2014 1:22 pm
by PHPHelpNeeded
Do I have to use the $_GLOBAL['VARIABLE name'] variable?

Re: Need Help With Array and Why My Code is Not Working

Posted: Tue Nov 18, 2014 1:24 pm
by Celauran
You should really try to avoid using globals. Does that array need to be available elsewhere? If not, you could define it in the method. If it does, either pass it in as an argument or consider moving your code into a class.