【PHP面试题】PHP变量的作用域和静态变量

【PHP面试题】局部变量,全局变量和引用传参在函数中的使用【代码实现】 一、解题方法 着重 记忆 PHP函数的定义相关内容,理解变量作用域、静态变量、函数的参数和返回值的相关内容,重点记忆我们总结的内置函数。 二、练习题 1、写出如下程序的输出结果: $count = 5; // 不能在函数内部使用 function get_count() { static $count; return $count++; } echo $count; // ... 阅读详情

一、变量的作用域

变量的作用域也称变量的范围,变量的范围即它定义的上下文背景(也是它的生效范围)。大部分的PHP变量只有一个单独的范围。这个单独的范围跨度同样包含了 include 和 require 引入的文件。

1. global关键字:

// global关键字
$outer = 'str'; // 全局变量
function mgfunc()
{
    global $outer; // 可以把外部的变量拿到内部来使用
    echo $outer; // 局部变量
}// 里面的 $outer 和 外面的 $outer不是一个变量

2. $GLOBALS 及 其他超全局数组:

// $GLOBALS
$outer = 'str';
function mgfunc()
{
    $GLOBALS['outer']; 
}

二、静态变量

静态变量:仅在局部函数域中存在,但当程序执行离开此作用域时,其值并不会消失。

static 关键字

  • 仅初始化一次;
  • 初始化时需要赋值;
  • 每次执行函数该值会保留;
  • static 修饰的变量是局部的【仅在函数内部有效】;
  • 可以记录函数的调用次数,从而可以在某些条件下终止递归。

代码实现:

function mgFunc()
{
    static $a = 1;
    echo $a++;
}
myFunc(); // 1
myFunc(); // 2
myFunc(); // 3
三代测序选型指南:PacBio HiFi vs Nanopore vs Illumina技术对比与适用场景 本文深入对比了三代测序技术PacBio HiFi、Nanopore与Illumina的核心参数、成本模型和应用场景,为生物信息学研究提供选型指南。通过实测数据和技术组合策略分析,帮助实验室根据研究需求选择最适合的测序方案,涵盖基因组组装、表观遗传学等关键应用领域。 阅读详情

相关推荐

永磁同步电机高效标定技术:从理论到实践的全面解析

本文全面解析永磁同步电机高效标定技术,从MTPA标定、弱磁区优化到初始角校准,详细介绍了理论原理和实战技巧。针对新能源汽车应用场景,提供标定系统集成要点和AI辅助优化方案,帮助工程师提升电机性能标定效率与精度。

weixin_28452161的博客 510

内部函数和外部函数

内部函数(静态函数): 只能被本文件中其它函数调用。定义内部函数时,在函数名核函数类型的前面加上static。 static int fun (int a,int b) //fun是内部函数,不能被其他文件调用 通常把只能由本文件使用的函数和外部变量放在文件开头,且在前面冠以static将其进行局部化,使其他文件不能引用此函数和外部变量,提高了程序的可靠性。 外部函数:...

qq_44911784的博客 6714

华大HC32F460 TCP Client 实验

华大HC32F460 TCP Client 实验

ZhuoWuKeJi的博客 654

C++ 在函数中定义函数

C++ 实现在函数中定义“函数”的功能

一些笔记 1万+

Matlab实战:手把手教你用Zernike多项式模拟光学像差(附完整代码)

本文详细介绍了如何在Matlab中使用Zernike多项式模拟光学像差,包括基础理论、不同排序系统的实现、完整代码示例以及高级应用技巧。通过Zernike多项式,可以有效地描述和分析光学系统中的波前畸变,适用于望远镜校正、眼科光学等多个领域。

weixin_42523720的博客 171

开源的预测气象大模型与数据获取/可视化

「开源气象大模型与数据获取指南」摘要: 当前主流AI气象模型(如ECMWF的AIFS、DeepMind的GraphCast、华为Pangu等)主要基于ERA5/HRES数据训练。开源资源可通过ECMWF OpenData、GitHub仓库及Google Cloud获取,其中AIFS数据采用CCBY-4.0许可可商用,其他模型多有限制性许可。典型应用流程包括:1)通过Python包获取实时预报数据;2)加载预训练权重进行推理;3)使用xarray/cartopy或Zarr进行可视化。训练数据可从CDS下载ER

专注AI大模型,软件混淆,授权 819

js函数内部定义函数的理解

一,普通的函数中定义函数 首先要区分:函数中定义的函数,和函数作为对象,并给这个对象定义的方法。这两者是不一样的。 函数中定义的函数:需要从数据类型的角度理解,一旦执行这个函数,就会定义出内部一个函数。因为作用域的存在,无法在外面直接调用访问。但是可以在这个函数内直接使用。 function Foo(){ getName=function(){ //虽然这里没有用var,定义的是全局的,外部直接访问是无法访问的。 alert(1) } //这里定义一个函数(从数据

weixin_42349568的博客 9862

【x265编码器】章节1——x265的lookahead模块分析

目录一、模块功能二、lookahead模块分析1.调用流程2.代码分析Lookahead::addPicture()Lookahead::checkLookaheadQueue()Lookahead::getDecidedPicture()Lookahead::findJob()Lookahead::slicetypeDecide()PreLookaheadGroup::processTasksLookaheadTLD::lowresIntraEstimate()Lookahead::slicetypeAn

vcvdv123的博客 4万+

php static面试题,php程序员面试题及答案

php程序员面试题及答案4. 写出如下程序的`输出结果function get_count(){static $count = 0;return $count++;}echo $count;++$count;echo get_count();echo get_count();?>5. 写出如下程序的输出结果$var2 = 1;function get_value(){global $var2...

weixin_36378793的博客 259

VS Code 重大更新:Agent Skills完美支持!

代码不会骗人,但你的 LLM 可能会编个故事——除非你给它一本《技能手册》。——某位深夜调试成功的 Go 工程师。

weixin_44058951的博客 3472

[RK3568 Android11] 教程之原厂SDK源码适配AIO-3568J板子跑起来

目录 一、前提 二、修改dts设备树 三、适配板子的dts 四、编译系统烧入固件验证 一、前提 因源码是直接使用原厂的SDK,没有使用firefly配套的SDK源码,所以手上这块firefly aio-3568j板子在硬件设计上与原厂使用的板子会有些差异;就是因为这些差异会导致新固件烧入后,debug串口打印许多错误信息,可能的话,系统还会崩溃掉,跑不起来; 主要差异在哪里呢?比如: ● DDR类型和品牌多样性; ...

~未来可期~ 1749

自增/自减变量

今天看到一个面试题,里面涉及到一个很初级的一个问题,如下: 写出一下php段的输出结果: <?php$count=5;functionget_count(){static$count=0;return$count++;}echo$count;++$count;echoget_count();echoget_count();?> 答案为...

weixin_34279579的博客 107

android11.0 Launcher3 高端定制之循环滑动

待机界面可以向左、向右循环切换页面 效果图 修改清单 packages/apps/Launcher3/res/values-zh-rCN/strings.xml packages/apps/Launcher3/res/values/config.xml packages/apps/Launcher3/res/values/strings.xml packages/apps/Launcher3/res/xml/launcher_preferences.xml packages/apps/Launc

cczhengv 1559

有趣的PHP面试题----true、false、空串等的比较

对,没错,就是现在这家公司的面试题,这是其中的一部分,基础的一部分,但我答的很烂很烂,本来没有想到能拿到offer,结果却出人意外 &lt;?php // 1. $str1 = null; $str2 = false; echo $str1==$str2 ? '相等' : '不相等'; //相等 $str3 = ''; $str4 = 0; ...

admin_o1的博客 413

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

带坑的几道PHP面试题

第二题 代码如下: $i='11'; printf("%d\n",printf("%d",printf("%d",$i))); 输出结果是多少?如果你回答是11,或者111111就掉入陷阱了。先了解printf这个函数,printf不仅是打印函数,它还有返回值。重点就在这 代码如下: var_dump(printf("%d",$i)); 你猜猜上面的...

weixin_30408165的博客 153

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

php foreach循环 引用的时候的坑

前两天写递归的时候用到了 静态变量  引用  全局变量 用引用的时候出现了 一点意外  网上查资料 发现一个很好的例子来诠释 据说是金山的面试题 $a = array('a','b','c');   foreach($a as &$v){}      foreach($a as $v){      }   var_dump($a);   答案一般人肯定知道不

qq_30502699的博客 1365

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

php面试之考察作用域和静态变量以及函数引用返回

/** * 考察作用域和静态变量 */ $count=5;//全局变量 function getRes() { static $count; //定义了局部变量,这是$count的值是NULL,NULL++是1,true++还是true return ++$count;//这里是1,因为++在前边是先加再返回,如果是$count++就是先返回再加,返回NULL } echo $count;//5,全局变量 ++$count;//加一 echo $count;//6 var_dump(.

xiaoyun888_的博客 152

php中关键字static 和关键字public 有什么不同,php中static关键字对变量和函数影响...

在php中static是一个静态变量,他可以定义函数,变量为全局静态变量了,那么我们在函数或变量前面加上static会对函数与变量产生怎么样的影响呢,下面我们一起来看看。1) 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量。全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式。 这两者在存储方式上并无不同。这两者的区别在于非静态全局变量的作用域是整个源程序, ...

weixin_34416115的博客 690

php 静态方法 好处,php中static关键字对变量和函数影响

在php中static是一个静态变量,他可以定义函数,变量为全局静态变量了,那么我们在函数或变量前面加上static会对函数与变量产生怎么样的影响呢,下面我们一起来看看。1) 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量,全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式,这两者在存储方式上并无不同。这两者的区别在于非静态全局变量的作用域是整个源程序,当一个...

weixin_31741271的博客 324

php 函数中静态变量的问题

&lt;?php function msg() { static $a = 0; echo $a++, '&lt;br /&gt;'; } msg(); msg(); msg(); 上述代码,分别输出0,1,2 静态变量$a在第一次定义并初始化后就会常驻内存,直到脚本执行完毕。 当第二次调用msg()函数时,这时的$a值为1,而不会变成0。 ...

weixin_33708432的博客 184

常见php面试题,常见的 PHP 面试题和答案分享

搜索热词如何直接将输出显示给浏览器?将输出直接显示给浏览器,我们必须使用特殊标记 。PHP 是否支持多重继承?PHP 只支持单继承。PHP 的类使用关键字extends继承另一个类获取图片属性(size,width,和 height)的函数是什么?获取图片大小 size:getimagesize ();获取图片宽度 width:imagesx ();获取图片高度 height:imagesy ...

weixin_28736335的博客 213

php面试编程题_一份不错的php面试题(附答案)

一份不错的php面试题,附答案,有准备换工作的同学可以参考一下.一、基础题1. 写出如下程序的输出结果$str1=null;$str2=false;echo$str1==$str2?'相等':'不相等';$str3='';$str4=0;echo$str3==$str4?'相等':'不相等';$str5=0;$str6='0';echo$str5===...

weixin_39612849的博客 484

常见的 PHP 面试题和答案分享

如何直接将输出显示给浏览器? 将输出直接显示给浏览器,我们必须使用特殊标记 <?=and?>。 PHP 是否支持多重继承? PHP 只支持单继承。PHP 的类使用关键字extends继承另一个类 获取图片属性(size, width, 和 height)的函数是什么? 获取图片大小 size:getimagesize ();获取图片宽度 width:imagesx ();...

weixin_43814458的博客 435
上一篇: 【PHP面试题】请列出3种PHP数组循环操作的语法,并注明各种循环的区别。
下一篇: 【PHP面试题】正则表达式及手机号码的正则表达式。
studyphp123
博客等级 码龄9年 85粉丝 99原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址