Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.