Skip to main content
  1. Tech
  2. Gadgets & Tech
  3. Tech Gear

Using functions in a PHP program (part 4)

See also

Using functions in a PHP program (part 4)

As mentioned in the last article, Using functions in a PHP program (part 3), we will continue with a global variable discussion and demonstration. As discussed previously in Using a variable in a PHP program, the same rules apply. Please refer to the article for a refresher.

A global variable can be accessed in any part of the program. It is essential that you explicitly declare a variable global in the function in which it is to be modified, by placing the keyword global in front of the variable that is intended to be global as shown below:

global $name;

NOTE: As demonstrated in the previous programs, we used notepad a plain text editor. The PHP programs are stored on our web server, "C:\Wampserver\www." Please note the stored web server location.

Now let us create the PHP program, please follow the steps outlined below:

  1. Select and click "Start."
  2. Select and click "All Programs."
  3. Select and click "Accessories."
  4. Select and click "Notepad."
  5. Type the PHP code exactly as shown below.



<title>Global Variable</title>


<body><h1>Global variable</h1>

<h3>Demonstrates use of global variable<h3>


function format_name()


global $name;

$first_name = "Jane";

$last_name = "Singer";

$name = $last_name.", ".$first_name;



print "$name";




  1. Once you have type the entire program, Select "File," "Save As," and then go to the webroot of your server. For example, C:\Wampserver\www. Type Filename, globalvariable.php," and then Select "Save."

In the next article of the Using functions in a PHP program (part 5); we will discuss passing values to a function.


Don't Miss

  • Crew
    'The Crew' preview: Ubisoft has on its hands the 'Destiny' of racing games
    Games Preview
  • Cell phone
    A bill legalizing unlocking phones has passed Congress and is expected to be signed into law
    Tech Buzz
  • Crew
    'The Crew' exclusive: Details on in-game servers, leveling up and online play
    Games Interview
  • Comic Con
    See 10 defining moments from the 2014 San Diego Comic-Con
    10 Photos
  • Unity Exclusive
    'Assassin's Creed Unity' exclusive: Tell the world, 'I'm coming home'
    Games Interview
  • Typing
    Is your brokerage account safe from the attacks of cyber thieves?