Primary links
PHP Variables - Part I
admin — Wed, 30/08/2006 - 8:35pm
As in any Programming language, variables act as temporary place holders used to represent values used in a PHP script. There are two main types of variables in PHP : scalar and array. Scalar variables are those which can store only a single value at a time , whare as array variables are those which could have a list of values. PHP scalar variables contain values of the following types:
Integers - whole numbers or numbers without decimals.
eg : - 1, 444, 545443
Floating-point numbers (floats or doubles) - Numbers that contain decimals.
eg :- 1.34, 4.3, .34
Strings - text or numeric information. String data is always specified with quotes
eg:- "Good Morning Mumbai", "91-484-2245655"
Boolean - used for true or false values
PHP variables of all types begin with the "$" (DOLLAR) sign. Variable names can contain letters, numbers, and the (_) underscore character. Variables can never begin with numbers. In PHP variable names are case sensitive. So $name and $Name will be treated as 2 seperate variables.
Valid Variable Names
$gnews
$X_men
$label1
$str_sql
Illegal Variable Names
deamon
$3gen
$#76
PHP scalar variables are assigned values in the following format:
$username = "xmen"
$first_name = "Alex"
$Last_Name = "Chan"
The variable username contains the value xmen.
Displaying variables
The following code segment demonstrates how to declare a scalar variable, assign the scalar variable a value, and display the results in the browser window:
<?php
$str_var = "First program";
$int_var = 500;
$float_var = 2.25;
echo $str_var . "<br>" . $int_var . "<br>" . $float_var;
?>
PHP array variables can be created and assigned values using the array() construct or explicitly
A variable can be concatenated or joined together with other variables or XHTML tags using the PHP dot (.) operator. So the 3 variables will be in seperate lines as <br> gets concatenated to them.
First program
70
1.32
Variable Concatenation
The dot operator can also be used to join strings and variables:
The message - The user's name is Joe Doe - is displayed in the browser window.
The string - "The user's name is " is concatenated to the value of $fname (John) followed by a blank space - " " - and the value of $lname (Doe).
<?php
$fname = "John";
$lname = "Doe";
echo "The user's name is " . $fname . " " . $lname;
?>
The user's name is John Doe
Interpolation
PHP also supports a process known as interpolation - replacing a variable with its contents within a string. Instead of concatenating variables and literals, they can be combined inside of double quotes (""). Interpolation is a feature of double quotes only. Variables and literals cannot be combined inside of single quotes (''). With double quotes, the variable's value is displayed along with the literal. With single quotes, the variable name is "literally" displayed along with the rest of the string. The following example illustrates PHP's interpolation feature:
<?php
$fname = "John";
$lname = "Doe";
echo "The user's name is $fname $lname";
?>
This code produces the same output as the previous example. Here the variables are combined with the literal strings surrounded by double quotes. Concatenation is not required.
Formatting Currency Output
In addition to displaying standard text, a variation of the print construct, sprintf can be used to output formatted text. The statement requires a formatting string and a value to be formatted:
sprintf("%01.2f", $var) - formats and prints the value of '$var' as currency.
The sprintf statment is shown below:
<?php
$amount = 35;
$tax = 2.50 ;
$total = $amount + $tax echo "$" . $sprint("%01.2f", $total);
?>
The currency output is shown below:
$37.50
User login
Follow Us
Who's online
Who's new
- Nisha
- linnaeus
- Yameen
- TalleyReedy
- admin

