Ads

Meet the Basics of Variables

Basics over what variables are. First off you need to know unlike other languages you need to start PHP variables with the Dollar Sign($). PHP(dot)net has said each and every variable you make is case sensitive.

Remember Variables will always be invalid if you start them off using any numbers. To fix this change the number into the actual word or add an underscore before the number.

Some examples of PHP varibles.


CODE
<?

$variable 
"Some Content";

echo (
$variable);//would output "Some Content"

?>



There is another way to give variables a value Known as Referrences. References are basically a clone of another varible. You assign Varibles using the (&) AND symbol before the Dollar sign.

Example:

CODE
<?php

$example 
"Sup";

$copycat = &$example;

echo 
$copycat;//output would be "Sup"

?>



What that does like I said above it clones it's self into a different variable with the same value. PHP(dot)net said it's like an alias it does the same thing as the other varible.

Varibles can be assigned numbers also. When assigning varibles numbers you don't need quotes.

Example:

CODE
<?php

$example 
1;

echo 
$example;

?>



That would output "1". You can assign a boolean to a varible also. A boolean is also known as true or false. So you can assign a variable to be true or false and do checking. Look at the example.


CODE
<?php
$example 
true;//assign the variable to be true.

//lets do a if else check to see what we can do with this variable.

if($example == true){

    echo 
"$example is true";
}else{
//else

    
echo "$example is false";
}

?>



The above isn't totally necessary. The below example will show you how to use a if statement.

CODE
if(<i>expression</i>){
//do something
}


if statements usually always checks to see if your expression is true to do the same exact thing we did above we could do.


CODE
<?php

$example 
true;

if(
$example){
    echo (
"$example is true");
}else{
    echo (
"$example is false");
}
?>




The last example is showing you how to add intergers.

first you start off with 2 numbers doesn't have to be 2 numbers at all it can be 1. Example;


CODE
<?php

$number 
1;
$number $number+1;

echo 
$number;//Would output 2

?>



Example with 2 numbers


CODE
<?php
$numberone 
1;
$numbertwo 2;

$total $numberone+$numbertwo;
echo 
$total;

?>



To add we use the +(plus) sign like regular addition. and we do the same with -(minus) signs to subtract. The (.) period symbol is used to addon like + but more like adding chunks together.

Example:


CODE
<?php

$word 
"Sup guys";
$yeah "have a great time";

$output $word." Welcome to PHPDiscovery ".$yeah;
echo 
$output;//would output "Sup guys welcome to PHpDiscovery have a great time"
?>



We simply used the period to add on a chuck from another variable to complete a phrase.

Well this was one of your Decent Variable tutorials for PHP Have fun!

Next: Globals Pretty Detailed Tutorial.

If there is something I missed please fill it in instead of telling me that I forgot something Just make a note for other users to know. And if I have something incorrect please correct me. And I'll make the change if it's necessary.

Sunday May 27, 2007 - 98 reads