Page 1 of 1

trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 3:27 pm
by ahhreeyell
I'm writing a program that gives you the chemical symbol of each element in the periodic table. I'm making it so that anytime you type in hydrogen, boron, carbon, nitrogen, oxygen, fluorine, phosphorus, sulfur, vanadium, yttrium, iodine, or uranium, it gives you the first letter of that element, and that anytime you type in another element, it gives you the first two letters of the element. It's not working though—anybody have any ideas as to why? For some reason, it always just gives me the first letter of the element, every single time.

Thanks!

Here's the problematic code:

Code: Select all

<?php

	$elementName = $_GET[ 'element' ];
	$ucElementName = ucfirst ($elementName);
	echo $ucElementName;
	
	echo "<br><br>";

echo "<elementBox>";
echo "<br>";
	
	if(element == "hydrogen" || "boron" || "carbon" || "nitrogen" || "oxygen" || "fluorine" ||  "phosphorus" || "sulfur" || "vanadium" || "yttrium" || "iodine" || "uranium"){
		$elementSymbol = substr($ucElementName, 0, 1);
	}
	else{
		$elementSymbol = substr($ucElementName, 0, 2);
	}
		echo $elementSymbol;
echo "</elementBox>";

?>

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 3:32 pm
by Celauran
You have OR conditions that are simply strings and, thus, evaluate to TRUE.

Code: Select all

if (element == "hydrogen" || element == "boron" || element == "carbon" || etc.)

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 3:49 pm
by ahhreeyell
Yes, but then when I make that change, the first TWO characters of ALL the elements are returned. I want hydrogen, boron, and the other elements specified to return only the first character.

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 3:59 pm
by Celauran
Didn't even realize you (and, subsequently, I) had written element == instead of $elementName ==

There's the problem.

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 4:16 pm
by ahhreeyell
Right, I fixed it. Thanks.

I have another problem now. I have a set of elements from which I'm trying to remove all the vowels, THEN display the first two letters, but when I run the code, now only one letter is returned for ALL the elements.

Code: Select all

echo "<elementBox>";
echo "<br>";
	
	if($element == "hydrogen" || "boron" || "carbon" || "nitrogen" || "oxygen" || "fluorine" ||  "phosphorus" || "sulfur" || "vanadium" || "yttrium" || "iodine" || "uranium"){
		$elementSymbol = substr($ucElementName, 0, 1);
	}
	
	else if($element == "magnesium" || "manganese" || "zinc" || "rubidium" || "zirconium" || "niobium" || "technetium" || "cadmium" || "caesium" || "hafnium" || "dubnium" || "bohrium" || "hassium" || "meitnerium" || "neodymium" || "samarium" || "gadolinium" || "neptunium"){
		$vowels = array("a", "e", "i", "o", "u");
		$string = str_replace($vowels, '', $ucElementName);
		$elementSymbol = substr($string, 0, 2);
	}
	
	else{
		$elementSymbol = substr($ucElementName, 0, 2);
	}
	
		echo $elementSymbol;
echo "</elementBox>";

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 4:21 pm
by Celauran
It's the initial mistake all over again.

Code: Select all

<?php

$elementName = $_GET['element'];
$ucElementName = ucfirst($elementName);
echo $ucElementName;

echo "<br><br>";

echo "<elementBox>";
echo "<br>";

if ($elementName == "hydrogen" ||
    $elementName == "boron" ||
    $elementName == "carbon" ||
    $elementName == "nitrogen" ||
    $elementName == "oxygen" ||
    $elementName == "fluorine" ||
    $elementName == "phosphorus" ||
    $elementName == "sulfur" ||
    $elementName == "vanadium" ||
    $elementName == "yttrium" ||
    $elementName == "iodine" ||
    $elementName == "uranium")
{
    $elementSymbol = substr($ucElementName, 0, 1);
}
elseif ($elementName == "magnesium" ||
        $elementName == "manganese" ||
        $elementName == "zinc" ||
        $elementName == "rubidium" ||
        $elementName == "zirconium" ||
        $elementName == "niobium" ||
        $elementName == "technetium" ||
        $elementName == "cadmium" ||
        $elementName == "caesium" ||
        $elementName == "hafnium" ||
        $elementName == "dubnium" ||
        $elementName == "bohrium" ||
        $elementName == "hassium" ||
        $elementName == "meitnerium" ||
        $elementName == "neodymium" ||
        $elementName == "samarium" ||
        $elementName == "gadolinium" ||
        $elementName == "neptunium")
{
    $vowels = array("a", "e", "i", "o", "u");
    $string = str_replace($vowels, '', $ucElementName);
    $elementSymbol = substr($string, 0, 2);
}
else
{
    $elementSymbol = substr($ucElementName, 0, 2);
}
echo $elementSymbol;
echo "</elementBox>";

?>

Re: trouble with strings and comparison operators

Posted: Sun Oct 16, 2011 4:32 pm
by Celauran
Better still:

Code: Select all

<?php

$elementName = strtolower($_GET['element']); // In case of element=Boron etc.
$ucElementName = ucfirst($elementName);

$first_letter = array("hydrogen", "boron", "carbon", "nitrogen", "oxygen",
                      "fluorine", "phosphorus", "sulfur", "vanadium", "yttrium",
                      "iodine", "uranium");
$no_vowels = array("magnesium", "manganese", "zinc", "rubidium", "zirconium",
                   "niobium", "technetium", "cadmium", "caesium", "hafnium",
                   "dubnium", "bohrium", "hassium", "meitnerium", "neodymium",
                   "samarium", "gadolinium", "neptunium");

if (in_array($elementName, $first_letter))
{
    $elementSymbol = substr($ucElementName, 0, 1);
}
elseif (in_array($elementName, $no_vowels))
{
    $vowels = array("a", "e", "i", "o", "u");
    $string = str_replace($vowels, '', $ucElementName);
    $elementSymbol = substr($string, 0, 2);
}
else
{
    $elementSymbol = substr($ucElementName, 0, 2);
}

echo $ucElementName;
echo "<br><br>";
echo "<elementBox>";
echo "<br>";
echo $elementSymbol;
echo "</elementBox>";

?>