php 基础笔记 - variables

本文探讨了PHP中变量命名规范、作用域规则以及变量引用的深入理解,包括全局变量、局部变量、静态变量和变量变量的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***************************by garcon1986********************************/

<?php // variable name is sensitive $var = "sjg"; $Var = "wl"; echo $var.' loves '.$Var.'<br>'; echo "$var, $Var<p>"; //naming conventions for variables //$4site = 'not yet'; // invalid; starts with a number $_4site = 'not yet'; // valid; starts with an underscore $täyte = 'mansikka'; // valid; '洄 is (Extended) ASCII 228. echo $_4site.'<br>'; echo $täyte.'<p>'; // pass the address $ref1 = 'ref1'; $ref2 = &$ref1; echo $ref1.'<br>'; //ref1 echo $ref2.'<br>'; //ref1 $ref2 = "My name is $ref2"; echo $ref1.'<br>'; //ref1 echo $ref2.'<p>'; //ref1 //$ref2 = &(24*7); //Invalid reference. function test1() { return 25; } //$bar = &test(); // Invalid reference. //global scope ; $a1 = 1; /* global scope */ function test2() { $a1 = 2; // local variable echo $a1.'<br>'; /* reference to local scope variable */ } test2(); echo $a1.'<br>'; // global variable //local scope $a2 = 3; $b2 = 4; function Sum2(){ global $a2, $b2; $b2 = $a2 + $b2; } Sum2(); echo $b2.'<p>'; //static variable function test3(){ static $a=0; echo $a; $a++; } test3(); echo '<br>'; test3(); echo '<br>'; test3(); echo '<br>'; test3(); echo '<br>'; test3(); echo '<p>'; //global variable and static variable function test_global_ref(){ global $obj; $obj = &new stdClass; } function test_global_noref(){ global $obj; $obj = new stdClass; } test_global_ref(); var_dump($obj); echo '<br>'; test_global_noref(); var_dump($obj); echo '<p>'; // variable variables $a3 = 'hello'; $$a3 = 'world'; echo "$a3 ${$a3}"."<br>"; echo "$a3 $hello"."<p>"; ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值