Jump to content
phppup

having a brain-block

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.

 

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Posted (edited)

Thanks for the clarifications guys and examples.

Good educational for when my head clears.

Edited by phppup

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

At that point, a is carrying the value of another variable for simplification

$a = $selected_value_1;

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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).

Share this post


Link to post
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.

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Try var_dump($a) - that will show if there are superfluous spaces in $a

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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????

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

That was previously pointed out by me, but I kept to the "if" logic to clear up the confusion he was having.

Share this post


Link to post
Share on other sites
1 hour ago, ginerjm said:

That was previously pointed out by me,

Which, in turn, had been previously suggested by me. ?

  • Haha 1

Share this post


Link to post
Share on other sites

As well as the nifty use of an array!  Touché!

Share this post


Link to post
Share on other sites

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.