how to use variable in php

Variable Introduction -: Like other language a variable in php store the temporary data.A variable data name that may be used to store a data value only constant that remain unchanged during the execution of a program a variable may take different values at different times during execution we used several variable.

Variable store the value of money at the end of each year A variable name can be chosen by the programmer in a meaningful way so as to replace function or nature in the program some example of some names are here height, Total, weight, amount etc.

How to declare a variable in php language -: Before we use the variable it isĀ  necessary to declare a variable. In the PHP language a variable is declare using the Dollar sign ($). For example

$variable name = value;

$ x =5 ;   //here x is the variable name and 5 is the value of x

There are 2 main variable are use in the php language. These are the

1-: Single dollar sign($)- In this type simple variable are use as like

  1. string
  2. integer
  3. float

2-: Double dollar sign ($$) – In this type variable a specialĀ  reference variable stores the value of the $variable inside it.

Example of single variable -:

<?php  
   $str="hello netnic";  
   $a=100;  
   $b=22.6;  
   echo "string is: $str <br/>";  
   echo "integer is: $a <br/>";  
   echo "float is: $b <br/>";  
?>  

output of this programe

string is:hello netnic

integer is: 100

float is: 22.6

 

how to add two variable in php –: we can easily add two variable in php language. see the example here

<?php 

 $a=10; 
 $b=22; 
 $c=$a+$b;
 echo  $c ; 
?>  

Output of this program

32

Rules for variable in php –: Like any other programming language there are some rules for declaring a variable in php these are the

  • A variable name is case sensitive means color and Color are different. These are two variable.
  • A php variable start with letter or underscore. like $ x, $-netnic, etc.
  • Keyword are not use in variable name.

 

Write a program to double variable -:

<?php  
   
   $a = "netnic";  
   $$a = 500;  
   echo $a"<br/>";  
   echo $$a"<br/>";  
   echo $netnic;  

?>  

Output of this programe

netnic
500
500

Leave a Comment