October 9, 2011 learn_php

Learn Basics for PHP Part 2

Learn Basics for PHP Part 2

Welcome back to part 2 of Learn Basics for PHP. If you feel confortable with PHP, then you can keep reading this post. Otherwise, I recommend you read part one.

So before we get into web forms, we would need to remember some things about PHP variables. We’ve already talked about how they are declared, and how to assign a value to them. The few other things we need to know/rememeber are their scope. The scope of a variable can be seen as the places within the script in which a variable can be used or changed. There are a few rules for variables scopes, so let’s take a look:

Build in Superglobals These are visible throught the whole PHP script.
Constants These become visible globally as soon as they are declared. These can be used inside and outside of your script functions.
Global variables These are visible within the whole script, except inside functions. To use these inside functions, a variables needs to be declared inside the function with the exact same name as the global.
Normal Variables These can be declared inside a function. These cannot be seen from outside their functions. These do not lose their current value between function calls.
Local Variables These will only be visible in between their block of code of existance. Once their block of code terminates, they move on to a better life.

The following is a list of super globals that every PHP Guru should know about:

$GLOBALS This contains an array of all global variables.
$_SERVER This is an array of all server variables
$_GET Contains an array of variables that are passed on to the script through the GET method
$_POST Just like the GET method, POST contains an array of variables that are passed to the script through the POST method.
$_COOKIE An array that cointains:”Guess” all cookie variables.
$_FILES This is an array of variables associated to file uploads.
$_ENV Array of environment variables
$_REQUEST This is an array that contains all user inputs. This array includes $_GET, $_POST, and $_COOKIE variables.
$_SESSION Last but not least, the Session variable contains an array of all session variables.

In part one of this basic tutorial, we saw how variables were assinged values. Those were called, “String Operator” and “Assingment Operators”. Let’s take a look at what PHP calls as Combined Assignment Operators. BTW, these are known in most languages as well.

Now that we know about these specific variables, lets create a web form asking the user for some personal information. Once the user clicks on submit, we will handle the information and display it on the web.

Let’s start with the simple form:

Go ahead and save that file as userreg.php. Now, create a new file named register.php. If you notice on the form tag, we have a line that says: action = “register.php”. This means, that when the user clicks on the Submit button, the next page to execute will be register.php. This is where our logic will reside, and thus our validations as well. We will look at native PHP functions which are isset and empty. Let’s see how can we achieve our simple validation with these PHP native functions:

Here we used the $_POST method to grab the value that the user typed in. How do we know which method to use? Remember, we set the method as POST in our original form, form method=”post” . We would do the same thing for the other inputs, so I’m going to leave that task for you to do, except for the email one.

The email validation is a bit different now days. From PHP version 5.2.0, filter_var has been added, and is very effective. To use this filter, we need to call the native function from php, filter_var and send the email and the filter as parameters. Here’s how:

The filter we used if FILTER_VALIDATE_EMAIL. Let’s finish up the script for register.php to see what it looks like.

Here you have it folks. This is a very simple PHP validation method. Remeber that to have better and robust code, more emphasis should be made when making these types of validations. Some websites require for a password to be of a specific length. Others may require to add a symbol, but that all depends on everyone’s needs.

Before I go for today, there is a little hint. There is another way to grab the variables from post that do not require you as a developer to write so many lines of code. See this post on Variable variables to get an idea of how can you optimize this validation.

Hope to hear from you guys.

2 thoughts on “Learn Basics for PHP Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.