T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 64
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 215
  Visitor Online : 1
  Today Visit: 104
  Total Visitor : 301488
  Most Online : 41
::: Sponsored Links :::

PR 4 For This Webpage

Vinaora Logo
Home Today :

Using If Else Statements


When you were a child did you mother ever point her finger at you and say "You'd better do this or else I am gonna..."? Well, if your mom was like my mom she may have taught you something you can use with your PHP development. As funny as it sounds, relating some PHP to your real life habbits can really help you in development when you stumble into something that has you smoking cigarettes and drinking vodka when you don't smoke or drink.

An if else statement can make things really neat on your website. The basics of it are so simple that if you blink your eyes, you should be able to figure it out. Let's cover an if else statement.

Here's a basic code flow:

PHP Tutorials :

<?
if($this == $that){ 
    do 
this 
} else { 
    do 
this instead
}
?>


That's it? What just happened?
Well let's talk about it. If $this variable stood for "Dog" and $that variable stood for "Cat", this function would execute the else statement because Dog and Cat are not the same. You must have two = signs (==) to make two variables match each other. If you wanted something to be equal to or greater than you would use $this >= $that use that upside down frowning face (>=). We will cover some more basic expressions later on in this tutorial. So, let's show you one that does work.

PHP Tutorials :

<?
$this 
"dog"
$that "dog"

if (
$this == $that){ 
    echo 
"You are a Dog!"
} else { 
    echo 
"You are something besides a dog!"
}
?>


So now we see in this example that I defined $this as "dog" and $that as "dog" and I asked the if statement to ensure that both have matched each other. If they didn't the else statement would come in and say hey, You are something besides a dog!.
Hold it right there!
You should also know before we go much further that you don't have to run an if else statement with variables. You can do it like:

PHP Tutorials :

<?
if($this == dog){ 
    do 
this 
} else { 
    do 
that 
}
?>


Ok, so we've got the basics down. What if you want to make sure that more than one variable matches another variable? That's not so hard! Here, check this out.

PHP Tutorials :

<?
$this 
"dog"
$that "dog"
$the "cat"
$other "dog"

if(((
$this == $that) or ($this == $other)) && ($other == $that)){ 
    echo 
"Yep, $this is definately $that and $other is definately $that"
} else { 
    echo 
"We did not get any matches on that try."

?>


Tearing this one apart is easy. Here we go. First , let's look at our expressions outside of the if statement.

PHP Tutorials :

<?
($this == $that) or ($this == $other)) 
?>


Here we must have one of the two expressions match each other. $this has to equal $that or $this has to equal $other or this whole if statement will go to straight to the else statement. If these variables do match then we will go to the next expression: ((our first expressiion defined above)) && ($other == $that) So our first expression worked and now we are going to make sure that $other is equal to $that or we will kill the if statement and jump to the else statement.

So to break it down. If $this equals $that or $this equals $other check to see (and) if $other equals $that or go to my else statement.

Here's the same statement without the expressions above inside of it:

PHP Tutorials :

<?
if ( expressions here ){ 
    echo 
"Yep, $this is definately $that and $other is definately $other"
} else { 
       echo 
"We did not get any matches on that try."

?>


What if I don't want an else statement? The answer to that question is simple. Don't put one! Check this out:

PHP Tutorials :

<?
if($this){ 
    echo 
"this is $this"

?>


That's the lowest form of an if statement I have ever found. It's the simplest and it works great. Especially when you want to hide rows in a database for fields that were left blank. Here's how I would use it.

Let's say I pulled my database query and I got a list of variables $cow, $horse, $haybail. I would do this with my table rows that are in the array:

PHP Tutorials :

<?echo "<table>"
// Do your MySQL queries here 
if($cow){<br
    echo 
"<tr><td>$cow</td></tr>";<br

if(
$horse){<br
    echo 
"<tr><td>$horse</td></tr>";<br

if(
$haybail){<br
    echo 
"<tr><td>$haybail</td></tr>";<br

// End your MySQL array<br> 

echo "</table>"
?>


Now looking at the statements above you will see that if the variable $cow, $horse or $haybail have anything inside of them then their table rows will be echoed. If not, that entire row will not be visible. This really shaves alot of space out of your table that is not being used.

Another way of looking at things is the ! (not) expression. Here I am going to pull an SQL statement that is going to go bad and return an error. Below my statement I am going to pull an if else statement to do something if the query is good or do something else if the query is bad.

PHP Tutorials :

<?
$sql_query 
mysql_query("SELECT wrongfield FROM wrongtable WHERE wrong_item=right_item"); 
      
if(!
$sql_query){ 
    echo 
"Ok, your query did not work!"
} else { 
    echo 
"Query was sucessful! Congratulations!"

?>


Hopefully that made sense. Here's what we did. If not or error (!) $sql_query say: Ok, your query did not work! or else say: Query was sucessful! Congratulations!.

You can use some other things such as if, else, &&, or, for and etc. You should check the PHP manual for other expressions.

All Information From phpfreaks..







Other Tutorials

::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007