Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php: Lesson #2; If...else Statements., Basics of If...Else statements.
Archimedes
post Jul 28 2008, 11:04 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 8
Joined: 22-July 08
From: Pitcairn, PA
Member No.: 31,591



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.
Go to the top of the page
 
+Quote Post
yordan
post Jul 30 2008, 03:39 PM
Post #2


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 2,238
Joined: 16-August 05
Member No.: 7,896
myCENTs:34.00



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" ?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Php:lesson #1(4)


 



- Lo-Fi Version Time is now: 1st December 2008 - 11:57 PM