PHP Knowledge

  • what is PHP

  • What is PHP class
  • Smarty PHP
  • PHP Calendar

  • PHP delete
  • PHP count

  • PHP Directory
  • php echo
  • php exec
  • PHP explode
  • PHP fopen

  • PHP Form
  • PHP get

  • PHP If
  • PHP list
  • php login
  • PHP LOOP
  • PHP Random
  • PHP Redirect
  • PHP replace
  • PHP session
  • PHP sort
  • PHP split
  • php substr
  • PHP Syntax
  • PHP time

  • PHP upload
  • php URL
  • PHP version
  • PHP Cookie
  • PHP Global
  • php LDAP
  • PHP list
  • php strip
  • PHP template
  • php PEAR
  • PHP md5
  • PHP ini
  • PHP FTP
  • More others

    Javascript
    CSS
    PHP

     

    PHP Global


    The PHP Global Statement

    The global statement brings an outside variable "into" a function. A variable created outside of a PHP function is not available to be used by that function. Usually you will get around this problem by passing an argument when you call the function.

    function print_name($first_name)
    {echo "Hello $first_name";}

    print_name("Susan Ann");



    Another way to make an "outside" variable available to a function is to use the PHP global statement. The global statement brings the outside variable "into" the function. Let's take a look at the code.

    $first_name = "Susan Ann";

    function print_name( )
    {global $first_name;
    echo "Hello $first_name";}

    print_name( );

    $first_name = "Susan Ann";

    This code will set the value of the $first_name variable to Susan Ann . This is not part of the function code.

    function print_name( )

    function function_name( )
    This tells the program that the following is a PHP function. print_name is the name of the function and is used to ID the function to the program. Although we are not passing any arguments to the function, we still need to follow the function name with parenthesis.

    Inside the opening { and closing } is the code for the body of the function. . .

    global $first_name;

    global $variable;
    This is the global statement that will make the value to the $first_name variable available to the function. As you can see, this statement ends with a semicolon.

    echo "Hello $first_name";

    This line of code is the echo statement that will print Hello Susan Ann to the web browser.

    print_name( );

    This is the function call for the print_name function. As you can see, there is nothing inside the parenthesis. We do not need to pass an argument to the function because we are using the global statement.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • Javascript |