Skip to main content

IF Else in PHP

If/else statements are very useful because they allow your code to be expressed in certain situations but not in others. You can control when those situations occur by using control statements such as if/else.

Before we delve any further, a basic If / Else statement in PHP looks like the following:


Simply put, if a condition happens to be true, the code in pink will be executed. If the condition happens to be false, the code in orange, will be executed. The Else statement is optional though. If you do not specify an Else statement, your code will not do anything if the condition happens to be false.

For example, here is the code without an Else statement:



--------------------------------------------------------------------------------

Let's take an example involving a robot called kirupaTron that is programmed to do among many other things, cross a busy intersection.

kirupaTron is at an intersection. He is programmed to wait for the Walk light before crossing the intersection. Once kirupaTron sees the Walk light come on, he knows it must be safe to cross the intersection. The logic behind his action is controlled by an If / Else statement.

The robot checks to see IF the Walk light turns on. If the Walk light is on, the robot decides to walk. IF the walk light is off, the robot decides to stay put. Lucky for us, kirupaTron is programmed in PHP, and the code that helps him at intersection looks like the following:


The above code is a simplified form that is similar to what is displayed above. If the variable, $light is set to Walk, the condition becomes true, and therefore, the code will execute. If the $light is set to something other than Walk, the condition fails, and the code for not walking will execute.

For example, copy and paste the following code into a new PHP document:


When you preview the page containing the above code, you will see that the text for not walking is displayed. But why? The condition for the if/else statement to be true is when $light equals "Walk". Since in the previous line, we mention that $light equals "Stop", the condition fails. Therefore, the code for not walking executes.

Now, let's say you want to make the condition true. Well, in that case, replace the "Stop" with "Walk." Your first line of code should look like the following:

$light = "Walk";
Now, test your page again. You will find that the code for for walking is displayed. The condition is true. The reason the condition is true is because the variable $light does equal "Walk."

Comments

  1. The php script has a closing tag but u forgot the opening tag.

    ReplyDelete

Post a Comment

Give Your Comments

Popular posts from this blog

A KING WHO WAS POPULAL AMONG HIS PEOPLE (King Birendra)

King of Nepal Reign 31 January 1972 –1 June 2001 Predecessor Mahendra Successor Dipendra Consort Aishwarya Rajya Laxmi Devi Shah Father King Mahendra Bir Bikram Shah Dev Mother Indra Rajya Laxmi Born 28 December 1945 Kathmandu, Nepal Died 1 June 2001 (aged 55) Kathmandu, Nepal Religion Hinduism Birendra and Aishwaraya had three children. Prince Dipendra (27 June 1971 – 4 June 2001) Princess Shruti (15 October 1976 – 1 June 2001) Prince Nirajan (6 November 1977–1 June 2001) Known As: A soft-spoken man with glasses and a mustacheKing Birendra was 10th in his line to rule Nepal and considered by some to be an incarnation of the Hindu god Vishnu.On formal occasions, the king was known for stilted speeches full of jargon that did not generate much inspiration.He was more at ease and best liked for listening closely to the problems of common people, especially poor villagers in a country that is among the poorest in the world with ...

A KING WHO WAS FAMOUS (His Majesty King Mahendra)

King Mahendra  Born > 11 June 1920 Father > King Tribhuvan Bir Bikram Shah Married To >  Indra Rajya Laxmi, daughter of General Hari Shamsher Rana in 1940 . Three sons >  Birendra,  Gyanendra, and Dhirendra Three daughters >Shanti, Sharada and Shobha. After Queen Indra died in 1950. In 1952 Mahendra married Indra's sister Ratna Rajya Lakshmi Devi. Crowned > May 2, 1956 Mahendra was made a British Field Marshal in 1960. Mahendra implemented a land reform policy, which provided land to many landless people. The Mahendra Highway (also called East-West Highway) that runs along the entire Terai belt in southern Nepal was constructed during his reign. He played a key role in making Nepal a member of the United Nations. Mahendra died with a heart attack while hunting in Chitwan with Tiger Tops Hotel. It is believed  that his death was a conspiracy of CIA as John Coapman who was also proprietor of  Chitwan with Tiger Tops Hotel was ...

Act Today (Swami Chinmayananda)