06-variables/index.php

Go to the documentation of this file.
00001 <?php
00014 $global1='This is global';
00015 
00016 
00022 $global2='This is global too';
00023 
00030 function main()
00031 {
00032     global $global1;
00033 
00034     $local1="String variable";
00035     $local2=5;
00036 
00037     //Trying to access global variable global1
00038     if(isset($global1))
00039         echo "A. Value of global global1 - $global1 <br/>\n";
00040 
00041 
00042     //Demonstrating that global2 is not accessible
00043     if(isset($global2))
00044         echo "B. Value of no so global global2 - $global2 <br/>\n";
00045     else
00046         echo "B. global2 is not yet set.<br/>\n";
00047 
00048 
00049     //Creating local variable with name global2
00050     $global2="Local value of global2";
00051 
00052 
00053     //Demonstrating that local global2 is now accessible
00054     if(isset($global2))
00055         echo "C. Value of no so local global2 - $global2 <br/>\n";
00056     else
00057         echo "C. Local global2 is not yet set.<br/>\n";
00058 
00059 
00060     //Show value of local variables both numeric and strings
00061     echo "D. Value of local1 is $local1 <br/>\n";
00062     echo "D. Value of local2 is $local2 <br/>\n";
00063 
00064 
00065     //Generating line breaks for cleaner output
00066     echo "<br/>\n<br/>\n";
00067 
00068 
00069     //Demonstrating that PHP arrays can use both numbers
00070     //and strings as indexes
00071     $array1[5]="Value at index 5 of array1";
00072     $array1["five"]="Value at index 'five' of array1";
00073 
00074     echo 'E. $array1[5] is ' . $array1[5] . "<br/>\n";
00075     echo 'E. $array1["five"] is ' . $array1["five"] . "<br/>\n";
00076 
00077 
00078     //Array indexes can be supplied using variables
00079     $local1=5;
00080     $local2="five";
00081     echo 'F. $array1[5] is ' . $array1[$local1] . "<br/>\n";
00082     echo 'F. $array1["five"] is ' . $array1[$local2] . "<br/>\n";
00083  
00084 
00085     //Generating line breaks for cleaner output
00086     echo "<br/>\n<br/>\n";
00087 
00088 
00089     //Showing that arrays can be nested / recursive
00090     $array2["array1"]=$array1;
00091     echo "G. Value of \$array2 is <br/>\n";
00092     //print_r can print recursive arrays without ending in infinite loop
00093     print_r($array2);
00094     echo "<br/>\n";
00095     echo "H. Use view source to see how print_r actually prints output <br/>\n";
00096     echo 'H. Value of $array2["array1"]["five"] is ' . $array2["array1"]["five"] . "<br/>\n";
00097     
00098 
00099     //Generating line breaks for cleaner output
00100     echo "<br/>\n<br/>\n";
00101 
00102 
00103     //Values of variables and \r\n etc. get substituted in double quotes ""
00104     echo "I. Value of \$local1 is $local1 <br/>\n";
00105     echo 'I. Value of $local1 is ' . $local1 . "<br/>\n";
00106 
00107 }
00108 
00109 main();
00110 
00111 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7