Declared string not recognized in the same scop

hello
im new to arduino coding and i cant get this code to work.
can anyone see why on the loop scop the string "command" isnt declared when i try to use it in the if function.

//#include <WiFi.h>
#include<ESP8266WiFi.h>
#include <WiFiUDP.h>


int WDT_millis = 0;

// WiFi ---------------------------
char *ptr = NULL;
char packetBuffer[255];
WiFiUDP udp;
#define v1 16
#define v2 2
#define v3 12
#define v4 14



void setup() {

  Serial.begin(115200);
  Serial.setTimeout(5);

  WiFi.begin("GL-MT300N-V2-b51", "goodlife");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500); Serial.print(".");


  }

  IPAddress ip(192, 168, 8, 3); // the desired IP Address
  IPAddress gateway(192, 168, 8, 1); // the router IP
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet); // configuring to the desired local IP
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); //prints my ip
  udp.begin(8888);

  
  WDT_millis = millis();
}

void loop() {
  


    int packetSize = udp.parsePacket();
    if (packetSize) {
      int len = udp.read(packetBuffer, 255);
      ptr = strtok(packetBuffer, ",");  // takes a list of delimiters


      // this is for 2 variables. repeat for the needed number.
      String command;
      command = ptr;
      Serial.println("command=");
      Serial.println(command);
      ptr = strtok(NULL, ",");  // takes a list of delimiters
      float vel = atof(ptr);
      Serial.println("vel=");
      Serial.println(vel);
      WDT_millis = millis();
    }
    
      if (command=="1") {
      analogWrite(v1,175);
      analogWrite(v2,0);
      analogWrite(v3,175);
      analogWrite(v4,0);
        WDT_millis = millis();
      }
            if (command=="0") {
      analogWrite(v1,0);
      analogWrite(v2,0);
      analogWrite(v3,0);
      analogWrite(v4,0);
        WDT_millis = millis();
      }
            if (command=="2") {
      analogWrite(v1,0);
      analogWrite(v2,175);
      analogWrite(v3,0);
      analogWrite(v4,175);
        WDT_millis = millis();
      }

  if (millis() - WDT_millis > 1000) 
  {
      analogWrite(v1,0);
      analogWrite(v2,0);
      analogWrite(v3,0);
      analogWrite(v4,0);
      
    while (Serial.available()) {
      Serial.read();
    
    WDT_millis = millis();

  }
delay(10);
}
}
    

      

  

move this above void setup() {

Welcome to the forum!

Please read any C/C++ language textbook about the "C variable scope".
The scope of a variable is the block of code where the variable is declared.

Your "command" variable is declared inside if () { } block, so it is not exist outside this block, for example, in block embraced by else { } braces

Remove String command; from the sketch and use strcmp instead:

char * token = strtok(packetBuffer, ",");

if (strcmp(token, "1") == 0)
{
  Serial.println("command = 1");
}
else if (strcmp(token, "2") == 0)
{
  Serial.println("command = 2");
}
// and so on..

posted code compiles w/o errors for me

You have declared command within this if statement scope... you can only use it within the { } related to that scope.

If you move if outside the if, for example declare it at the top of loop - then its scope will apply to anything in the loop{}.

Simplified example

void setup()
{
  {
    int x = 0;
  }

  x++;
}


void loop()
{
  
}

Why?

    while (Serial.available()) {
      Serial.read();
      WDT_millis = millis();
    }