-2

I am creating the CDT function but after performing the validation of a condition with the if I assign the data to the variable $ vector that are a string of characters that I have this error I have three modules, form, operation, functions

function CDT($montoCDT, $nombrec, $email, $time) {
    if ($montoCDT >= 1000000) {
        if ($time == 1) {
            $vector = ['Ganancia equivale a'] = "el 3.5%"; //in this line the error is marked
        } elseif ($time == 2) {
            $vector = ['Ganancia equivale a'] = "el 4.0%";
        } elseif ($time == 3) {
            $vector = ['Ganancia equivale a'] = "el 4.8%";
        }
    } else {
        echo "No se puede abrir el CDT debe ingresar un monto mayor o igual a $1.000.000";
    }
    return $vector;
}

the function should initially validate that $montoCDT is May to 1,000,000 and if so, compare if it is 1 to 3 months to get a message if it is 3 to 6 other and if it is 6 to 12 other, finally if the value is less than 1,000,000 show a message that says you can not open the CDT

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Can you indent this code properly? And what is `$vector=['Ganancia equivale a']="el 4.0%";` supposed to do? It's not reasonable. You can't assign a string to an array to a variable. – miken32 Apr 24 '19 at 20:57
  • $ vector = ['Profit equals'] = "4.0%"; If you meet the condition you must keep an index that says $ vector = ['Profit equals'] and inside a message that would be "4.0%"; I show you, it is to be able to print neatly what the vector brings. – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 21:03
  • `=` is used for assignment. `[ ]` are used for identifying arrays. You are misusing operators. https://www.php.net/manual/en/language.operators.php – miken32 Apr 24 '19 at 21:05
  • The MVCE of this would be `$vector=['Ganancia equivale a']="el 4.8%";`. What do you intend on that to be? Please don't put code in comments. – user3783243 Apr 24 '19 at 21:05
  • I do not know how to correctly bleed the code, I'm sorry I'm new in this forum – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 21:05
  • 2
    Remove the first `=` from `$vector = ['Ganancia equivale a'] = "el 3.5%";` – Dharman Apr 24 '19 at 21:06
  • only if it complies with the validation should it print in an orderly way in a table the message that I am trying to assign, Sorry for putting the code here, I just wanted you to see that the creation of the matrix is ​​for the ordered print – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 21:09
  • or tell me how you would develop the function initially should validate that $ amountCDT is> 1,000,000 and, if so, compare if it is 1 to 3 months to receive a message if it is 3 to 6 more and if it is 6 to 12 others Finally, if the value is less than 1,000,000, a message will appear saying that you can not open the CDT. – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 21:16
  • @SARAYPEDRAZACASTELLANOS the problem is not the logic, that seems right. The problem is the "double" assigning in that one line. Since we don't know what you expect/want `$vector` to be we can only guess. Dharman's guess is a very good one in my opinion. (meaning that you want `$vector` to be an array with a key "Ganancia equivale a", which would work with the `foreach($vector as $key =>$row)` you use later ) – Jeff Apr 24 '19 at 21:19
  • effectively @jeff the conjecture of Dharman was completely correct the error was generated by the = that did not belong to the code, it is excellent that you could help me with this, because even though it was a small syntax error I could not see it. Now, please explain how I can thank you for your support, I am new to this platform and I do not know how to rate or thank for your answers :) regards .. – – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 21:45
  • One way to thank would be to accept an answer that @Dharman would have to write.. (these are all comments only) Anohter way is to stay on this site and help others where you can help! – Jeff Apr 24 '19 at 21:57
  • perfect as I do to accept Dharman answer then @jeff I understand that is how to qualify the help is right? – SARAY PEDRAZA CASTELLANOS Apr 24 '19 at 22:05
  • Yes, indeed! ... – Jeff Apr 24 '19 at 22:13

1 Answers1

1

You have a syntax error because you are trying to assign a string to a constant array which you then assign to a variable. What you probably wanted to do is to assign a string to an element of an array. You can use the bracket operators:

$vector['Ganancia equivale a'] = "el 3.5%"; 

An alternative to this, seeing as you are creating a brand new array would be to return the array directly:

return ['Ganancia equivale a' => "el 3.5%"]; // this will return an array with one element

or named array:

$vector = ['Ganancia equivale a' => "el 3.5%"]; // Assing a new array with 1 element to variable $vector
return $vector; 

However, ['Ganancia equivale a'] = "el 4.8%"; is invalid syntax because left side is a constant array, not a variable.

Dharman
  • 30,962
  • 25
  • 85
  • 135