Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Basics Of PHP [Part 2]
MindTrix
Join Date: Apr 2002
Posts: 1,833

Emcee and Nerd

United Kingdom
by MindTrix MindTrix is offline 08 Jan 2004

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




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



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.




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



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




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



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


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



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




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



Resulted in PHP telling us TRUE.



So basically, if PHP told us TRUE then it carrys on with the code after the
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View 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




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



Which would simply display too us



Yes that is correct, My name is Liam



then finally we close the code with a


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View 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




Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



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.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

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


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

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.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

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 )

Last edited by MindTrix; 14 Jan 2004 at 05:44..
Views: 5655
Reply With Quote
Comments
  #2  
Old 08 Jan 2004, 20:56
NTLDR's Avatar
NTLDR NTLDR is offline
 
Join Date: Apr 2002
Real name: Lee Botley, BSc (Hons)
Originally Posted by MindTrix
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.
Shouldn't that be


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

(== and not =)?
__________________
Lee Botley, BSc (Hons) - vBulletin.org Moderator
http://www.thesistersthree.com/lee/sigs/signature.ts3
The Sisters Three Charmed Discussion Forums

WinterDir .:. vBindex v2.1 for vB2 .:. vBindex v3.0.0 RC5 for vB3
Reply With Quote
  #3  
Old 08 Jan 2004, 21:00
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Ok ok one error isnt that bad is it? Thanks for pointing it out though.
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #4  
Old 09 Jan 2004, 16:33
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Real name: Dean Clatworthy
I always wonder why the first thing in PHP books isn't how to correctly use quotes in PHP Strings should be single-quoted and strings containing a variable should be double quoted If you know this now it'll help you on later
__________________
Dean Clatworthy - Web Developer/Designer
Reply With Quote
  #5  
Old 09 Jan 2004, 16:38
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Mist, please read the last couple of lines of my first post, Thank you, would be appreciated
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #6  
Old 14 Jan 2004, 02:00
Marzas's Avatar
Marzas Marzas is offline
 
Join Date: Jan 2003
Hey, great tutorial, should help alot of people.

Also, there is little type in the elseif php box, it says:

elseif ( $name =="Bob" )

you forgot to space it., but i cant blame ya, thats alot of typing, shesh!

cheerz

Marzas
Reply With Quote
  #7  
Old 14 Jan 2004, 05:43
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
Thanks for the comments Marzas.
As for the error that isnt one. You do not have to put spaces like that really, i just put them there to make it look more readable.

I'll still edit the post though and put the space in, Keep it all the same huh

Cheers.
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #8  
Old 09 Jun 2004, 02:30
sketch42's Avatar
sketch42 sketch42 is offline
 
Join Date: May 2004
Real name: Zac
Originally Posted by MindTrix
Thanks for the comments Marzas.
As for the error that isnt one. You do not have to put spaces like that really, i just put them there to make it look more readable.

I'll still edit the post though and put the space in, Keep it all the same huh

Cheers.
hey will there be anymore of these .... you have a fan MindTrix
__________________
Some Hacks Installed on my vb3.0.7 Board
Quick Ban G.A.S. Sigs Once Per Page
Database Backup Pro Get New Posts
Reply With Quote
  #9  
Old 09 Jun 2004, 09:20
MindTrix's Avatar
MindTrix MindTrix is offline
 
Join Date: Apr 2002
Real name: Liam Hawkins
hmmm never really thought about doing anymore, didnt think many people were that bothered about these two
__________________
Hacks Made :
Store 3.0 Color Name Add On.
Last 5 Attachments On Forum Home
Rap Battle System -=RBS=-

Tutorials Made :
The Basics Of PHP For Beginners and The Basics Of PHP [Part 2]

Running away from that Thousand Post Mark
Reply With Quote
  #10  
Old 09 Jun 2004, 09:55
sketch42's Avatar
sketch42 sketch42 is offline
 
Join Date: May 2004
Real name: Zac
Originally Posted by MindTrix
hmmm never really thought about doing anymore, didnt think many people were that bothered about these two
i would sooo love to see this come back.. it benefits everyone.... personally i would love to know and understand how to create a hack from scratch and that aint happening as im not too familiar witha ll the different wording ... please please pleeeeeeeeeeeease....... ill be your best friend
__________________
Some Hacks Installed on my vb3.0.7 Board
Quick Ban G.A.S. Sigs Once Per Page
Database Backup Pro Get New Posts
Reply With Quote
  #11  
Old 11 Jun 2004, 10:38
zahco zahco is offline
 
Join Date: Aug 2003
thanks a lot MindTrix
i have worked with (( if )) Statement codes om my computer server and all of them working good

best regards
zahco
http://www.arabswell.com/vb/
Reply With Quote
  #12  
Old 08 Aug 2004, 16:01
xTerMn8R xTerMn8R is offline
 
Join Date: Mar 2004
Question Keep it flowing

Hey it looks a lot like general if then else statements in HTML, is the if in PHP the same as HTML?

I think the time you take to type these tutorials is a real example of true dedication to coding and sharing open code. I commend you on ur efforts and also hope these types of post will continue, as they certainly help us cut and pasters get a better feel for coding our own ...

How bout a tutorial about how to do a eval on voting on a poll. ie. Users cant login to Site till ya vote on a certain poll...

Great work and thank you for you time ...
Reply With Quote
  #13  
Old 12 Dec 2004, 17:15
Guy G Guy G is offline
 
Join Date: Nov 2004
will there be a part 3? a complex one? arrays and other stuff?
Reply With Quote
  #14  
Old 14 Dec 2004, 23:28
Michael Morris's Avatar
Michael Morris Michael Morris is offline
 
Join Date: Nov 2003
Real name: Michael Morris
Few things to keep in mind about if statements. First, and most important, is that they are the fundemental building block of all programming languages. PHP follows the formatting conventions of C++

Second, if clauses are supported in the XML templates, though their format is different


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

And Visual Basic and Javascript use yet a third format


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Master the principles of one and you should be able to apply it to the others with a little adjustment.

In the case of PHP the actions outlined between the {} brackets is carried out only if the statement evaluates to true. If it evaluates to false then the information is ignored.

PHP allows you to simply check if a variable is set. For instance


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

If $bbuser info isn't set or hasn't been declared as an array at all this returns false.

You can set a variable specifically to true or false. Such a variable is known as a Boolean variable. If a variable isn't boolean and you check it then it returns true for all values but Null and 0, which return false.

Mindtrix went over == adequately, but you also can check for greater than (>), less than (<), greater than or equal to (>=), lesser than or equal to (<=). All of these are only useful if the compared variables are numeric - I'm don't know how they'll behave if strings are compared.

Examples


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Logical Operands

In in addition to the comparison operands, you will see frequent use of logical operands: NOT, AND, OR, and XOR. These allow you to test multiple conditions. For instance


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The and operand requires both sub-statements to evaluate "true" for the overall statement to evalutate true.

AND is often shorthanded as &&

OR evaluates true if either or BOTH the conditions are true.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

If either X or Y is true, then the statement is true as a whole. If both are true the statement still holds. Only when both are false does the statement evaluate false.

OR is often shorthanded as ||

NOT reverses the value of the statement. For instance


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Note this particular example is the same as the mathmetical operand != (not equal).

Not is always shorthanded "!" in PHP. In visual basic and Javascript you have to write it out.

XOR is shor for "Exclusive Or" It excludes the And result. It is logically equivalent to this.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

XOR evaluates as true if $x or $y is true, but evaluates false if $x AND $y are true. Of course, both XOR and Or evaluate false if both inputs are false.

The final example shows exactly how convoluted if statements can get. Sub-statements can be placed in parenthesis to set them apart.

Functions and If

If a function returns true or false then it can be used in an if statement. For example, the empty function checks to see if a variable is empty (or null). You can use it for a conditional.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

One frequently used predefined function in vbulletin is is_member_of.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

If the usergroup # in $Y is among the usergroups stored in $X, it returns true. It's a little easier to see in practice...


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The admin group is predefined in vbulletin and group #6. $bbuserinfo is the current user's information. $post is another variable that carries user information while posts are assembled.

Hope this flood of tidbits help.

Last edited by Michael Morris; 14 Dec 2004 at 23:30.
Reply With Quote
  #15  
Old 19 Dec 2005, 23:32
paulomt1 paulomt1 is offline
 
Join Date: Nov 2001
Please, I need help with this:

This line of code is correct?
if (!defined (TABLE_PRX))
or must be:
if (!(defined (TABLE_PRX)))


Thank you
Reply With Quote
Reply

Similar Article
Article Author Type Replies Last Post
vBGlossary Basics SS9267547 vBulletin 3.0 Full Releases 12 27 Jun 2008 01:03



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

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


New To Site? Need Help?

All times are GMT. The time now is 04:53.

Layout Options | Width: Wide Color: