Egypt and Turkey have the same basic political problem. Democracy can work despite huge ideological differences, but only if everybody is willing to be very tolerant of other people's ideas and values.
Three weeks ago, the streets of Turkish cities were full of protesters demanding the resignation of Prime Minister Recep Tayyip Erdogan, who won his third straight election in 2011. Why? Because, they say, he is shoving conservative Islamic values down their throats.
The Turkish protests have now died down, but this week the streets of Egyptian cities have been full of protesters demanding exactly the same thing for exactly the same reason. The Egyptian army has now intervened to remove the Islamist president, Mohammed Morsi, and the very survival of the new Egyptian democracy is in doubt.
Neither Erdogan nor Morsi could have come to power in a country that wasn't fully democratic. Turkey has been a partly democratic country for 60 years, but if a politician with a religious agenda won, the army would remove him. It even hanged one prime minister in 1960.
In Egypt, three generals had ruled the country in unbroken succession since the mid-1950s. Latterly, they allowed "elections", but their party always won, and the main religious party, the Muslim Brotherhood, was always banned.
The Turkish and Egyptian generals were mostly devout Muslims themselves, but they were willing to kill to keep religion out of politics. Islamic parties were a vehicle for traditional and anti-modern values, and the generals' goal was to modernize their countries so they would be strong enough to stand up to the West.
In both countries, the generals' modernizing agendas had considerable success. Turkey is now a powerful middle-income nation, and at least half its 75 million people are secular and "modern" in their political values. So they wanted the military out of politics, and finally the army withdrew -- only to see the new Justice and Development (AK) Party, a "moderate" Islamist party led by Erdogan, win the 2003 election.
The Turkish generals let the AK rule because it didn't try to impose its own religious values on the whole population. It refrained because even in its best result, in 2010, the AK only won 50.3 per cent of the vote -- and some of that support came from secular voters who saw it as the best hope for permanently excluding the army from politics.
Egypt is a much poorer, less-educated country than Turkey, but at least a third of the 85 million Egyptians would also qualify as "modern" people with secular values. They were the ones who made the revolution happen in 2010 -- but in the new democracy's first free presidential election last year, the Muslim Brotherhood's candidate, Morsi, won 51.7 per cent of the vote.
The Muslim Brotherhood promptly started writing its conservative religious values into the new constitution. More recently, Erdogan's AK Party in Turkey passed some laws that imposed its religious values too.
It wasn't a wholesale assault on the secular society -- in Turkey, they just placed some restrictions on the sale of alcohol -- but in both countries it greatly alarmed the secular part of the population. So it took only the slightest pretext -- a demonstration over the destruction of a park in Istanbul, the first anniversary of Morsi's election in Egypt -- to bring huge crowds of protesters out on the street in every city.
At that point, both Islamist leaders stopped pretending they governed in the name of the entire nation.
The Islamists are to blame for this crisis in both countries because their political program does ultimately involve shoving their values down everybody else's throats. But the secular parties are also to blame because it is their inability to unite behind a single candidate and program that has let the Islamists win power in both Turkey and Egypt.
It is hard for democracy to survive in a country where large parts of the population hold radically different ideas about the purposes of the state and the rights of its citizens. Urbanization will ultimately resolve this conflict, for in one more generation, most of the recent immigrants to the fast-growing cities will have adopted secular values. But in the meantime, Egypt will have a very rough ride. Maybe Turkey, too.
Gwynne Dyer is an independent journalist whose articles are published in 45 countries.