Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Basics Of PHP [Part 2]
MindTrix's Avatar
MindTrix
Join Date: Apr 2002
Posts: 1,833

Emcee and Nerd

United Kingdom
Show Printable Version Email this Page Subscription
MindTrix MindTrix is offline 01-07-2004, 10:00 PM

Statement's Tutorial



Introduction.



Ok, so i wrote The Basics Of PHP Tutorial a little while ago just too give people, well, the basics i guess This tutorial will help people learn just that little bit more and should help them on their way to understanding how some of the basic functions of PHP work Lets roll!!!



Why bother with Statements?



Using statements allow us to to do different tasks, depending on the result of one question from a piece of code.



The if Statement.



The if Statement is a very simple piece of code that looks like this when it is on its own



PHP Code:
if () 


Lets jump straight into an if statement piece of code, and then explain it as we go along.



Here is the code we shall use.



PHP Code:
 
<?php
 
$name 
"Liam";
 
if ( 
$name == "Liam" )
 
{
 
print 
"Yes that is correct, My name is $name";
 
}
 
?>


First we will note this symbol ==



It is known as a Comparison Operator. Its mission is simple.It checks that the LEFT is equivalent too the RIGHT, so basically both sides MUST be the same for it too continue.



Now, at the beginning we created a Variable called $name and assigned the string "Liam" too it.



Now inside the brackets of the if statement we place an expression to see what we will do next.



In the Second line we have the following code



PHP Code:
 
if ( $name == "Liam" 


So what are we doing here? Simple really, we are testing too see whether whatever is stored inside the Variable $name is exactly the same as "Liam"



After testing this we can only get Two possible answers. If what ever is stored inside $name is exactly the same as "Liam" PHP basically says TRUE.



If however the code inside $name does not match "Liam" PHP tells us FALSE.



Now then, the next code we see is

PHP Code:
 



After this tag, is where we tell PHP what too do if when we tested



PHP Code:
 
if ( $name == "Liam" 


Resulted in PHP telling us TRUE.



So basically, if PHP told us TRUE then it carrys on with the code after the
PHP Code:

symbol, however, if PHP tells us FALSE it just goes to sleep and does nothing


Now then after the { symbol we placed the code we wanted PHP too show us if $name was equal too "Liam" , in this case it was some simple text



PHP Code:
 print "Yes that is correct, My name is $name";) 


Which would simply display too us



Yes that is correct, My name is Liam



then finally we close the code with a

PHP Code:

tag.



Now, this is all fine if $name is equal too "Liam" but what if it wasnt?

Well quite simply, at the top of the code change



PHP Code:
 
$name 
"Liam";
 
Too
 
$name 
"Bob"


Now if you tried this in the Second line when it tests $name against "Liam" PHP would just turn around and say "Nope they are not the same so i will not do nothing" and simply, it wont it will sit there and be lazy and you will have a blank screen



This doesnt really help us much does it? We need more options!!! Well, say hello to Else.



The Else Clause.



The Else clause is simply an addition to the If statement to basically allow us to execute another piece of code if the first one is not shown.

Lets jump feet first in again and look at a full piece of script.

PHP Code:
<?php
 
$name 
"Liam";
 
if ( 
$name == "Liam" )
 
{
 
print 
"Yes that is correct, My name is $name";
 
}
else {
print 
"No, My name is $name";
}
?>

So lets explain, at the beginning we had the code that we had with the first if statement, but after telling PHP what code to print and closing the command with a } tag, we give PHP a second option, just incase $name did not equal "Liam" . We simply start the second option after the } tag by writting Else

We then decide PHP what too do by using the { symbol again and there we write what we want done. Come on its pretty simple to grasp you have to admit Then we close the file as normal.

Now lets change the variable $name too "Bob" so

PHP Code:
$name "Liam";
 
Is now changed too
 
$name 
"Bob"
If we run the above ELSE code, with the variable changed as shown above, First in the if() statement PHP will check too see if $name == "Liam" and as we now know, it does not, so PHP will say "Nopes you two are not the same so i will do nothing", Then it gets a shock, it says "WAIT!!! Whats this? I see a ELSE written here, Oh wow i have another option!" So it goes onto the next option and does not have to check the variable name again, as we have not asked it too, it simply runs the code shown which was

PHP Code:
print "No, My name is $name"
So, it will diplay

No, My name is Bob

I'm sure i dont need to explain in anymore detail about this code as im sure no matter how short your knowledge is, you will understand it

So, Now what i hear you cry!! Well the ELSE clause only allows us two options which is nice, but not brilliant huh

Ladys and Gentlemen, say hello to ElseIf

Using ELSE with IF


God you guys just cannot get enough can you?

Here is a Elseif piece of code.

PHP Code:
<?php
 
$name 
"Peter";
 
if ( 
$name == "Liam" )
 
{
 
print 
"Yes that is correct, My name is $name";
 
}
elseif ( 
$name == "Bob" )
{
print 
"My name is Bob";
}
else {
print 
"No, My name is $name";
}
?>
Now notice we have changed the variable $name too "Peter"

So first we have the if statement, checking $name against the name "Liam" , PHP decide they dont mach then moves onto the brand new Elseif statement.
Elseif is EXACTLY the same code layout as if. You can do a equivalance test ( $name == "Liam" ) once again, and decide code to run if it matches, Nice huh? Once again these do not match, so it moves onto the final ELSE statement, which we looked at a minute ago depending on how fast you read i guess

The Elseif ALWAYS goes after the if statement and before the ELSE statement. You can put as many Elseif statements as you wish

Sidenote : Elseif can be written as else if and elseif . They both have the same effect and the space makes no difference.


Well i believe that should be the end now

There are of course other codes similar too If // Elseif // Else statements,

They are Switch // ? operator // While // do...while // for

But that is wayyyyy too much writting for me

I hope you find this information helpfull in your quest for PHP Domination and i hope you found it enjoyable to read, as you can see i added some humour in there

Oh and one more thing, Mist, Look no mistakes HORRAY!!! (Dont you dare prove me wrong )
Reply With Quote
  #22  
Old 06-19-2006, 12:23 AM
Bubble #5 Bubble #5 is offline
 
Join Date: Apr 2005
Posts: 984
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Keep the tutes coming!

Demystifies php for non-coders :bunny:
Reply With Quote
  #23  
Old 06-19-2006, 04:25 PM
Transverse Styles Transverse Styles is offline
 
Join Date: Jul 2004
Location: Florida
Posts: 50
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You should point out that when using double quotes, PHP will attempt to evalute it as if there is a variable in there. For most of your examples you can use a single quote.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:29 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03865 seconds
  • Memory Usage 2,249KB
  • Queries Executed 19 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (13)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (3)post_thanks_box
  • (3)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit_info
  • (2)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete