Nov 22, 2009

Php: Lesson #2; If...else Statements. - Basics of If...Else statements.

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Php: Lesson #2; If...else Statements. - Basics of If...Else statements.

Archimedes
Alright, this is part #2 of my PHP Lessons. We will go into depth with more of the, scripting part of PHP.

Things you should know.
1. Basic PHP
2. Understanding of scripting.

If...Else statements
1. If...Else statements are used if you want your script/scripts to do something based on a variable.
CODE
<?php
if (condition)
code to be done if condition is true;
else
code to be done if condition is false;
?>

2. With that, you're probably a bit still confused as to what it's doing, I was too at first. Look at the next code for a better explanation. This is if there was a form prior to this page, that form, asking what is 4+5, their answer being $a.
CODE
<?php
$a = 4+5;
if($a == 9)
echo "You are correct, 4+5=9!";
else
echo "You are not correct, please go back and try again.";
?>

3. If the persons answer is 9, the page will say 'You are correct, 4+5=9!', if not, it will say 'You are not correct, please go back and try again.'
4. Now, the If...elseif...else statement.
CODE
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Mon")
  echo "Have a nice week!";
else
  echo "Have a nice day!";
?>

5. If the day is Friday, it says 'Have a nice weekend!', if the day is Monday, it will say 'Have a nice week!', and if the day is not Friday or Monday, it says 'Have a nice day!'.

That's it for If...Else statements, next lesson would be on Arrays, but I don't like Arrays so it'll be made later on, next Lesson will be on functions.

 

 

 


Comment/Reply (w/o sign-up)

yordan
QUOTE
if ($d=="Fri")

I guess that this supposes that you defined somewhere that your current language is English ? Else, if the language comes from your operating systems default, it could be french, and then the day is "Ven" and not "Fri" ?

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for php, lesson, 2, statements, basics, statements,
advertisement



Php: Lesson #2; If...else Statements. - Basics of If...Else statements.

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com