Home  >  Q&A  >  body text

How to declare a string variable?

The first time this variable $totpro appears in my code is like this

$totpro = $totpro + $row['profitloss'];

I want to use it to aggregate all profits, however, I get this warning message when running

Warning: Undefined variable $totpro

But if I put this code before the previous code, it works fine

$totpro = "0";

I don't like using that code to declare a function, it tried

String $totpro

But I didn’t expect it to be successful. Now tell me how to define $totpro without having to use $totpro = "0";

P粉392861047P粉392861047224 days ago436

reply all(1)I'll reply

  • P粉937769356

    P粉9377693562024-04-02 21:42:37

    If you want to sum numbers, the initial declaration should set the value to 0 (i.e. a number):

    $totpro = 0;

    You tried "0", which is a string. Technically, this works, but it's not the best approach.

    reply
    0
  • Cancelreply