phppup Posted May 28, 2019 Share Posted May 28, 2019 It's one of those days after a long weekend and a rainy morning. Simply trying to get the correct message depending on the variable's value if($a = 'one'){ echo "POOR";} if($a = 'two'){ echo "GOOD";} if($a = 'three'){ echo "VERY GOOD";} if($a = 'four'){ echo "EXCELLENT";} Not sure if I need to use ==, extra quotes, or ELSEIF for the best result. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/ Share on other sites More sharing options...
benanamen Posted May 28, 2019 Share Posted May 28, 2019 (edited) A single equals is an assignment. A double equals is a comparison. Quote The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").https://www.php.net/manual/en/language.operators.assignment.php Quote Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.https://www.php.net/manual/en/language.operators.comparison.php Edited May 28, 2019 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567121 Share on other sites More sharing options...
Barand Posted May 28, 2019 Share Posted May 28, 2019 ($a = 'one') assigns the value 'one' to $a then returns $a. ($a == 'one') returns true if $a is equal to 'one' and 'false' if it is not. Which do you think you need? As it can be only one of those values you should use if ($a == 'one') { echo 'POOR'; } elseif ($a == 'two') { echo 'GOOD'; } elseif ($a == 'three') { echo 'VERY GOOD'; } elseif ($a == 'four') { echo 'EXCELLENT'; } Alternative 1 switch ($a) { case 'one': echo 'POOR'; break; case 'two': echo 'GOOD'; break; case 'three': echo 'VERY GOOD'; break; case 'four': echo 'EXCELLENT'; break; } Alternative 2 $status = ['one' => 'POOR', 'two' => 'GOOD', 'three' => 'VERY GOOD', 'four' => 'EXCELLENT' ]; echo $status[$a]; 1 Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567122 Share on other sites More sharing options...
phppup Posted May 28, 2019 Author Share Posted May 28, 2019 (edited) Thanks for the clarifications guys and examples. Good educational for when my head clears. Edited May 28, 2019 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567123 Share on other sites More sharing options...
phppup Posted May 28, 2019 Author Share Posted May 28, 2019 An extension of my initial issue is still giving me trouble. if($a == 'one'){ $variable = "POOR";} if($a == 'two'){ $variable = "GOOD";} if($a == 'three'){ $variable = "VERY GOOD";} if($a == 'four'){ $variable = "EXCELLENT";} echo $variable //used later on If I alter the == to a single = then the 'functional result' seems to work, so I know the underlying code is valid. And with single = (as expected) the $variable value displays the TERMINOLOGY that was last replaced as the $a value (because it is re-sretting to each value). But when using == the $variable remains empty from: echo "Your score was $variable even though I inserted echo $variable in random areas and can confirm that it is carrying the value and displaying it elsewhere. Weird. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567124 Share on other sites More sharing options...
Barand Posted May 29, 2019 Share Posted May 29, 2019 7 hours ago, phppup said: But when using == the $variable remains empty from: echo "Your score was $variable What value does $a contain when this happens? Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567127 Share on other sites More sharing options...
phppup Posted May 29, 2019 Author Share Posted May 29, 2019 At that point, a is carrying the value of another variable for simplification $a = $selected_value_1; Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567133 Share on other sites More sharing options...
Barand Posted May 29, 2019 Share Posted May 29, 2019 That answers the question "where does $a come from?", however the question was "what does it contain?" If you echo $a, what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567134 Share on other sites More sharing options...
phppup Posted May 29, 2019 Author Share Posted May 29, 2019 $selected_value_1 is passed from a form as INPUT with a value of either 'one', 'two', 'three', 'four' $a = $selected_value_1; // in scripting which brings me back to if($a == 'one'){ $variable = "POOR";} if($a == 'two'){ $variable = "GOOD";} if($a == 'three'){ $variable = "VERY GOOD";} if($a == 'four'){ $variable = "EXCELLENT" ;} echo $variable //used later on Currently echo " $a "; Results either one, two, three, four (depending on which input I select). Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567138 Share on other sites More sharing options...
ginerjm Posted May 29, 2019 Share Posted May 29, 2019 Try reading this code : if($a == 'one') $variable = "POOR"; elseif($a == 'two') $variable = "GOOD"; elseif($a == 'three') $variable = "VERY GOOD"; elseif($a == 'four') $variable = "EXCELLENT" ; else $variable = 'Invalid response'; Is it a little easier to read and make sense of? You could also do a little research on the "switch" construct in the manual to find an even clearer approach to this problem. Your last question seemed to be talking about the use of "==" and "=". Be aware that the two things are complete different. One moves a value around for you and the other compares two things. It's that simple and always will be. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567139 Share on other sites More sharing options...
phppup Posted May 29, 2019 Author Share Posted May 29, 2019 ginerjm: I appreciate the input, but that still does not address the problem I am currently experiencing. Currently, with a single IF statement if($a = 'one'){ $variable = "POOR"; } //single EQUAL sign [which CHANGES the value of $a] The result of echo "Your score was $variable " //result is: Your score was POOR But if($a == 'one'){ $variable = "POOR"; } //double EQUAL sign [which COMPARES the value of $a while echo $a displays: one ] The result of echo "Your score was $variable " //result is: Your score was Thus, the $variable is not being 'grabbed' or the comparison is somehow failing. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567140 Share on other sites More sharing options...
Barand Posted May 29, 2019 Share Posted May 29, 2019 Try var_dump($a) - that will show if there are superfluous spaces in $a Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567141 Share on other sites More sharing options...
phppup Posted May 29, 2019 Author Share Posted May 29, 2019 Thanks, Barand. I now see that there are spaces ahead and behind the designated VALUE. I thought I had trimmed them, but apparently more work to be done. At least now I have a direction. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567142 Share on other sites More sharing options...
ginerjm Posted May 29, 2019 Share Posted May 29, 2019 I believe that when you say " if ($a=1)" you will get a TRUE result because the operation (assignment) was successful. Hence you will always get a value of POOR in your example. When you write a PROPER if statement your variable in this limited size sample will NOT get assigned a new value and thus end up as blank. Why are we spending so much time on this topic???? Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567143 Share on other sites More sharing options...
Strider64 Posted May 30, 2019 Share Posted May 30, 2019 Wouldn't you be better off using a 'switch' statement? switch ($a) { case "one": $variable = 'poor'; break; case "two": $variable = 'good'; break; case "three": $variable = 'very good'; break; case "four": $variable = 'excellent'; break; default: $variable = 'invalid response'; } To me it would be easier to make sense of the logic and to modify. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567151 Share on other sites More sharing options...
ginerjm Posted May 30, 2019 Share Posted May 30, 2019 That was previously pointed out by me, but I kept to the "if" logic to clear up the confusion he was having. Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567152 Share on other sites More sharing options...
Barand Posted May 30, 2019 Share Posted May 30, 2019 1 hour ago, ginerjm said: That was previously pointed out by me, Which, in turn, had been previously suggested by me. ? 1 Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567153 Share on other sites More sharing options...
ginerjm Posted May 30, 2019 Share Posted May 30, 2019 As well as the nifty use of an array! Touché! Quote Link to comment https://forums.phpfreaks.com/topic/308766-having-a-brain-block/#findComment-1567155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.