Jump to content


[PHP] If Else Tutorial


5 replies to this topic

#1 CyrusKafaiWu

    Advanced Member

  • Members
  • PipPipPip
  • 147 posts
  • LocationCanada

Posted 13 February 2011 - 11:13 AM

Another tutorial for PHP beginners.

Problem: We want to display certain content in a certain case (ex. certain ip to display page)

So we start off with an if statement:
<?php
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1") { //this part annouces that if the ip is 127.0.0.1 to run the code below.
echo "You are 127.0.0.1"; // just an echo which displays content.
} // end the if

Now you can see that is a starting part of an if else statement. But, what if we want to add onto that code and make it display something else for another certain ip. Lets start it off!

<?php
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1") { //this part annouces that if the ip is 127.0.0.1 to run the code below.
echo "You are 127.0.0.1"; // just an echo which displays content.
} // ends the if.
elseif($_SERVER['REMOTE_ADDR'] == "196.168.0.1") { // pratically the same thing as above but we change if to elseif.
echo "You are 196.168.0.1";
}//end that section.

Now when using an if and elseif statement in one code, you will need to always added an else. An else is practically when non of the if or elseif statements fit the criteria they ask.

Lets finish this off.

<?php
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1") { //this part annouces that if the ip is 127.0.0.1 to run the code below.
echo "You are 127.0.0.1"; // just an echo which displays content.
} // ends the if.
elseif($_SERVER['REMOTE_ADDR'] == "196.168.0.1") { // pratically the same thing as above but we change if to elseif.
echo "You are 196.168.0.1";
}//end that section.
else { //start the else.
echo "Your IP is not 127.0.0.1 or 196.168.0.1";
} // end that section
?>

Simple eh? Hopefully, that helped you in your life. If you still don't understand reply to this thread. Some people will be happy to help you.
CyrusKafaiWu <333

[url="http://cyruswu.com"]cyruskafaiwu[/url] | [url="http://toptopgames.com"]noobowned.com[/url] | [url="http://falloutarcade.com"]FalloutArcade[/url] | [url="http://themewheel.com/"]Template Finder[/url]

#2 Xstrike

    Advanced Member

  • Moderators
  • 116 posts
  • LocationIn a server

Posted 13 February 2011 - 12:59 PM

Nice one, I've never been too bright with PHP with those If and Else commands, but this makes it more easier to understand.

#3 CyrusKafaiWu

    Advanced Member

  • Members
  • PipPipPip
  • 147 posts
  • LocationCanada

Posted 13 February 2011 - 01:11 PM

@Zide glad it helped you out.
CyrusKafaiWu <333

[url="http://cyruswu.com"]cyruskafaiwu[/url] | [url="http://toptopgames.com"]noobowned.com[/url] | [url="http://falloutarcade.com"]FalloutArcade[/url] | [url="http://themewheel.com/"]Template Finder[/url]

#4 Mike Grace

    Advanced Member

  • Members
  • PipPipPip
  • 62 posts
  • LocationAustin, TX

Posted 14 February 2011 - 09:09 AM

This thread makes me think that this forum should have a code/language forum section for this type of stuff. Could be helpful since some of it could be tailored to what can /can't be run on different accounts on eleven2.

#5 CyrusKafaiWu

    Advanced Member

  • Members
  • PipPipPip
  • 147 posts
  • LocationCanada

Posted 14 February 2011 - 02:13 PM

PHP is on all Eleven2 accounts. No need to worry. But a coding section would be nice.
CyrusKafaiWu <333

[url="http://cyruswu.com"]cyruskafaiwu[/url] | [url="http://toptopgames.com"]noobowned.com[/url] | [url="http://falloutarcade.com"]FalloutArcade[/url] | [url="http://themewheel.com/"]Template Finder[/url]

#6 Andy Johnson

    Advanced Member

  • Administrators
  • 188 posts
  • LocationLondon, UK

Posted 21 December 2011 - 03:54 AM

View PostMike Grace, on 14 February 2011 - 09:09 AM, said:

This thread makes me think that this forum should have a code/language forum section for this type of stuff. Could be helpful since some of it could be tailored to what can /can't be run on different accounts on eleven2.

Mike, In the new year we will be adding more to the forum ;)
-----

Andy Johnson
Eleven2 Creative Director.

http://twitter.com/andy92





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users