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

free web hosting
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.

 

 

 


Reply

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" ?

Reply


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*

Similar Topics

Keywords : php lesson statements basics statements

  1. Php:lesson #1 - The basics of PHP. (4)
    First off, we need to meet each other. I am Archimedes! xD I chose this name because I have a joy
    for mathematics and calculators and such. Archimedes is the man who discovered Pi(3.14159). Things
    you should know before we start Lesson #1. 1.HTML 2.General scripting knowledge or what it actually
    does. Alright, to the PHP lessons. 1.PHP stands for PHP; Hypertext Preprocessor. 2.PHP is used
    all around the world to build dynamic web pages and web sites. Basic Syntax 1. All PHP scripts
    start with the 2. To write something in PHP, we use the 'echo' or 'pr...
  2. Php: Lesson #3;functions - PHP. (0)
  3. Php Tutorial #1 - Starting off with the basics. (0)
    Many people have helped me in learning php, heck if it weren't for a certain someone(not on this
    site, on legacy-game.net) then i would'nt even be here seeing or posting any of this. Just a
    couple of things to be kept in mind, commands/statements we are learning are going to be in bold :
    CODE echo "..."; ?> and extra stuff will be in italics and will be explained in detail ------
    Now to get started with php, you would need a webhost if you want to directly do things online,
    otherwise download a localhost server: -Apache -Php -MySQL You have to get a sepe...



Looking for php, lesson, 2, statements, basics, statements,






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




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