NORTH CHARLESTON, S.C. (AP) — Texas Gov. Rick Perry on Thursday dropped out of the race for the Republican presidential nomination and endorsed Newt Gingrich, adding a fresh layer of unpredictability to the campaign two days before the South Carolina primary.

"Newt's not perfect, but who among us is?" Perry said. He called the former House speaker a "conservative visionary" best suited to replace Barack Obama in the White House.

While the ultimate impact of Perry's decision was unclear, it reduced the number of conservative challengers to Mitt Romney. The decision also reinforced the perception that Gingrich is the candidate on the move in the final hours of the South Carolina campaign, and that front-runner Romney is struggling to hold onto his lead there.

Perry had scarcely finished speaking when Gingrich issued a statement welcoming the endorsement. "I ask the supporters of Governor Perry to look at my record of balancing the budget, cutting spending, reforming welfare, and enacting pro-growth policies to create millions of new jobs and humbly ask for their vote," Gingrich said.

Romney reacted by praising Perry for running "a campaign based upon love of country and conservative principles" and saying he "has earned a place of prominence as a leader in our party."

Perry said he decided to suspend his campaign after concluding "there is no viable path forward for me."

Spokesman Ray Sullivan said money was also a factor: "We have spent the bulk of our funds." Perry chose to drop out before Saturday's primary because he wanted to "respect" the state's voters by giving them a choice among other candidates, Sullivan said.

Perry made his decision Wednesday night and began telling staff and supporters, spokesman Ray Sullivan said. The Texas governor called Gingrich with the news Thursday morning to inform the former House speaker of his endorsement.

Sullivan wouldn't say whether Perry intended to hurt Romney but noted that Perry and Gingrich have a long-standing relationship and said Perry is enthusiastic about the possibility of a Gingrich presidency. But Perry will support the candidate who wins the Republican nomination, Sullivan said.

Perry's exit marked the end of a campaign that began with soaring expectations but quickly faded. He shot to the head of the public opinion polls when he announced his candidacy last summer, but a string of poor debate performances and campaign flubs soon led to a decline in support.

His defining moment came during one debate when he inexplicably could not recall one of three federal agencies he had pledged to abolish. He joked about it afterward, telling reporters, "I stepped in it," but never recovered from the fumble.

Also problematic for conservative supporters: Perry's support of a Texas policy to allow children of illegal immigrants to pay in-state tuition rates and his 2007 order to require schoolgirls in Texas to be vaccinated against human papillomavirus, an order later overturned by state lawmakers.

Perry also risked backlash from elderly voters after calling Social Security a fraud and a "Ponzi scheme." He said the popular federal retirement program for seniors was financially unsustainable and pledged to retool it if elected president.

Romney, the former Massachusetts governor considered the more moderate candidate in the race, has benefited thus far from having Perry and several other conservative challengers competing for the same segment of voters. New polls show Romney leading in South Carolina but Gingrich gaining steam heading into Saturday's contest in a state where conservatives hold great sway in choosing the GOP nominee.

Perry's decision to endorse Gingrich does not necessarily mean conservatives will rally behind the former House speaker. Former Pennsylvania Sen. Rick Santorum, an anti-abortion champion, is still in the race and last weekend was endorsed by a group of evangelical Christian leaders.

And there is no guarantee the Texas donors who fueled Perry's bid will shift to Gingrich, even if the governor asks them to.

Romney has been working to court them in recent weeks and has also won the backing of former President George H.W. Bush. Several Perry supporters, who spoke on condition of anonymity to avoid publicly discussing their next steps before Perry's announcement, said they have been approached by Romney's campaign and will support him as the candidate most likely candidate to face President Barack Obama in November.

Also in play are at least three influential "super" political action committees supporting Perry. One so-called super PAC, called Make Us Great Again, aired more than $3.3 million worth of ads in Iowa and South Carolina supporting Perry. A spokesman for the group did not immediately return calls from the AP seeking comment about whom the PAC will support with Perry out of the GOP race.

Perry, 61, was relatively unknown outside Texas until he succeeded George W. Bush as governor after Bush was elected president in 2000. A former Democrat, Perry had already spent about 15 years in state government when he became governor. He went on to become the state's longest-serving chief executive, winning the office three times, most recently in 2010.

Part of Perry's appeal came from his humble beginnings in tiny Paint Creek, Texas. He graduated from Texas A&M University and was a pilot in the Air Force before winning election in 1984 to the Texas House of Representatives. He switched to the GOP in 1989 and served as the state's agriculture commissioner before his election as lieutenant governor in 1998.

Perry's success as a politician suggested he would be a strong competitor to Obama. He had never lost a race in Texas, and his fight against Sen. Kay Bailey Hutchison for the Republican gubernatorial nomination in 2010 showed how tough he could be on a rival.