eXophase


Reply
 
Thread Tools
  #1  
Old 06-20-2008, 04:19 AM
Kuro's Avatar
Kuro Kuro is offline
#include <dumbass.h>
 
Join Date: Jun 2008
Location: Portugal
Posts: 7
Kuro is on a distinguished road
[PHP] Tutorial 2 - Covering Variables Basics

Note: I believe my english is pretty bad, so please try and bear with it :3

Continuing Darkchild's PHP Tutorial (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, to print any information to the page you use:

PHP Code:
<?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 Code:
<?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
- 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 Code:
<?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 Code:
<?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..

Cheers!

Reply With Quote
  #2  
Old 06-20-2008, 04:30 AM
Darkchild's Avatar
Darkchild Darkchild is offline
Staff & Ds Dev
 
Join Date: Apr 2007
Location: Portugal
Posts: 2,823
Darkchild is a splendid one to beholdDarkchild is a splendid one to beholdDarkchild is a splendid one to beholdDarkchild is a splendid one to beholdDarkchild is a splendid one to beholdDarkchild is a splendid one to beholdDarkchild is a splendid one to behold
Send a message via MSN to Darkchild
thanks for continuing my tutorials I kinda lost the patience to make more
__________________
Reply With Quote
Alt
Sponsor
 
 Reply

  • Submit Thread to Digg Digg
  • Submit Thread to del.icio.us del.icio.us
  • Submit Thread to StumbleUpon StumbleUpon
  • Submit Thread to Google Google
  • Bookmarks


    Thread Tools

     
    Similar Threads
    Thread Author Forum Replies Last Post
    [vb6] Tutorial 1 Darkchild Tutorial Grounds 20 11-07-2007 08:58 AM

    Forum Jump


    no new posts