Page 40 - Open Soource Technologies 304.indd
P. 40

Web Technologies-I



                   Notes         Use the is_null( ) function to test whether a value is NULL for instance, to see whether a variable
                                 has a value:
                                 if (is_null($x)) { // $x is NULL }

                                 2.3 Variables

                                 A variable is just a name assigned to reference a location in memory where some value is stored.
                                 It is a shortcut, so that you do not have to refer to memory addresses directly. Since variables are
                                 meant to be people-friendly versions of memory locations, we like to pretend that the variables
                                 in our programs actually store data. Thinking in computer language is just a little bit easier that
                                 way. But you should still know the difference.

                                 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 parser to recognize the variable
                                 as such.
                                 2.3.1 Declaring Variables

                                 PHP has no command for declaring a variable. A variable is created the moment you first assign a
                                 value to it. Setting a variable functions as its declaration. If you want to create a variable without
                                 assigning it a value, then you assign it the value of null.
                                 If you try to read the value of a non-existent variable the value also equates to null. You can test
                                 whether a variable exists with the isset() function, which returns true if a variable exists and has
                                 been assigned a value. You can also use its opposite, the empty() function, which returns true if
                                 there is no value assigned or the variable does not exist.
                                 The unset () function can also be used to set the value of a variable to null.
                                 2.3.2 Variable Variables and Variable References

                                 PHP allows you to do some neat things with variables. It allows you to create aliases for variables,
                                 and it also allows you to have variables whose name is a variable.

                                 A variable reference, or alias, is a variable assigned to refer to the same information as another
                                 variable. To assign an alias to a variable, you use the reference operator, which is an equals sign
                                 followed by an ampersand (=&).
                                 $that = ‘abc’;   // $that now equals ‘abc’
                                 $this =& $that; // $this now equals ‘abc’;
                                 $that = ‘def’;   // both now equal ‘def’;

                                 $this = ‘ghi’;   // both now equal ‘ghi’;
                                 Since they are both now references for the same location in memory, changing the value of one,
                                 changes the value of the other. Note however that if you unset () one alias, the other aliases for
                                 the same value will not be affected. This is because unset () removes the reference to the memory
                                 location for that variable, rather than changing the value stored in that memory location.

                                 You can also use references with functions to return values by reference instead of by value. To do
                                 this, you precede the name of the function with an ampersand (&) when you define the function,
                                 and then use the reference operator to assign its returned value.
                                 Variable variables are variables whose name is a variable. In other words, the name of the variable
                                 refers to a memory location that stores the name of the variable that stores the information you
                                 want.





        34                                LOVELY PROFESSIONAL UNIVERSITY
   35   36   37   38   39   40   41   42   43   44   45