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


Messages In This Thread
PHP: If Statement - by BreShiE - 12-05-2011, 09:39 AM
RE: PHP: If Statement - by BreShiE - 12-07-2011, 01:10 PM
RE: PHP: If Statement - by Peter L - 12-07-2011, 01:34 PM
RE: PHP: If Statement - by BreShiE - 12-07-2011, 03:27 PM
RE: PHP: If Statement - by Peter L - 12-07-2011, 03:56 PM
RE: PHP: If Statement - by BreShiE - 12-07-2011, 07:06 PM
RE: PHP: If Statement - by Peter L - 12-07-2011, 08:48 PM
RE: PHP: If Statement - by BreShiE - 12-07-2011, 09:04 PM
RE: PHP: If Statement - by Peter L - 12-07-2011, 09:06 PM
RE: PHP: If Statement - by BreShiE - 12-07-2011, 09:15 PM
RE: PHP: If Statement - by Strafeness - 12-11-2011, 02:34 PM
RE: PHP: If Statement - by BreShiE - 12-16-2011, 05:12 AM
RE: PHP: If Statement - by Strafeness - 12-16-2011, 02:06 PM
RE: PHP: If Statement - by HB Virus - 12-16-2011, 10:26 AM
RE: PHP: If Statement - by ๖ۣۜHacker™ - 12-16-2011, 10:54 AM
RE: PHP: If Statement - by BreShiE - 12-16-2011, 11:02 AM
RE: PHP: If Statement - by Retribute - 06-07-2012, 06:23 PM
RE: PHP: If Statement - by Haxalot - 06-21-2012, 02:43 PM
RE: PHP: If Statement - by Retribute - 06-21-2012, 04:03 PM
RE: PHP: If Statement - by joeywilliams - 04-16-2019, 09:38 AM

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 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,666 10-10-2011, 01:00 PM
Last Post: Greyersting
  Need an If/Else Statement Grizzly 5 1,369 12-10-2009, 06:43 AM
Last Post: Grizzly

Forum Jump:


Users browsing this thread: 4 Guest(s)