Jump to content

having a brain-block


phppup

Recommended Posts

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.

 

Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

($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];

 

  • Great Answer 1
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

$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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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????

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.