通过php中的include(另一个php文件)传递变量

时间:2014-01-28 14:39:37

标签: php variables

我正在为一个网站开发一个php web应用程序,我想将一些变量从一个php文件传递到另一个。我用包含文件尝试了旧时尚方式,但没有成功。我还尝试在全局范围内设置变量,但仍然无效。 first.php文件中的代码是:

function rc_getAvailableVehicles($pickup_timestamp, $return_timestamp, $vehicle_classes=array()) {
global
    $wpdb;
    $rc_currency = RC_Registry::get('rc_currency');


$where_classes = "";
if ($vehicle_classes) {
    foreach($vehicle_classes as $vehicle_class) {
        $where_classes[] = " v.class = '". $wpdb->escape($vehicle_class) ."'";
    }
    $where_classes = "AND (".implode(' OR ', $where_classes).") ";
}

$sql = "SELECT *
        FROM ".$wpdb->rc_vehicles." v
        WHERE v.quantity > (SELECT COUNT(*) FROM ".$wpdb->rc_bookings." b WHERE b.vehicle_id = v.vehicle_id AND ((UNIX_TIMESTAMP(b.pickup_date) >= '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.pickup_date) < '". (int)$return_timestamp ."') || (UNIX_TIMESTAMP(b.pickup_date) < '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.return_date) >= '". (int)$pickup_timestamp ."')) AND (b.status != 'new' AND b.status != 'canceled') AND b.trash = '0') ".$where_classes."AND v.status = '1' AND v.archive = '0'
        ORDER BY v.rent ASC";   

$results = $wpdb->get_results($sql,ARRAY_A);
$vehicles = array();
$rental_days = ceil(($return_timestamp-$pickup_timestamp)/91800);
$xfee = get_field('another_location_fee');
$xfee2 = get_field('return_to_another_location_fee_2');


if ($results) {

    foreach ($results as $result) {

        if ($result['image'] && file_exists(RC_UPLOADS_DIR . "vehicle_" . $result['image'])) {
            $image = RC_UPLOADS_URL . 'vehicle_' . $result['image'];
            $image_thumb = RC_UPLOADS_URL . 'cache/' . rc_image_resize(RC_UPLOADS_DIR . "vehicle_" . $result['image'], 220, 160);
        } else {
            $image = '';
            $image_thumb = '';
        }
        $result['description'] = do_shortcode($result['description']);
        $description = html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8');


        global $GeneralPrice, $discount_price, $rent1, $rent5;

include_once ('file2.php');             

                    //$GeneralPrice = $result['rent'];
         $rent1 = $vehicle_meta['rent1'];
         $rent2 = $vehicle_meta['rent2'];
         $rent3 = $vehicle_meta['rent3'];
         $rent4 = $vehicle_meta['rent4'];
         $rent5 = $vehicle_meta['rent5'];
         $rent6 = $vehicle_meta['rent6'];
         $rent7 = $vehicle_meta['rent7'];
         $rent8 = $vehicle_meta['rent8'];
         $rent9 = $vehicle_meta['rent9'];
         $rent10 = $vehicle_meta['rent10'];
         $rent11 = $vehicle_meta['rent11'];
         $rent12 = $vehicle_meta['rent12'];
         $rent13 = $vehicle_meta['rent13'];
         $rent14 = $vehicle_meta['rent14'];
         $rent15 = $vehicle_meta['rent15'];
         $rent16 = $vehicle_meta['rent16'];
         $rent17 = $vehicle_meta['rent17'];
         $rent18 = $vehicle_meta['rent18'];
         $rent19 = $vehicle_meta['rent19'];
         $rent20 = $vehicle_meta['rent20'];
         $rent21 = $vehicle_meta['rent21'];
         $rent22 = $vehicle_meta['rent22'];
         $rent23 = $vehicle_meta['rent23'];
         $rent24 = $vehicle_meta['rent24'];
         $rent25 = $vehicle_meta['rent25'];
         $rent26 = $vehicle_meta['rent26'];
         $rent27 = $vehicle_meta['rent27'];
         $rent28 = $vehicle_meta['rent28'];
         $rent29 = $vehicle_meta['rent29'];
         $rent30 = $vehicle_meta['rent30'];
         $rent31 = $vehicle_meta['rent31'];
         $rent32 = $vehicle_meta['rent32'];
         $rent33 = $vehicle_meta['rent33'];
         $rent34 = $vehicle_meta['rent34'];
         $rent35 = $vehicle_meta['rent35'];
         $rent36 = $vehicle_meta['rent36'];


        $vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);
        $vehicles[$result['vehicle_id']] = array(
            'vehicle_id'    => $result['vehicle_id'],
            'title'         => $result['manufacturer']." ".$result['series'],
            'manufacturer'  => $result['manufacturer'],
            'series'        => $result['series'],
            'year'          => $result['year'],
            'class'         => $result['class'],
            'image'         => $image,
            'image_thumb'   => $image_thumb,
            'description'   => $description,
            'seats'         => $vehicle_meta['seats'],
            'doors'         => $vehicle_meta['doors'],
            'baggages'      => $vehicle_meta['baggages'],
            'conditioning'  => $vehicle_meta['conditioning'],
            'transmission'  => $vehicle_meta['transmission'],
            'rent1'          => $vehicle_meta['rent1'],
            'rent2'          => $vehicle_meta['rent2'],
            'rent3'          => $vehicle_meta['rent3'],
            'rent4'          => $vehicle_meta['rent4'],
            'rent5'          => $vehicle_meta['rent5'],
            'rent6'          => $vehicle_meta['rent6'],
            'rent7'          => $vehicle_meta['rent7'],
            'rent8'          => $vehicle_meta['rent8'],
            'rent9'          => $vehicle_meta['rent9'],
            'rent10'          => $vehicle_meta['rent10'],
            'rent11'          => $vehicle_meta['rent11'],
            'rent12'          => $vehicle_meta['rent12'],
            'rent13'          => $vehicle_meta['rent13'],
            'rent14'          => $vehicle_meta['rent14'],
            'rent15'          => $vehicle_meta['rent15'],
            'rent16'          => $vehicle_meta['rent16'],
            'rent17'          => $vehicle_meta['rent17'],
            'rent18'          => $vehicle_meta['rent18'],
            'rent19'          => $vehicle_meta['rent19'],
            'rent20'          => $vehicle_meta['rent20'],
            'rent21'          => $vehicle_meta['rent21'],
            'rent22'          => $vehicle_meta['rent22'],
            'rent23'          => $vehicle_meta['rent23'],
            'rent24'          => $vehicle_meta['rent24'],
            'rent25'          => $vehicle_meta['rent25'],
            'rent26'          => $vehicle_meta['rent26'],
            'rent27'          => $vehicle_meta['rent27'],
            'rent28'          => $vehicle_meta['rent28'],
            'rent29'          => $vehicle_meta['rent29'],
            'rent30'          => $vehicle_meta['rent30'],
            'rent31'          => $vehicle_meta['rent31'],
            'rent32'          => $vehicle_meta['rent32'],
            'rent33'          => $vehicle_meta['rent33'],
            'rent34'          => $vehicle_meta['rent34'],
            'rent35'          => $vehicle_meta['rent35'],
            'rent36'          => $vehicle_meta['rent36'],
            'total_price'  =>  $rc_currency->format((($rental_days*$GeneralPrice)-(($rental_days*$GeneralPrice) * ($discount_price)/100)+ $utime)),
            'total_price_return_fee'  => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent']) * ($discount_price)/100)+ $xfee),
            'total_price_return_fee2'  => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent'])*($discount_price)/100)+ $xfee2),
            'daily_price'   => $rc_currency->format((($GeneralPrice)-($GeneralPrice)*($discount_price)/100)+ ($utime / $rental_days)),
            'daily_price_return_fee'   => $rc_currency->format(($result['rent']-($result['rent'])* ($discount_price)/100)+ ($xfee / $rental_days)),
            'daily_price_return_fee2'   => $rc_currency->format(($result['rent']-($result['rent'])*($discount_price)/100) + ($xfee2 / $rental_days))

        );
    }

}

return $vehicles;

}

我希望回显$rent

中的所有file2.php变量

我试过了

echo $rent;没有成功!

3 个答案:

答案 0 :(得分:0)

$pass = "anything";
include_once ('file2.php'); 

file2.php,您可以访问$pass变量。

答案 1 :(得分:0)

您需要调用file2.php

中存在的file1.php中的函数

离。在file1.php中你可以调用函数return_rent

return_rent($rent);

在file2中然后你创建函数

function return_rent($var){
echo $rent;
}

答案 2 :(得分:0)

您遇到代码顺序问题

$vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);

应该在填充租金变量之前,

include_once ('file2.php');

应该在您填充租金变量之后。