PDA

View Full Version : [PHP] Tutorial 2 - Covering Variables Basics


Kuro
06-20-2008, 05:19 AM
Note: I believe my english is pretty bad, so please try and bear with it :3

Continuing Darkchild's PHP Tutorial :D (I love teaching but yet suck at it T_T)

He taught you how to make an Hello World, very advanced :) But you need more to build a good and dynamic website! (Don't get too happy.. You won't learn much here too probably, but it's a good start! =D)

As it was already seen here (http://forums.exophase.com/showthread.php?t=3085), to print any information to the page you use:


<?php
echo "Something through echo function! =D\r\n"; //\r\n it's what tells PHP to generate a new line
//or
print 'Something through print function! =D';
?>

Now! Let's imagine you've read a value from the user, since it's input, it is a variable value! That's what variables are for! To store (during runtime) a value in it!


<?php
//To declare a variable in PHP you use the $ sign and the desired variable name after it, example: $lulz
$lulz='Hi!';

//Now let's print the value stored in the variable to the page
echo $lulz;
?>

Rules in variables:

- You can't name a variable, with the name of a reserved PHP variable/array. List (http://www.phptutorial.info/?reserved.variables)
- You can't start the variable name with a number. E.g.: $1contact is an invalid name.
- You can't use spaces in a variable name. E.g.: $I like cows is an invalid name.
- You can use _ numbers and letters to name a variable but MUST start with a letter.

Examples of valid names:
$iLikeCows
$I_like_cows
$I_l1ke_c0ws

Examples of invalid names:
$1likecows
$_iLikeCows

Most programming languages require variable declaration before using them, but PHP doesn't! And also there's something called "variable types", but you need not worry about those because PHP will evaluate the value stored in it, and assign a type automatically! (Unless you e.g. convert a string value to an integer)

But although it does it for you, basic knowledge is always important!
So! The 4 most important types of variables are (examples are separated with ; ):

- Integers: 1;2;10;100;32221. //Integer values (as the name suggests)
- Floats/Reals: 1.23;69.21111;43.21 //Real values
- Strings: "LULZ!";"Yes";"No way!" //Variables with letters and numbers defined under "x" and 'x'
- Booleans: true;false;1;0 //Has only 2 possible values - True or False

Now! There's something very important, that people tend to overlook, which is the difference between " " and ' '.

For example:

<?php
$var="Yes I like cows!";

echo "Answer: $var";
//This will output -> Answer: Yes I like cows!

echo 'Answer: $var';
//This will output -> Answer: $var
?>

The difference is that " " is "smart", and will try to interpretate what is between it, and ' ' will return raw information!

Also \r\n and several other \ operands only work with " ".

Concatenation:

What is it? Concatenation is the union of 2 strings.

For example:


<?php
$var1="Yes! ";
$var2="I like cows!";

$finalVar=$var1.$var2; //This . mark will join both of those strings together, turning it into only one string!

echo $var1."\r\n"; //Will output -> Yes!
echo $var2."\r\n"; // Will output -> I like cows!
echo $finalVar; //Will output -> Yes! I like cows!
?>

----------------

And that's the basics around variables! :)

I hope this is useful for you. I'll be posting more tutorials, in time.. :D

Cheers! :D

Darkchild
06-20-2008, 05:30 AM
thanks for continuing my tutorials xD I kinda lost the patience to make more XD