Jump to content

the global


runeveryday

Recommended Posts

why not use global? if you want to use a variable which declare out of a function,but how to do it?

 

Pass it to the function via the function's argument list?

 

Example:

 

function priceWithTax($price, $salesTax)
{
   return $price + ($price * $salesTax);
}

$price = 5.00;
$tax = 0.08;

$finalPrice = priceWithTax($price, $tax);

Link to comment
https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021387
Share on other sites

You pass the value pass into the function as a parameter when you call the function -

 

<?php   
function test($a) {   
   $a = NULL; 
   return $a;   
}   
   
$a = 1;   
$a = test($a);   
var_dump($a);
?>

 

The above code passes the value in as a parameter and returns the result (you cannot actually use unset in this example like the original code was doing because $a would no longer exist and could not be returned.)

 

If you actually want to unset a main program variable, I recommend just using unset() in your main code rather than making a function with it in it. Replacing a call to a built in function with a call to a user function that only contains a call to that built in function just adds unnecessary overhead to your program.

Link to comment
https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021391
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.