Variables in PHP


Introduction

A variable is a thing which is used to store the value or information. The value or information can be text, number, string, and array. In PHP variables are assigned with $ sign. It used before assigning variables in PHP. If you do not use a $ sign before assigning the variables then they will not work. All variables in PHP are prefixed with a dollar sign. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP variables. The syntax of declaring the variable in PHP following is:

$variable=value;

e.g.:- $name="Vineet";
         $num= 40;

Rules for Declaring Variables

  • PHP variables must start with (dollar) $ sign.
  • PHP variable name must start with a letter or an underscore "_". e.g.:- $variable or $_variable.
  • PHP variable name can only contain alpha-numeric characters and underscores.
  • PHP variable name should not contain spaces.
  • PHP variables more than one word can also be distinguished with capitalization. e.g.:-$variable Name.
  • PHP variables more than one word should be separated with underscore. e.g.:-$variable_name.

Types of variables in PHP

PHP variables can have different types which are as follows:

  • Booleans :- That takes two values TRUE or FALSE.
  • integers:- Integers are {...,-2,-1,0,1,2,...}.
  • floating point numbers:- It is also called "doubles" or "real numbers". which represent numbers with decimals.
  • strings:- Strings are set of characters.
  • arrays:- Array is also a variable but array can store more than one value or information.
  • objects:- Objects are user-defined.
  • resources:- Resource variables are created by special PHP functions.

PHP is Loosely Type Language

PHP is loosely typed language because there is no need for type casting; i.e. there is no need to declare (define) the type and name of the variable before using it. PHP automatically converts the variable to the correct data type, depending on its value. While in a strongly typed programming language you need type casting i.e. you have to declare (define) the type and name of the variable before using it.

Some examples of variables in PHP

1.  In the following example we declare a variable with the name var and the value of var is 10.

<?php
$var=10;
echo $var;
?>

Output

image1.jpg

2. If you want to add two values then you can add them. In the following example you will see how to add two values in PHP.
 
 
<?php
 
$var1=10;
 $var2=20;
 $sum=$var1+$var2;
 
echo "The sum of 10 and 20 is : ",$sum;
 
?>

Output

image8.jpg

3.  In the following example we declare a name variable and the value assigned to the name variable is "Vineet Saini". This is a string example.

<?php
$name="Vineet Saini";
echo $name;
?>

Output

image2.jpg

4. If you want to add two strings i.e. first name and last name then you will use the dot (.) operator. The Dot (.) operator is used to add two strings i.e. concatenate two strings. Which is shown in the following image.

<?php
$firstname="Vineet";
$lastname=
"Saini";
echo "The full Name is : " .$firstname." ".$lastname;
?>

Output

image4.jpg

5. If you want to find the length of your name or any string then you will use the strlen() function. For the new line you will use <br> . Such as in the following image.

<?php
$firstname="Vineet";
$lastname=
"Saini";
$fullname=$firstname.
" ".$lastname;
echo "The full Name is : " .$fullname.'<br>';
echo "The length of fullname is : ";
echo strlen($fullname);
?>

Output

image5.jpg

6. If you want to find the position of any text/character in a string then you will use the strpos() function. In the following example we find the position of "Saini" text in the fullname string. Such as in the following image.

<?php
$firstname="Vineet";
$lastname=
"Saini";
$fullname=$firstname.
" ".$lastname;
echo "The full Name is : " .$fullname.'<br>';
echo "The position of your text/character is : ";
echo strpos($fullname,"Saini");
?>

Output

image6.jpg

7. In PHP the syntax $name and $$name can be very confusing. But I tell you $name is a variable and $$name is a variable of a variable. A variable of a variable allows us to change the name of a variable dynamically.

<?php
$name="Vineet";
$Vineet=
"Saini";
echo $name.'<br>';
echo $$name;
?>

Output

image7.jpg

Conclusion

So in this article you saw, how to use of variables in PHP. Using this article you can easily understand variables in PHP. This article is very useful for PHP beginners.

Some Useful Resources


Similar Articles