I need an help to understand this problem I am facing...and apologies if it seems silly.
I wrote a function that makes some calculations according to two differents dates (arrival and departure). It all works fine, the returned values is given and it is correct.
Nevertheless while this calculation is made there is variable created ($days) which I would like to use (echo)开发者_高级运维 outside the function. I have tried to make it global but I get an error...guessing that I am on the wrong path!
So my question is how do you get a value inside a function other than the returned value? if it is at all possible of course.
code below:
function costs($date1, $date2, $price) {
$arr= explode("/", $date1);
$timestamp1 = mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
$arr2= explode("/", $date2);
$timestamp2 = mktime(0,0,0,$arr2[1],$arr2[0],$arr2[2]);
$timestamp = $timestamp2 - $timestamp1;
$days = $timestamp/86400;
$cost = $days * $price;
return $cost;
}
Appreciated any little help to understand this. Francesco
You could wrap it up in a class, so you can access the $days..
`
class Something {
public $days;
public function calc($arrival, $departure)
{
// Do your thing
$this -> days = $days;
return $something;
}
public function getDays()
{
return $this -> days;
}
}
`
In your function, include an optional parameter. If this is set, then return the date /instead/ of the difference.
<?php
function fn($arrival, $departure, $getDate = FALSE)
{
//your function stuff
...
//if optional flag is set, then return number of days.
if($getDate)
{
return $days;
}
//if flag not set, then return original value.
return $values;
}
For specific to ur code...try this
function costs($date1, $date2, $price) {
$arr= explode("/", $date1);
$timestamp1 = mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
$arr2= explode("/", $date2);
$timestamp2 = mktime(0,0,0,$arr2[1],$arr2[0],$arr2[2]);
$timestamp = $timestamp2 - $timestamp1;
$days = $timestamp/86400;
$cost = $days * $price;
$arr = array();
$arr['cost'] = $cost;
$arr['days'] = $days;
return $arr;
}
If you have the $cost value returned by the function, and you know the $price value that you're passing into the function when you call it; then
$days = $cost / $price;
Either return an array from your function, or send a reference to the destination into the function by way of a parameter.
list ($cost, $return_days)=costs("01/03/2011", "17/03/2011", 100, $ref_days);
// $ref_days and $return_days now hold the same value
function costs($date1, $date2, $price, &$days) {
...
$days = $timestamp/86400;
$cost = $days * $price;
return array($cost,$days);
}
These should really be two separate functions, with the one using the other.
function costs($date1, $date2, $price) {
$cost = days($date1, $date2) * $price;
return $cost;
}
function days($date1, $date2) {
$arr= explode("/", $date1);
$timestamp1 = mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
$arr2= explode("/", $date2);
$timestamp2 = mktime(0,0,0,$arr2[1],$arr2[0],$arr2[2]);
$timestamp = $timestamp2 - $timestamp1;
return $timestamp/86400;
}
EDIT: Then you can call either function depending on what you want without duplicating code.
It doesn't look like you're using objects, so I won't give you the OO spiel, but you can definitely split these up into separate functions so you're not doing wierd stuff with globals and muddying up your namespaces.
精彩评论