Thursday, March 12, 2009

PHP Variables

What is variables?

A variable is a mean to store values such as strings or integers so we can easily reuse those values in our code.

Example

<?php
$str="Welcome to Php";
//here is one variable with a string value
echo $str;
//here we print the $str value onto the browser
$num = 100;
//another variable with a integer value
echo $num;
// here we echo the $num variable to print its value
echo $str;
//here we reuse the $str variable to print its value again in our code
echo $num;
//here we reuse the $num variable to print its value again in our code
?>


The following code should print "Welcome to php100
Welcome to php100". Try it.Copy the code in an empty
file,save the file as php and run it on your browser.

Variables in PHP

Every language has its own semantics of defining
variables.In PHP, we define a variable starting
with a $ sign,then write the variable name
and finally assign a value to it.


Here is an example below.

$variable_name = value;

You must add the $ sign when declaring variables,
otherwise it will not work. It is advisable that when
you define a variable, you must initilaize it by
assign it a value.


Working With PHP Variables
Few Naming Rules
There are few things we need to note when working with PHP variables.
  • A variable name should only start with underscore "_" or a letter.
  • A variable can only contain letters, numbers or underscore. In another words, you can't use other funky characters like <^# in your variables names.
Declaring a PHP Variable

Let say we want to store values using variables in PHP. Here's how we do it.
  • First, think of a logical name for a variable. For example, if you we want to store the text of your company logo in a variable, the logical name for that variable would be something like $logo_text. Remember, you can name your variables whatever you like. But you must choose logical names so it is easier to remember what they are
  • Assign that variable a value.
  • Remember to put the $ sign in front of the variable name.
Example
PHP String Variable
Example - PHP variable with a string value
<?php
$logo_text = "Welcome to php";
echo $logo_text;
?>


In the above code we define a variable name $logo_text,
assign it a string value of "
Welcome to php"and print
its value onto the page.


PHP Integer Variable

Example - PHP variable with an integer value

<?php
$number = 100;
echo $number;
?>

In the above code we define a variable name $number,
assign it an integer value of 100 and print its value
onto the page.


PHP Float or Decimal Variable

Example - PHP variable with a float or decimal value

<?php
$decimal_number = 100.01;
printf("%.2f", $decimal_number);
?>


In the above code we define a variable name
$decimal_number, assign it a decimal value of 100.01 and
print its value onto the page.

points to when declaring variables in PHP
  • 1.Remember to always put $ sign in front of variable names. That tells PHP that we're working with a variable.
  • 2.In PHP a variable name must start with a letter or an underscore followed by combination of letters, number or underscores.
  • 3.PHP variables are case sensitive, that means they could be either lower case or upper case or combination of both.
  • 4.You don't have to worry about declaring the type of the variable. For example strings and integers are declared the same way.
  • 5.It's good practice to initialize a variable i.e. assign it a value. Uninitialized variables in PHP have a value of either false, empty string or empty array.

No comments: