php_lang_ref:Language Reference >> References Explained >> What References Do

本文深入探讨了PHP中的引用概念,包括不同场景下引用的行为表现,并通过具体案例解释了引用在全局变量、foreach循环及函数参数传递中的应用。
<?php
// +----------------------------------------------------------------------
// | Created by im-server.
// +----------------------------------------------------------------------
// | Language Reference >> References Explained >> What References Do
// +----------------------------------------------------------------------
// | Author: alexander <gt199899@gmail.com>
// +----------------------------------------------------------------------
// | Datetime: 2017-07-16 16:52
// +----------------------------------------------------------------------
// | Perfect Is Shit
// +----------------------------------------------------------------------

/**
 * PHP 的引用允许用两个变量来指向同一个内容。
 * 需要注意的是如果$a引用了$b,并不是$a指向了$b,或者相反,而是$a和$b同时指向了一个内容。
 * case1:如果对一个未定义的变量进行引用赋值、引用参数传递或引用返回,则会自动创建该变量;
 * case2:如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用 $GLOBALS 数组避免这一点;
 * case3:foreach中的另一种引用赋值;
 * case4:引用传递变量;
 */

namespace case1;
$x = &$xx;
var_dump($x);
var_dump($xx);

function foo(&$var)
{
}

foo($a); // $a is "created" and assigned to null
var_dump($a);

$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)

$c = new \StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)

function &fee()
{
    static $a;
    return $a;
}

$fee = fee();
var_dump($fee);
$fee = 10;
$fee = fee();
var_dump($fee);

$fee = &fee();
var_dump($fee);
$fee = 10;
$fee = &fee();
var_dump($fee);

/**
 * 输出:
 * NULL
 * NULL
 * NULL
 * bool(true)
 * bool(true)
 * NULL
 * NULL
 * NULL
 * int(10)
 */

namespace case2;
$var1 = "Example variable";
$var2 = "";

function global_references($use_globals)
{
    global $var1, $var2;
    if (!$use_globals) {
        $var2 =& $var1; // visible only inside the function
    } else {
        $GLOBALS["var2"] =& $var1; // visible also in global context
    }
}

global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
/**
 * global $var; 是$var = &$GLOBALS['var'];的简写。
 * 从而 $var2 =& $var1; 仅仅只改变了本地变量的引用。
 * 输出:
 * var2 is set to ''
 * var2 is set to 'Example variable'
 */

namespace case3;
$ref = 0;
$row =& $ref;
foreach (array(1, 2, 3) as $row) {
    // do something
}
echo $row . " ";
echo $ref . " ";
$row = 1;
echo $row . " ";
echo $ref . " ";
/**
 * 输出:
 * 3 3 1 1
 */

namespace case4;
function foo(&$var)
{
    $var++;
}

$a = 5;
foo($a);
echo $a;
/**
 * 输出:
 * 6
 */
Table users { id int [pk, increment] username varchar [not null, unique] password varchar [not null] email varchar phone varchar address text create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:禁用 1:正常'] role varchar [default: 'USER', comment: 'USER:普通用户 ADMIN:管理员'] points int [default: 0] total_spent decimal [default: 0.00] } Table categories { id int [pk, increment] name varchar [not null] description text create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1] } Table products { id int [pk, increment] name varchar [not null] description text price decimal [not null] stock int [default: 0] image varchar category_id int create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:下架 1:上架'] sales_count int [default: 0, comment: '销量'] rating decimal [default: 0.00, comment: '平均评分'] update_time timestamp Ref: categories.id > products.category_id } Table cart_items { id int [pk, increment] user_id int [not null] product_id int [not null] quantity int [default: 1] create_time timestamp [default: `CURRENT_TIMESTAMP`] update_time timestamp [default: `CURRENT_TIMESTAMP`, on_update: `CURRENT_TIMESTAMP`] Ref: users.id > cart_items.user_id Ref: products.id > cart_items.product_id Unique: (user_id, product_id) } Table orders { id int [pk, increment] user_id int [not null] order_no varchar [not null, unique] total_amount decimal [not null] status int [default: 0, comment: '0:待付款 1:已付款 2:已发货 3:已完成 4:已取消'] address text phone varchar receiver_name varchar create_time timestamp [default: `CURRENT_TIMESTAMP`] pay_time timestamp ship_time timestamp complete_time timestamp payment_method varchar [comment: '支付方式:ALIPAY/WEIXIN/BANK'] payment_status int [default: 0, comment: '0:未支付 1:已支付 2:支付失败'] Ref: users.id > orders.user_id } Table order_items { id int [pk, increment] order_id int [not null] product_id int [not null] product_name varchar [not null] price decimal [not null] quantity int [not null] subtotal decimal [not null] Ref: orders.id > order_items.order_id Ref: products.id > order_items.product_id } Table product_reviews { id int [pk, increment] user_id int [not null] product_id int [not null] order_id int [not null] rating int [not null, check: `rating >= 1 AND rating <= 5`] content text images text [comment: '评价图片,多个图片用逗号分隔'] create_time timestamp [default: `CURRENT_TIMESTAMP`] status int [default: 1, comment: '0:隐藏 1:显示'] Ref: users.id > product_reviews.user_id Ref: products.id > product_reviews.product_id Ref: orders.id > product_reviews.order_id } Table payment_records { id int [pk, increment] order_id int [not null] payment_no varchar [not null, unique] amount decimal [not null] payment_method varchar [not null] status int [default: 0, comment: '0:待支付 1:支付成功 2:支付失败 3:已退款'] create_time timestamp [default: `CURRENT_TIMESTAMP`] pay_time timestamp transaction_id varchar [comment: '第三方支付交易号'] Ref: orders.id > payment_records.order_id } Table user_permissions { id int [pk, increment] user_id int [not null] permission varchar [not null, comment: '权限名称'] create_time timestamp [default: `CURRENT_TIMESTAMP`] Ref: users.id > user_permissions.user_id Unique: (user_id, permission) } 帮我修改
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值