President Trump escalated his verbal fusillade against immigrants this week by announcing a foolish plan to deploy troops along the Mexican border. Such a move has at best a tenuous basis in law and none in logic, and it will burn through federal funds better spent elsewhere.

Mr. Trump has long stoked a xenophobic fear of newcomers among his political base. Ahead of the 2018 midterm elections, he seems increasingly desperate to find ways to compensate for his failure to deliver on his promise to build a “big, beautiful” border wall on Mexico’s dime.

On Sunday, Mr. Trump began a new round of confusing tweets and specious claims about undocumented immigrants. The president tweeted about “the big caravan of People from Honduras, now coming across Mexico and heading to our ‘Weak Laws’ Border,” suggesting Central American hordes were converging upon the United States to take advantage of the Deferred Action for Childhood Arrivals program, or DACA. “Must pass tough laws and build the WALL,” another tweet read. “Democrats allow open borders, drugs and crime!”

On Tuesday, he went further, declaring an urgent need for military forces on the border to confront this growing threat. He was expected to issue the order on Wednesday.

It is a president’s duty to defend the country, and putting troops on the border is not unprecedented. Yet Mr. Trump’s move is not rooted in the reality of immigration in recent years. The number of people apprehended at the border plummeted from more than 1.6 million in 2000 to 310,000 last year, the lowest level since 1971. The main causes are slowed population growth, improved economic opportunities in Mexico and stronger American border security, the result of investment in thousands of guards, physical barriers and technology.

Like so many of the president’s decisions, the one to put troops on the border seems impulsive, spiteful and politically motivated. Kirstjen Nielsen, the homeland security chief, and other aides reportedly briefed Mr. Trump on the idea last week. Even so, after his unexpected announcement on Tuesday, the White House struggled to explain his intentions, including how many and which troops would be deployed. Officials have since said that he was referring to National Guard troops, not active-duty troops. There was no consultation with the Mexican president, whose ambassador to the United States called the decision unwelcome.

Mr. Trump is feeling heat from supporters who believed his pledge to build a wall paid for by Mexico. Not only has Mexico refused to fund this folly, Congress also thwarted him by approving only $1.6 billion for the project in a recent omnibus spending bill, rather than the $25 billion Mr. Trump sought. Despite opposing the wall, Democratic leaders offered to fund it if Mr. Trump backed a path to citizenship for 1.8 million DACA-eligible immigrants. He refused.

One impediment to using troops on the border could be American law. The 1878 Posse Comitatus Act bars the use of the armed forces for civilian law enforcement tasks unless authorized by the Constitution or Congress. Since the 1980s, however, the Defense Department, including the National Guard, has provided indirect support to border-related anti-drug and counterterrorism efforts.

In 2010, President Barack Obama sent 1,200 National Guard troops to the border to support operations against illicit networks trafficking in people, drugs, illegal weapons and money until stronger border protections and more guards were put in place. In 2006, President George W. Bush responded to requests from the governors of Arizona, California, New Mexico and Texas in deploying 6,000 National Guard troops to help with engineering, aviation, entry identification and other support. The Pentagon has often been reluctant to engage in such activities, which detract from its core war-fighting mission. With combat operations underway in Afghanistan, Syria, Iraq and elsewhere, it may be even less eager to do so now, especially if the operation is not limited in scope and duration.

Border operations often lead to struggles among the Pentagon, Congress and states over who should bear the cost. That tension could prove even more acute now, especially if lawmakers and governors object to the deployment.

Spending billions of dollars for extraneous operations doesn’t seem to concern Mr. Trump, who appears indifferent to the ballooning federal deficit; he also suggested, inappropriately, that the Pentagon could pay for the wall. All of which undercuts his previous claims that the Pentagon was underfunded and needed a huge budget increase in 2019, to $716 billion, which is already earmarked to give troops a raise and buy new weapons.

Mr. Trump is once again demonstrating an inability to make sound, considered judgments about what’s needed to lead the nation and keep it safe. He is resorting to the demagogue’s tactic of inspiring fear and appears not to understand why the Posse Comitatus Act was enacted — to limit the powers of the federal government in using military personnel to enforce domestic policies within the United States. He should recognize that the move is costly and should be used sparingly, if at all — and certainly not for crass political purposes.