trouble with strings and comparison operators

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ahhreeyell
Forum Newbie
Posts: 4
Joined: Mon Sep 26, 2011 1:45 am

trouble with strings and comparison operators

Post 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>";

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: trouble with strings and comparison operators

Post 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.)
ahhreeyell
Forum Newbie
Posts: 4
Joined: Mon Sep 26, 2011 1:45 am

Re: trouble with strings and comparison operators

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: trouble with strings and comparison operators

Post by Celauran »

Didn't even realize you (and, subsequently, I) had written element == instead of $elementName ==

There's the problem.
ahhreeyell
Forum Newbie
Posts: 4
Joined: Mon Sep 26, 2011 1:45 am

Re: trouble with strings and comparison operators

Post 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>";
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: trouble with strings and comparison operators

Post 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>";

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: trouble with strings and comparison operators

Post 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>";

?>
Post Reply