PHP Variables

kane

194.***.***.***
1,451 days ago

PHP Variables

Variables in PHP

All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays.

Below, the PHP script assigns the string "Hello World" to a variable called $txt:

Code:

<html>
<body>

<?php
$txt="Hello World";
echo $txt;
?>

</body>
</html>



To concatenate two or more variables together, use the dot (.) operator:

Code:

<html>
<body>

<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2 ;
?>

</body>
</html>



The output of the script above will be:

Code:

"Hello World 1234".

penguinmama

12.***.***.***
1,442 days ago
Very useful! I find I use concatenation a LOT in my scripts, for output.

phool4fool

203.***.***.***
1,417 days ago

An Introduction to Variables

What is a variable?
Good question. Think of a variable as one piece of data stored in memory, which can be called and changed or used later. Ok, you call your doctor and he tells you that your appointment is at 5:30, and you store that in your memory. He calls back in an hour, and he says "we've changed your appointment to 4:00." So you in turn grab that appointment time from memory, change it to 4:00, and put it back in memory. Let's do that in php:


PHP Example: (!)

PHP code:

<?php
//you call your doctor...and an appointment is made:
$appointment = "5:30";
//he calls back in an hour... and changes your appointment time:
$appointment = "4:00";
//one of your friends asks what time your going to the doctor, so you tell them:
echo $appointment; //this will return 4:00.
?>





That is one way to think about it, but lets go further in the explanation. There are different types of variables. The ones I will be explaining here will be string, double, and integer. These 'types' of variables are called "Scalar" variables, and are defined using the dollar sign ( $ ).

string
A string is merely a group of characters. Meaning... a string is basically a sentence. A string can be assigned with letters or numbers (or both), and can be assigned with other variables included as part of the string. Lets take a look at some examples...

PHP Example: (!)

PHP code:

<?php
$this_string = "this sentence is a string.";
//contains the value "this sentence is a string"
$thisother_string = "$this_string, that includes another variable.";
//contains the value "this sentence is a string that contains another variable."
$THIS_STRING = "12345....678910! notice I have THIS_STRING and this_string?";
?>



Notice how I used this_string and THIS_STRING as variable names? Variable names are case sensetive. Meaning that this_string, and THIS_STRING are two different variables. Take special care when naming your variables, don't name 2 variables the same thing (unless you want to change the value of that variable), and don't call a variable with the wrong name.

Integer
An 'integer' is any whole number. Here are some examples...

PHP Example: (!)

PHP code:

<?php
$forty_five = 45;
$seventy = 70;
$sixty_four = 64;
$two_plus_four = 2+4;//this variable now contains the 'Integer' '6'
?>




Do you get the idea now? Hopefully so... cause were moving on to the beloved Double.



Double
A Double is any number that contains a decimal point. You might have seen these being called "float", or "floating point" variables. Lets see some examples:

PHP Example: (!)

PHP code:

<?php
$pi = 3.14;
$another_double = 50000.09;
$and_another = 45.96;
?>




Fun, no? Of course it is!

You want more, right?
Ok, so now you know how to define variables, and you want more. You're a very needy programmer aren't you? Well, we better get started...

Lets learn how to do basic math on variables, and increment/decrement doubles...


PHP Example: (!)

PHP code:

<?php
$one = 1;
$two = 2;
$three = $one+$two;//+ is the 'addition' operator in PHP
$four = $two*$two;//* is the 'multiplication' operator in PHP
$negative_one = $one-$two;//- is the 'subtraction' operator in PHP
$one_half = $one/$two;// / is the 'division' operator in PHP
$one++; // $one becomes 2, because it was auto incremented 1 time.
$one++;//now $one becomes 3, because it was auto incremented again.
$one--;//now $one is 2 again.
$one--;//now $one is back to 1.
?>




Ok, now lets look at how to "add" (so to speak) strings... We'll use a method called 'concatenation', which uses the dot ( . ) operator.
PHP Example: (!)

PHP code:

<?php
$one = "this is";
$two = "a string that has been put together";
$three = "using concatenation.";
echo $one." ".$two." ".$three;
//would return "this is a string that has been put together using concatenation."
$thing = "woo";
$otherthing = "hoo!";
echo $thing . $otherthing;
//would return "woohoo!"
?>




Now that you have basic examples, play around-> see what you can create.

Related Functions:
doubleval, empty, floatval, get_defined_vars, get_resource_type, gettype, import_request_variables, intval, is_array, is_bool, is_callable, is_double, is_float, is_int, is_integer, is_long, is_null, is_numeric, is_object, is_real, is_resource, is_scalar, is_string, isset, print_r, serialize, settype, strval, unserialize, unset, var_dump, var_export

triumph

69.***.***.***
1,416 days ago
Do you need to clear out php variables at the end of a php script, or does it do it automatically when the script ends?

Bradd

203.***.***.***
1,412 days ago

triumph;309:

Do you need to clear out php variables at the end of a php script, or does it do it automatically when the script ends?


I think all variable in al llanguages flushes reight after end of application and this is called some garbage collection and i feel you are asking about it .is it?

penguinmama

12.***.***.***
1,398 days ago
It does it automatically, triumph. If you finish a php script, and then start another and attempt to call the same variables, they will be empty. Each variable is good for within that script (or function) only, unless you pass it to the other within the code.