Php variable

 


what is variable
   the main way to store information in the middle in the middle of a php program of a php program is by using variable a way to name hang on to any the that you want to use later.
       here are the most important thing to know about variable in php (more detailed explanation will follow)
       
        all variable in php are denoted with a leading dollar sing $
      
        the value of a variable is the value of its most recent assignment
 
         variable are assigned with the= operator with variable on the left - hand side and the expression to  be evaluated on the right
    
         variable can but do not need to be declared before assignment 

          variable have no intrinsic type other than the type of their current value

          variable used before they are assignment have default value.


php variable are perl-link

          all variable in php start with a leading $ sign just like scalar variable in the perl scripting lan- guage and in other ways have smiler behavior  after the initial $ variable names be composed of letter digit and underscore characters furthermore first character after may not be number

derclaring variable 
      
          this subheading is here simply because programmers from some other languages might  be looking for it in language such as c c++  and java the programmer must declare must the name and type of any variable  before making use of it however php because types are associated with value rather then variable no such declaration is necessary the first step in using variable is to assign it a value.

assigning variables
       
          variable assignment is simple -just write the variable name. and add a single equal (=);then add the expression that you want to assign to that variable:
          $pi =3+0.14159:// approximately
note that what is assigned is the result of evaluating the expression not the expression itself after the preceding statement is evaluated there is no way to tell that value of $ pi was created by adding two number together.

         its conceivable that you will want too actually the preceding math expression rather than evaluate it you force php to treat a mathematical variable assignment  as a string by quoting the expression: 
              $pi="3+0.14159";

reassigning    variable 
           there is no interesting distinction in php between assigning a variable for the first time and changing its value later this true even if the assigned value are of different type for example the following is perfectly legal

          $my_num_var ="this should be a number = hope its ressigned";
           $my_num_var =5:

unassigned variable
         many programming languages will object if you try to use a variable before it is assigned others will let use it but if you many find yourself reading the random contents area of memory in php the deafult error - reporting setting allows you to use unassigned variable without errors php ensures they have reasonable defaulf\t value
  
default value
         variable in php do not have intrinsic types a variable does not know in advance whether it will be used to stro a number or a string of character 
 
      explain php variables
                   variable are used for storing values like text string number  or array when a variable is declared it can be over agin your script all variables in php strat with  $ sign symbol.

syntax:-$vaR-NAME=VALUE;
EXMAPLE :- <?PHP
                            $TXT="HELLO WORLD";
                             $X=16;
                         ?>

GLOBAL V/S LOCAL VARIABLE

     if a variable declare in a function that variable know as local variable
      for global variable the value of global variable is same during the execution of program
    with the help of global keyword we can memtion variable as global 

<?php
          globaL $myname;
           myage();
          mysalary();
          function myage()
{
          $age =10;//local variable for function 
         $myname="vaiwala";
         echo"<br> my name is $myname and my salary is $salary";
}
?>

static variable
         for static variable the value of static variable is same during the execution of program with the help of static keyword we can mention variable static
          by default function is not take a spse in memory but with help of we can reserve our in memory

<?php
         static $myname="vimal";
          $myname="vaiwala";
         my age ();
         mysalary(0;
        function myage()
       {
               $age=10;
               $myname="vishal";
               echo "my name is $my name and my age is$age;

            }
                      function mysalary()
                {
                        $salary=1000;
                           $myname="ywala";
                           echo"<br>my name is $myname and my salary is $salary";
}
?>

comment in php 
      1)single line comment  :_it is use to give comment only sinle line with the help of / (double slash) and #(hash sign)
ex:- // my name is vimal vaiwala  or #my name is vimal vaiwala

2} multi line comment :- it is use give a comment more then line with help of ?**?
ex/*my name is vimal vaiwala
and always believe in destiny*//

Post a Comment

0 Comments