Using PHP 'if' Statements

Using PHP 'if' StatementsThis 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 Statements
    1. 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 Programming
    Well start of with if
    Here is the basic usage of it
    variable would be something like $variable
  • The Google Trap: A Warning
    If you happen to use Googles proprietary meta tag: the search results pages will only display your web pages ti...
  • Declaring Variables and Functions
    Now 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 Mistakes
    Nowadays, 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 Mistakes
    Nowadays, 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 Hosting
    All the web hosting companies claim to be world best but how true are the statements?
  • Using Multiple Files in PHP Programming
    A 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 Speed
    Sometimes 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 Tutorial
    To 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

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.