PHP is a dynamic, weakly-typed language, and as such, beginners to the language will often find their code acting unpredictably because of assumptions they have made.
PHP has a fairly low entry point for new programmers - it’s easy enough to get a couple of small sample scripts up and running, but on the same token, it’s really easy to pick up bad habits and carry them through into larger projects. Since PHP is often the first language used extensively by a new generation of web programmers, and doesn’t have strict typing or error reporting, a lot of people simply never pick up the best practices for system design and implementation.
This tutorial is going to be a quick introduction to variables in PHP, and how to test them effectively for missing or incorrect data.
To define a variable in PHP, you simply refer to it and assign it a value. Notice that you don’t need to place a type before it, e.g. int or string.
$var1 = 'abc';
One variable is now present in your system, $var1, with the value abc.
If you are planning on using a group of variables in your program, it is generally good practice to declare them near the top of your function or method, or near the top of the file, and assign them sensible default values:
$counter = 0;
$name = '';
$max = 10;
Testing variables
To maintain a predictable system, you need to ensure that you are always testing variables for expected values and sanitising them where necessary. Sanitising refers to cleaning up any data that has come from an untrusted source, i.e. a user submitted form or something similar. Such data must never be used directly without being checked or parsed - a common mistake is to take data a user has submitted and immediately use it in a database query:
$query = "SELECT * FROM table WHERE name LIKE = {$_POST['name']}";
This is a classic example of an SQL injection flaw. A malicious user could easily craft a request to modify or even drop your entire database.
A better query would look like this:
$name = mysql_escape_string($_POST['name']);
$query = "SELECT * FROM table WHERE name LIKE = '$name'";
Notice the additional quotes around $name in the query; and the curly braces around the variable are no longer required as it isn’t a value being accessed in an array. Additionally, notice that the $query string is surrounded by double quotes (”) - this means that variables and string literals inside the string will be parsed.
$name = 'Michael';
$test1 = 'My name is $name';
$test2 = "My name is $name";
Try printing the variables above in your own script and see the difference. $test1 will say ‘My name is $name‘ whilst $test2 will say ‘My name is Michael‘.
PHP provides several functions to test variables with, here is a table of the results from a couple of them:
| $var is | Function | PHP returns |
|---|---|---|
| (not set) | isset | false |
| ” (empty string) | isset | true |
| 123 | isset | true |
| null | isset | false |
| ” (empty string) | empty | true |
| 123 | empty | false |
| (not set) | empty | true |
Above, we use empty to test if a variable has a value, this function will return FALSE if variable has a non-empty and non-zero value. The following are values that PHP treats as false (zero):
""i.e. empty string0as an Integer"0"as a StringNULLFALSEarray()
Also note that empty only tests variables, anything else will result in a parse error. In other words, the following will not work: empty(trim($name)); (From www.php.net/empty).
Once a variable is defined (or set), you can use unset($var) or $var = null to destroy it.
Because PHP is weakly-typed, since PHP 5 (I think, maybe sooner), an additional operator has been included. By extending the standard boolean test operator ‘==’ to 3 equal signs, ‘===’, the variables being tested will also be tested to see if their base types match. In other words:
(0 == '')
returns true because both are seen as false (0) by PHP, but
(0 === '')
returns false because 0 is an integer and ” is a string, so their values match, but not their types.
This additional checking can also be used in the negative form, i.e. ‘!==’.
Using everything we’ve discussed above, a sensible way to read from the HTTP POST parameter super-global and set a default value is to use the ternary operator in PHP:
$name = (isset($_POST['name'])) ? $_POST['name'] : 'none';
A good way to quickly play around with all these different functions, and to test your logic, is to use the PHP interactive shell (PHP 5.1 onwards):
$ php -a
Alternatively use PHP-Shell which has more features than the built-in shell (link below).
Related links
- PHP-Shell by Jan Kneschke
- http://www.php.net/manual/en/function.isset.php
- http://www.php.net/manual/en/function.empty.php
- http://www.php.net/manual/en/function.unset.php









Great tutorial bud
This is often a wonderful web site which aids me to acquire far more data to the topic. I’ll bookmark it!
Hi I love this discussion board I’m also passionated in extreme sport and methods to be far more successful…I discovered a piece technology that make me much more efficient I also like mode and fashion. Thanks again for your forum Bye KITESURF PAS CHER
your post in fact aids, today i get the identical issues, and i’ve no concept on tips on how to solve the problem. thankgod i appear yahoo and discovered your post, it helps me get rid of my trouble. thanks as soon as againjust one factor, might i paste your article on my weblog? i will add the source and credit to your website.regards!
“,, I am really thankful to this topic because it really gives great information ~,;
pWTARu http://gdjI3b7VaWpU1m0dGpvjRrcu9Fk.com
Right, what a good start however i’ll have to explore that a little bit more. Will show you exactly what more there is.
This site won’t display correctly on my blackberry - you may wanna try and fix that
real beauty page Preteen Pics
%OO Naked Preteens
=-PPP Preteens
850 Sexy Preteen Models
odruf Preteen Porn
:[ Nn Preteen
46289 Preteen Tgp
vbhvi Preteen Sex
:-[ Preteen Nude Art
bnopvs Preteen Nude
980 Naked Preteen
=OOO Preteen Bikini
72477 Preteen Nude Models
172 Top Preteen Models
psikrk Preteen Art
2147 Preteen Incest
>:[ Preteen Nudist
0886 Preteen Toplist
%-)) Nude Preteen
ysgi Nude Preteens
895 Preteen Sex Stories
mpte Nude Preteen Models
630 Preteen Nudists
=-( Nude Preteen Girls
04815 Preteen Bbs
218 Preteen Pussy
66650 Preteen Naked
4079 Preteen Nudes
=]]] Preteen Nn
>:-(( Preteen Girls
hcv
We work together Free Boy Nymphet
8924 Url Nymphet
rcgift Home Nude Nymphets
ozs Ukrain Nymphet Pics
733146 Japan Nymphets
=-D Nymphet Heaven
202 Nymphet Ring
907 Tiny Nymphetts
741743 Natural Nymphet Gallery
gjfp Hot Nymphet 14
1849
Hello! I simply want to give a huge thumbs up for the great information you’ve gotten right here on this post. I will probably be coming back to your weblog for extra soon.
Can you put it on the scales, please? lolita sex with animals kmlve
Free medical insurance 12 years lolita models 0408
Will I get travelling expenses? nude lolita pics free 1810
We’re at university together Sandra Teen Model
684 Little Girl Models
:[[[ Young Models Gallery Top
264 Pretty Young Models
>:P Glamour Models
hba Ls Models Underground
205731 Very Very Young Models
%-]]] Junior Models
=-))) Ls Nn Models Forum
36045 14 Year Old Russian Models
>:-O
A law firm Bubbles Model Preteen
%-PPP Candy Preteen Nude
=-]]] Candid Bikini Preteen
0085 Litle Nude Preteen
8-D Preteen Boy Pantyhose
wql Top Preteen Video
mimxlc Kristina Preteen Website
zdls Preteens Candy Models
gykd Fake Preteen Nudes
05400 Preteen Nymphet Pussies
00820
I saw your advert in the paper Topless Nymphet
Child Nymphets
74521 Nymphets Toplist
qkupk Nymphet Russian
615 Nymphet Art
:-[[[ Nymphet Gallery
093482 Shy Nymphets
966170 Loli Nymphet
=-))) Wild Nymphets
511988 Illegal Nymphets
ygzyua
Heya i am for the first time here. I found this board and I find It really useful & it helped me out a lot. I hope to give something back and help others like you aided me. Best wishes, free online poker
I’d like to speak to someone about a mortgage Preteen Lolita Pussy
wdsem
SEVPRNTVKG ugg soldes LDTTAUNZWR http://www.ne-ns.com
I like watching football Dark Lolita
6121
What about beautiful downtown oakland? buy Tera gold