Home > php教程 > php手册 > PHP learning log (2)-php variables

PHP learning log (2)-php variables

WBOY
Release: 2016-08-18 08:57:57
Original
1375 people have browsed it

Variables are containers used to store data. Similar to algebra, variables can be assigned a certain value (for example: $x=3) or other variables (for example: $x=$y+$z). The definition of variables mainly has the following rules:

  • Variables start with $, followed by the name of the variable;
  • The variable name consists of numbers, letters, and underscores, and the first character cannot be a number;
  • The variable name cannot contain spaces;
  • Variable names are case-sensitive.

​ A variable in php is created when a value is assigned to it for the first time. If the variable is not assigned a value, an error will appear during output, as shown below:

<span style="color: #000000">php
</span><span style="color: #800080">$x</span><span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">;
</span>?>
Copy after login

At this time, the browser will display an error message:

PHP learning log (2)-php variables

This part is different from Python. If the variable is not assigned a value in Python, the compiler will directly prompt an error. We need to pay attention to it.

PHP learning log (2)-php variablesFigure, Python variable is not assigned an error and an error occurs​​​​​​​​​​​

PHP is a weakly typed language. When defining variables, we do not need to define the type of the variable. PHP will automatically convert the variable into the correct data type based on the value of the variable. As shown in the following example:

<span style="color: #000000">php
</span><span style="color: #800080">$x</span>=3<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=3.0<span style="color: #000000">;
</span><span style="color: #800080">$str</span>="hello"<span style="color: #000000">;
</span><span style="color: #800080">$bool</span>=<span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #800080">$arr</span>=<span style="color: #0000ff">array</span>(PHP learning log (2)-php variables,2,3<span style="color: #000000">);
</span><span style="color: #800080">$_null</span>=<span style="color: #0000ff">NULL</span><span style="color: #000000">;

</span><span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$x</span>),"<br>";     <span style="color: #008000">//</span><span style="color: #008000">输出类型为integ</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$y</span>),"<br>";     <span style="color: #008000">//</span><span style="color: #008000">输出类型为double</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$str</span>),"<br>";   <span style="color: #008000">//</span><span style="color: #008000">输出类型为string</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$bool</span>),"<br>";  <span style="color: #008000">//</span><span style="color: #008000">输出类型为boolean</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$arr</span>),"<br>";   <span style="color: #008000">//</span><span style="color: #008000">输出类型为array</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$_null</span>),"<br>";  <span style="color: #008000">//</span><span style="color: #008000">输出类型为NULL</span>
?>
Copy after login
The result is:

PHP learning log (2)-php variables

Next we will discuss the four variable scopes of PHP. The variable scope defines the scope of the variable. PHP mainly has the following four variable scopes:

    local
  • global
  • static
  • parameter
(PHP learning log (2)-php variables) Local and global scope

Variables defined outside a function have a global scope. Except for functions, the global scope can be accessed by any part of the script. To access global variables in a function, you need to add

before the variable in the function.

global keyword.

Example:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">局部作用域与全局作用域</span><span style="color: #008000">*/</span>
<span style="color: #800080">$a</span>=5<span style="color: #000000">;

</span><span style="color: #0000ff">function</span><span style="color: #000000"> test()
{
    </span><span style="color: #800080">$b</span>=PHP learning log (2)-php variables0<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> "测试函数内变量<br>"<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> "变量a的值为:<span style="color: #800080">$a</span> <br>";  <span style="color: #008000">//</span><span style="color: #008000">变量$a未在函数内定义,在引用时出现错误.</span>
    <span style="color: #0000ff">echo</span> "变量b的值为:<span style="color: #800080">$b</span><br>"<span style="color: #000000">;
}

test();

</span><span style="color: #0000ff">echo</span> "测试函数外变量<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "变量a的值为:<span style="color: #800080">$a</span> <br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "变量b的值为:<span style="color: #800080">$b</span><br>";    <span style="color: #008000">//</span><span style="color: #008000">变量$a未在函数内定义,在引用时出现警告.</span>
?>
Copy after login

结果如下:

PHP learning log (2)-php variables

图、局部变量与全局变量

可见,在局部函数里面,是不能直接访问全局变量的,如果要访问全局变量,须在函数里面的变量前加上global关键字。同样,在函数外也不能直接访问函数里面的变量,函数执行结束内存会自动回收,故我们无法访问到。

(2)global关键字

      global 关键字用于函数内访问全局变量,在函数内调用函数外定义的全局变量,需要用global关键字。值得一提的是,这和参数的调用不同,参数的调用并不会改变该变量在内存中的值,而global调用是直接调用内存中的该值,直接对它进行操作,故会改变其值。

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">在函数内调用函数外的变量,需要用到global关键字</span><span style="color: #008000">*/</span>
<span style="color: #008000">/*重要:</span><span style="color: #008000">这种调用会改变变量在内存中的值</span><span style="color: #008000">*/</span>
<span style="color: #800080">$x</span>=5<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=6<span style="color: #000000">;
</span><span style="color: #008000">/*</span><span style="color: #008000">
 * 以下这种方式是不被允许的,只有在函数内调用函数外的变量才能使用global
golbal $z=7;
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> myTest()
{
    </span><span style="color: #0000ff">global</span> <span style="color: #800080">$x</span>,<span style="color: #800080">$y</span><span style="color: #000000">;
    </span><span style="color: #800080">$y</span>=<span style="color: #800080">$x</span>+<span style="color: #800080">$y</span><span style="color: #000000">;
}

myTest();
</span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>"  <span style="color: #008000">//</span><span style="color: #008000">输出y=PHP learning log (2)-php variablesPHP learning log (2)-php variables</span>
?>
Copy after login

 

*注意:超级全局变量 $GLOBALS[index]

      php将所有全局变量存储在一个名为:$GLOBALS[index]的数组中,这个数组可以在函数内访问,也可以用来直接更新全局变量(只能在函数内进行)。

实例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000"> GLOBALS[index]的用法讲解 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$x</span>=5<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=6<span style="color: #000000">;

</span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_PHP learning log (2)-php variables()
{
    </span><span style="color: #800080">$GLOBALS</span>['y']=<span style="color: #800080">$GLOBALS</span>['x']+<span style="color: #800080">$GLOBALS</span>['y'<span style="color: #000000">];
    </span><span style="color: #008000">/*</span><span style="color: #008000">index不用写$字符,否则报错:$x,$y undefined
    $GLOBALS['$y']=$GLOBALS['$x']+$GLOBALS['$y'];
    </span><span style="color: #008000">*/</span><span style="color: #000000">
}

</span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_2()
{
    </span><span style="color: #800080">$GLOBALS</span>['x']=PHP learning log (2)-php variables5<span style="color: #000000">;
}

</span><span style="color: #0000ff">echo</span> "执行myTest_PHP learning log (2)-php variables:<br>"<span style="color: #000000">;
myTest_PHP learning log (2)-php variables();
</span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>","<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;
</span><span style="color: #800080">$GLOBALS</span>['$x']=PHP learning log (2)-php variables2;     <span style="color: #008000">//</span><span style="color: #008000">未将GLOBALS[index]放在函数里面,无效</span>
<span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;

</span><span style="color: #0000ff">echo</span> "执行myTest_2:<br>"<span style="color: #000000">;
myTest_2();
</span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;
</span>?>
Copy after login

结果为:

GLOBALS[index] introduce

图、GLOBALS[index]的用法示例

(3)static关键字

      在一个函数执行完成之后,它的变量通常都会删除,有时我们希望函数中的某个变量保留,这时我们可以在申明变量时使用static关键字:

实例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">静态变量static的用法</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> myTest()
{
    </span><span style="color: #0000ff">static</span> <span style="color: #800080">$x</span>=0,<span style="color: #800080">$y</span>=PHP learning log (2)-php variables<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">;
    </span><span style="color: #800080">$x</span>++<span style="color: #000000">;
}

myTest(); </span><span style="color: #008000">//</span><span style="color: #008000">输出为:0</span>
myTest(); <span style="color: #008000">//</span><span style="color: #008000">输出为:PHP learning log (2)-php variables</span>
myTest(); <span style="color: #008000">//</span><span style="color: #008000">输出为:2</span>

<span style="color: #008000">/*</span><span style="color: #008000">函数里面的静态变量不能直接被访问</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">echo</span> <span style="color: #800080">$y</span>;  <span style="color: #008000">//</span><span style="color: #008000">输出为:Notice: Undefined variable: y</span>
?>
Copy after login

(4)参数作用域

      参数(parameter)的作用是将值传递给函数的局部变量。

实例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">参数(parameter)传递</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> myTest(<span style="color: #800080">$x</span><span style="color: #000000">)
{
    </span><span style="color: #0000ff">echo</span> "传递的值为:<span style="color: #800080">$x</span>.<br>"<span style="color: #000000">;
}

myTest(</span>5);  <span style="color: #008000">//</span><span style="color: #008000">结果为:传递的值为5.</span>
myTest("string") <span style="color: #008000">//</span><span style="color: #008000">结果为:传递的值为string.</span>
?>
Copy after login

 

 

==php新手,有不对的地方希望各位博友提醒,万分感谢==

Technorati 标签: php,变量,local,global,static,参数传递,GLOBALS[index]
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template