

#Php if else code#
If nothing happens, or they are taken to the wrong page, the user may choose to stop using that website or company completely.ĭecisions written in code are formed using conditionals: “If x, then y.” Even a button click is a form of condition: “If this button is clicked, go to a certain page.” Conditional statements are part of the logic, decision making, or flow control of a computer program. When a person clicks the contact button on a website, they expect to be taken to a contact page. For a program to do anything useful, it must be able to respond to some sort of input. From the mundane decisions about what to wear, to the life-altering decisions of jobs and family. Introductionĭecisions are an integral part of life. Given that there is no speed difference between using braces and not using braces, it's usually down to a readability issue.The author selected Open Sourcing Mental Illness Ltd to receive a donation as part of the Write for DOnations program.

If you only have one line of code to execute, you can do without the braces entirely.

The downside of this system is that the $Age variable needs to be checked repeatedly. You could reach the same effect by having lots of if statements, however this is a slightly neater way of doing things. This means you can, for example, check whether a variable is set and whether it is set to a certain value - if the variable is not set, PHP will short-circuit the if statement and not check its value, which is good because if you check the value of an unset variable, PHP will flag an error!Ī helpful addition to if statements is the elseif statement, which allows you to chain commands together in a slightly more intelligent way. If ($Age > 10 & $Age 10) will fail, so PHP will not even bother checking it against twenty. One key thing to note is that PHP practises "if statement short-circuiting" - this is where PHP will try to do as little conditional work as possible, so it stops checking conditional statements as soon as it knows the result for sure.

This stops PHP from trying to execute both the true and false actions. Therefore 1 = 1 is true, and 1 = 2 is false.ĭid you notice that if statements use the code blocks I mentioned earlier? The code to be executed if the statement is true is in its own block (remember a block starts with ), and the code to be executed otherwise is in an else block. We will be looking at the complete list later, but first I want to mention one key check: =, or two equals signs put together. You have a vast array of ways to check statements, of which we have just looked at = (greater than or equal to). In this situation, if any of the conditions being checked in an if statement is true, the whole statement is considered true. The double ampersand, &, means that both statements must be true if the print "You're in the prime of your life\n" code is to be executed - if either one of the statements is not true for some reason, "You're not in the prime of your life" is printed out instead.Īs well as &, there is also || (the pipe symbol printed twice) which means "OR". PHP evaluates if statements left to right, meaning that it first checks whether $Age is greater or equal to 18, then checks whether $Age is less than 50. This condition can be anything you choose, and you can combine conditions together to make for actions that are more complicated. PHP allows you to choose what action to take based upon the result of a condition.
