This isn't a step by step tutorial but more of an explanation on how 'if' statements work.
Here is the basic syntax of an 'if' statement. Remember that the 'else' statement is not necessary.
| <? if (something == something else) { 'then' statement } else { 'else' statement } ?> |
Usually, 'if' statements are used to compare variables. All variables in php start with a "$" sign. The 'if' statement looks at the variable you specify, and checks to see whether it is indeed the 'something else' you specify.
| if ($name == "alex") |
The 'then' statement will only run when the 'if' statement is true:
| if ($name == "alex") { print "Hello, Alex"; } |
Now, when $name == "alex", the text "Hello, Alex" will show on the page. You could leave the script like this and it would just end there. But, if you wanted to display something else if $name wasn't "alex", you would add an 'else' statement:
| <? if ($name == "alex") { print "Hello, Alex"; } else { print "Hello, person who is not named Alex."; } ?> |
Now that we added the 'else' statement to our script, if $name isn't "alex", the browser will output the text "Hello, person who is not named Alex." Above is the entire script. You can use 'if' statements to do much more with PHP, but this is just a very simple introduction to them.
Related Articles
- If/Else Statements1. If and Else statements are special pieces of code that tell your server to do something under a certain condition. If and Else statements therefore are conditional statements. An example of a likely piece of code that uses these statements is:
- Introduction to Procedural ProgrammingWell start of with if
Here is the basic usage of it
variable would be something like $variable - The Google Trap: A WarningIf you happen to use Googles proprietary meta tag: the search results pages will only display your web pages ti...
- Declaring Variables and FunctionsNow its time to get into some really fun stuff. Yes, variables and functions. Dont worry, its not as bad as it sounds.....lets start with declaring variables. Youll want to keep all of your variables in the HEAD section for now. Place the declarations between the SCRIPT tags inside the
- 10 Prominent Website MistakesNowadays, sites are getting better. With minimal design and highly maintained archives they continue to offer comprehensive services. However, prominent mistakes on several websites are still apparent.
- 10 Prominent Website MistakesNowadays, sites are getting better. With minimal design and highly maintained archives they continue to offer comprehensive services. However, prominent mistakes on several websites are still apparent. Here is a list of the website mistakes that scares visitors away and ruins the business reputation...
- The 6 Golden Rules You Should Know in Getting a Quality Web HostingAll the web hosting companies claim to be world best but how true are the statements?
- Using Multiple Files in PHP ProgrammingA detailed explanation of the methods for making and using include files in PHP, as well as passing values between included and parent files without using global variables.
- Benchmark and Optimize PHP Script SpeedSometimes you create a script that can get slow for several reasons. In this tutorial youll learn how to analyze what is slowing the script and foremost what you can do about it!
- The Basics- JavaScript TutorialTo get started with JavaScript, you will want to be able to see the tag that will set a script apart from the HTML. The tags used to begin and end a script are the tags. The opening tag should appear like this
