Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP: If Statement
#1
[Image: YzwzC.png]


Introduction:
The if statement is necessary for most programming, so it is important in PHP. Imagine that on January 1st you want to print out "Happy New Year!" at the top of your personal web page. With the use of PHP if statements you could have this process automated, months in advance, occuring every year on January 1st.

This idea of planning for future events is something you would never have had the opportunity of doing if you had just stuck with HTML.

The "Happy New Year" example would be a little difficult for you to do right now, so instead let's start off with the basics of the if statement. The PHP if statement tests to see if a value is true, and if it is, a segment of code will be executed.


The If Statement Is Like This:
PHP Code:
<?php

If (statement==something) { // The "{" is the same as saying "Then"
 // Code to be executed if true
// Same As Saying "End Statement"

?>

So what we're going to do, is make an if statement to check if someone's name is as it should be.


So first of all you're going to want to open your php tags as so:

PHP Code:
<?php

?>


Then you're going to add in a variable:
PHP Code:
<?php
$name 
"Jimmy"// The "$name" is the variable and "Jimmy" is what the variable equals to.

?>


Now we're going to make the statement check if the variable is true.
PHP Code:
<?php
$name 
"Jimmy";

If (
$name=="Jimmy") {
   echo 
"Yes, your name is Jimmy!"// echo makes the page displays what's in quotations.
}

?>

There we go! You've created an If Statement!

We can also go further and make it check if it you're not called Jimmy, and then if it's not, echo out something:
PHP Code:
<?php
/*
If Statement Tutorial By BreShiE
Enjoy This Snippit
If You Need Any Help Just Ask
*/

$name "Jimmy";

If (
$name=="Jimmy") {
   echo 
"Yes, your name is Jimmy!";
} else {
   echo 
"Your name is not Jimmy!";
}
?>

IF NOT:
Along with the If statement, you can tell the page "If NOT Then Do This".

EXAMPLE:
PHP Code:
<?php
$name 
"Jimmy";

If (!(
$name=="Jimmy")) {   
   echo 
"Your name is not Jimmy!";
} else {   
   echo 
"Yes, your name is Jimmy!";
}
?>


Next up is the "AND" and "OR" statement, you can do this by placing the actual words or by using "&&" for "AND" and "||" for "OR".

OR EXAMPLE:
PHP Code:
$name "Jimmy";
$name2 "Bob";

If (
$name=="Jimmy" || $name2=="Bob") {   
   echo 
"Yes, your name is Jimmy or Bob!!";
} else {   
   echo 
"Your name is not Jimmy or Bob!";}
?>
This will then check to see if the variables are either "Jimmy" OR "Bob".

AND EXAMPLE:
PHP Code:
$name "Jimmy";
$name2 "Bob";

If (
$name=="Jimmy" && $name2=="Bob") {   
   echo 
"Yes, your name is Jimmy and Bob!";
} else {   
   echo 
"Your name is not Jimmy and Bob!";}
?>
This will then check the variablesto see if the names are both "Jimmy" and "Bob" if not then it will echo the else statement.
Reply
#2
Added in AND, OR and IF NOT Statements.

Hope you guys enjoy this tutorial, took quite long for me to do.
Reply
#3
Nice tutorial, but you're approach to teaching people wasn't very clear. If I knew nothing about PHP and tried learning from this I'd be very confused.
Reply
#4
(12-07-2011, 01:34 PM)Laugh Wrote: Nice tutorial, but you're approach to teaching people wasn't very clear. If I knew nothing about PHP and tried learning from this I'd be very confused.

People who are that new in my opinion shouldn't be learning the If Statement anyway, it's a bit advanced for new comers.

I may make a tutorial on the very basics of PHP.
Reply
#5
Well...this is one of the first things I learned. It is very basic and does a lot..
Reply
#6
That's why I thought I'd make a tutorial for it.

When I get enough time I'll add in some examples on how to use it in forms etc.
Reply
#7
That is not what I meant at all, fix what you have before adding more.
Reply
#8
What do you mean by "Fix" it? You mean add more of a description for each section?
Reply
#9
I mean make what you already wrote easier to understand. Also make the code less sloppy.

Simply put: You made an easy feature hard and confusing.
Reply
#10
I'm not all that sure on how I can make it better, I myself am quite new to PHP.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 918 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,596 10-10-2011, 01:00 PM
Last Post: Greyersting
  Need an If/Else Statement Grizzly 5 1,336 12-10-2009, 06:43 AM
Last Post: Grizzly

Forum Jump:


Users browsing this thread: 1 Guest(s)