A gunman holed up for 7½ hours in a Philadelphia rowhouse, firing on police and wounding six in a dramatic standoff that trapped two officers inside all while the commissioner and his attorney tried to negotiate his surrender.

Philadelphia Police Commissioner Richard Ross expressed amazement that the standoff, which began Wednesday when officers attempted to serve a warrant, ended with no one dead. It “could have been far worse,” he said Thursday.

“This was a very dynamic situation, one that I hope we never see again,” he said outside the Philadelphia Police Department.

The gunman came out of the home after police used tear gas in the building. He was taken to a hospital for evaluation and then placed into custody.

As officers flooded the scene, the situation grew chaotic at times. Police took to Twitter to ask media helicopters to pull back in the beginning of the standoff, saying they feared the gunman might be able to see police positions in the footage. Some residents shouted and threw things at officers.

A nearby day care center was locked down for hours and later evacuated, with police officers helping carry babies and two city buses set up where shaken children waited for their parents to pick them up.

The suspect was identified by his lawyer as Maurice Hill, 36. Pennsylvania prison officials said a man with the same name and date of birth served about 2½ years on drug charges and was paroled in 2006, and served more than a year for aggravated assault and before being released in 2013.

State court online court records indicate that man had multiple arrests in Philadelphia and adjacent Delaware County between 2001 and 2012, producing convictions that include perjury, fleeing and eluding, escape and weapons offenses.

Hill’s lawyer, Shaka Johnson, said Hill called him during the standoff asking for help surrendering.

Hill told him he wanted to make it out alive to see his newborn daughter and teenage son again.

Johnson told WPVI-TV he arranged for he and Hill to speak with Philadelphia District Attorney Larry Krasner and the police commissioner by phone.

President Donald Trump weighed in on the shootout Thursday morning, saying the gunman “should never have been allowed to be on the streets.”

“He had a long and very dangerous criminal record,” he wrote in the tweet. “Looked like he was having a good time after his capture, and after wounding so many police. Long sentence — must get much tougher on street crime!”

The standoff started around 4:30 p.m. as officers went to a home in a north Philadelphia neighborhood of brick and stone rowhomes to serve a narcotics warrant in an operation “that went awry almost immediately,” Ross said.

Many officers “had to escape through windows and doors to get (away) from a barrage of bullets,” Ross said.

The six officers who were struck by gunfire have been released from hospitals, said Philadelphia police Sgt. Eric Gripp.

Two other officers trapped inside the house for about five hours after the shooting broke out were freed by a SWAT team well after dark fell.

Ross said the reason he made the unusual decision to be the person negotiating with Hill was because he was “so worried” about his officers stuck inside.

“I wouldn’t be able to live with myself if I was 200 feet away,” he said Thursday.

Three people who officers had taken into custody in the house before the shooting started were also safely evacuated, police said.

At one point, authorities held a news conference in hopes that the gunman or someone he was communicating with may hear them. Ross said he was “very intentional and deliberate” with the words he used during the briefing since he was not sure if Hill knew there were two officers trapped upstairs.

Police tried to push crowds of onlookers and residents back from the scene. In police radio broadcasts, officers could be heard calling for backup as reports of officers getting shot poured in.

“There was just a lot of screaming and chaos,” said Abdul Rahman Muhammad, 21, an off-duty medic.

Dozens of officers on foot lined the streets. Others were in cars and some on horses.

Trump and Attorney General William Barr were briefed on the shooting, officials said.

Philadelphia Mayor Jim Kenney said he was thankful that officers’ injuries weren’t life-threatening.

“I’m a little angry about someone having all that weaponry and all that firepower, but we’ll get to that another day,” Kenney said.