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

Using functions in a PHP program (part 3)

See also

In part 3 of the Using functions in a PHP program, we will discuss and demonstrate using a variable declared inside a function. When we use a variable inside a function, it is called a local function.

As shown in the localvariables PHP program, there are two variable assignments, the first variable assignment $x = “Barbara” represents the outside variable and $x= “Joey” the second variable assignment represents the inside variable.

The localvariable PHP program demonstrates that a local variable declared inside a function is only viable inside that function. Once a new local variable is declared outside the function it is replaced.

As discussed previously in Using a variable in a PHP program, the same rules apply. Please refer to the article for a refresher.

The PHP programming code listed below demonstrates displaying local variables in the same script.

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.

<html>

<head>

<title>Local Variables</title>

</head>

<body><h1>Local variables</h1>

<h3>Demonstrates use of local variables<h3>

<?php

$x = "Barbara";

function assignx () {

$x = "Joey";

print "\$x inside function is $x. </p>

";

}

assignx();

print "\$x outside of function is $x.

";

?>

</body>

</html>

  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, "localvariables.php," and then Select "Save."

In the next article of the Using functions in a PHP program (part 4); we will discuss and demonstrate using global variables.

Advertisement

Don't Miss

  • Unity
    'Assassin's Creed Unity' preview: Ubisoft comes home to its urban origins
    Games Preview
  • Kindle
    The new 'Kindle Unlimited' program could cause legal troubles for Amazon.com
    Video
    Tech Buzz
  • Destiny
    The 'Destiny' beta: 7 things we absolutely love about Bungie's new franchise
    Games Feature
  • iOS Backdoors
    iOS backdoors: Hidden items found in 600 million devices, is Apple spying on us?
    Headlines
  • Far Cry
    'Far Cry 4' exclusive: Animals, avalanches, oxygen, side content and much more
    Games Interview
  • iPhone Handle
    A young inventor thinks all iPhones should come with one of these gadgets
    Video
    Headlines