[PHP]How to use 'if() and else' statements[TUT]

Status
Not open for further replies.
Nov 23, 2010
307
2
Hey peoples !!
Today i'll show you how to use if() and else statements .

How to use if() statement
EX 1:
As we know when we use date() function on PHP , it shows "DAY/MONTH/10" (10 = year). Some peoples want to show it as "DAY/MONTH/2010" , well to do that we use if() statement , watch and learn :
PHP:
<?php // start of PHP
echo date("d/m"); //shows only day and month
if(date("y") == "10") // if year is 10 (shows as 10 , means 2010)
{ // start of what does if() statement does.
echo "/2010"; // so , if year is 2010 , on date wont show 10 , it will show 2010.
} // end
//the following code will show : "DAY/MONTH/2010"
// end of PHP ?>

EX 2:
Ehm , i thought about showing "Have a nice sunday" if today is sunday :)
Watch and learn :
PHP:
<?php // start of PHP
$today_is = date("d"); // the day
if($today_is == "Sun") // if day is Sunday
{ // start of what if() statement does
echo "Have a nice sunday"; // so , if today is Sunday shows "Have a nice sunday"
} // end
// end of PHP ?>

How to use else statement
EX 1:
If year is not 2010 , and we want to show it 2011 we use else statement
Watch and learn :
PHP:
<?php // start of PHP
echo date("d/m"); //shows only day and month
if(date("y") == "10") // if year is 10 (shows as 10 , means 2010)
{ // start of what does if() statement does.
echo "/2010"; // so , if year is 2010 , on date wont show 10 , it will show 2010.
} // end
else // if year is not 2010
{ // start of what else statement will do , or show
echo "/2011"; // so , if year is not 2010 , shows /2011 :)
} // end
//the following code will show : "DAY/MONTH/2011" ONLY if year is not 2010.
// end of PHP ?>

EX 2:
If day is not sunday , it will show : "Have a nice day".
Watch and learn :
PHP:
<?php // start of PHP
$today_is = date("d"); // the day
if($today_is == "Sun") // if day is Sunday
{ // start of what if() statement does
echo "Have a nice sunday"; // so , if today is Sunday shows "Have a nice sunday"
} // end
else // if today is not sunday
{ // start of what else statement will do or show
echo "Have a nice day"; // so , if today is not sunday it will show "Have a nice day"
} // end
// end of PHP ?>

That was it peoples , hope you like it!
Rate tut ?/10

Thanks!!!!
StronGCoder.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Thanks for taking your time on making this tut, you missed something though, what if when it is Sunday I want to echo "It's a beautiful Sunday" but I also want to do the same with Monday, then I use:

PHP:
<?php
$today_is = date["d"];
if($today_is == "Sun")
{
echo "It's a beautiful Sunday";
}
elseif($today_is == "Mon")
{
echo "damn, it's Monday again ;(";
}
?>
 
Nov 23, 2010
307
2
Yeah , Kryptos , your right xD :p , anyway its
PHP:
date("d") // not date["d"]

ONT : Thanks.
 

Roper

Ancient Member
Jul 4, 2010
569
216
Heres an example for anyone who needs it.

PHP:
if($gay == "yes"){
echo "Your a gay person!";
}
else {
echo "Your not gay, GTFO";
}
 

Quackster

a devbest user says what
Aug 22, 2010
1,763
1,235
PHP:
<?php
session_start();

//Session starting
if($pass == "4dm1ncp")
{
  echo "Thanks for logging in";
  $_SESSION['admincp']=$pass;
}
else
{
  echo "Please try again :(";
}
?>
 

Livar

Now 35% cooler!
Oct 15, 2010
846
86
PHP:
<?php
session_start();
if($username == "Troll.")

{ echo "yutrollingfool?";
}

else { echo "yunotrollingfool?"; } ?>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
You can also do:

PHP:
<?php
$name = "Mark";
echo ( $name == "Mark" ) ? "Your name is Mark" : "y u n0 mark?????????";
?>

It is exactly the same as doing:
PHP:
<?php
$name = "Mark";
if ( $name == "Mark" )
{
    echo "Your name is Mark";
}
else
{
    echo "y u n0 mark?????????";
}
?>

The first example is called a ternary statement, the second example is called a standard if statement.

Hope I helped.
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
This looks like that tutorial that is on the PHP website.

EDIT:

PHP:
<?php
if ($_GET['sex'] == "yes") echo "You want sex?!";
if ($_GET['sex'] == "no") echo "You no want sex?!";
if ($_GET['you'] == "noob") header('Location:http://meatspin.com');
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top