Home     Wordpress     Codex

Posts Tagged ‘else’

Learning PHP Part Four – If and else

May 30th, 2009 by admin | No Comments | Filed in Learning PHP

Have you been playing around with the GET and POST arrays now?
If not, learn about foreach in PHP here.

What can I do now?
You can go ahead and use if to check the contents of variables.
Check this out.

If
The syntax for if goes like this:

if (statement) {
  Insert your code here
}

The statement will be checked, and if it is true the code within the { and } will be executed.
Take for example a script where you get a variable from the url, take the previous
example where you used $_GET['name'] to retrieve the ?name= part of the URL.

Now, if your name is John and you want the page to say hello to you but not to anyone else.


The $_GET['name'] == ‘John’ part is called the statement,
you probably understand it but it checks to see if the $_GET array variable
with the key ‘name’ contains a string ‘John’.

You can do similar tests with numbers, but then you won’t need the quotes
like the ones I have around ‘John’.

The example will then be:


Let us say you want to check if the name is NOT John.

You're not my master.

Else
Now, take the example where you output ‘Hi master’, what if you want
to print something to the ones not being the master? You can use else

You're not my master

That’s all for today, I don’t have so much time today so I am going to keep it quite short.
I will get back with a new article in these series in a couple of days, hope I’ll see you back here then!

Tags: , , ,