Heim > Artikel > Backend-Entwicklung > 第四章_PHP基本语法(2)_PHP教程
1.常量的声明
在PHP中,定义常量使用define()函数来实现
2.魔术常量
返回文件中的当前行号 | |
__FILE__ | 返回该文件的完整路径和文件名 |
__DIR__ | 返回该文件所在的目录 |
__FUNCTION__ | 返回该函数被定义时的名字 |
__CLASS__ | 返回该类被定义时的名字 |
__MTHOD__ | 返回该类的方法被定义时的名字 |
测试代码如下:
<span 1</span> <?<span php </span><span 2</span> <span function</span><span sayHello(){ </span><span 3</span> <span echo</span> '<p>the name of this function is:'.<span __FUNCTION__</span><span ; </span><span 4</span> <span } </span><span 5</span> <span 6</span> <span echo</span> '<p>这是第'.<span __LINE__</span>.'行。'<span ; </span><span 7</span> <span echo</span> '<p>本文件的绝对路径为:'.<span __FILE__</span><span ; </span><span 8</span> <span sayHello(); </span><span 9</span> ?>
3.引用赋值
用不同的变量名访问同一个变量内容,当其中一个变量的值改变时,另一个也跟着改变。
4.逻辑运算符
与 && 或 || 非 ! 异或 xor
<span 1</span> <?<span php </span><span 2</span> <span $a</span>=<span true</span><span ; </span><span 3</span> <span $b</span>=<span false</span><span ; </span><span 4</span> <span 5</span> <span var_dump</span>(<span $a</span> && <span $b</span>);<span echo</span> '<br>'<span ; </span><span 6</span> <span var_dump</span>(<span $a</span> || <span $b</span>);<span echo</span> '<br>'<span ; </span><span 7</span> <span var_dump</span>(<span $a</span> xor <span $b</span>);<span echo</span> '<br>'<span ; </span><span 8</span> <span var_dump</span>(!<span $a</span>);<span echo</span> '<br>'<span ; </span><span 9</span> ?>
5.比较运算符
①相等 == $a==$b 当$a的值等于$b的值,则返回真,否则返回假
②全等 === $a===$b 当$a的值等于$b的值,且$a与$b的类型也相等,则返回真,否则返回假
③不等 !=或
④不全等 !==
6.位运算符
按位与 | & | 均为1的位置1,其余置为0 |
按位或 | | | 有一个为1的位置为1,其余置为0 |
按位异或 | ^ | 不同的位置为1,其余置为0 |
按位非 | ~ | 1的位置为0,0的位置为1 |
左移 | 位向左移动n次,每移动一次相当于乘以2 | |
右移 | >> | 位向右移动n次,每移动一次相当于除以2 |
<span 1</span> <?<span php </span><span 2</span> <span $a</span>=123<span ; </span><span 3</span> <span $b</span>=321<span ; </span><span 4</span> <span 5</span> <span echo</span> '$a & $b='.(<span $a</span> & <span $b</span>).'<br>'<span ; </span><span 6</span> <span echo</span> '$a | $b='.(<span $a</span> | <span $b</span>).'<br>'<span ; </span><span 7</span> <span echo</span> '$a ^ $b='.(<span $a</span> ^ <span $b</span>).'<br>'<span ; </span><span 8</span> <span echo</span> '~$a='.(~<span $a</span>).'<br>'<span ; </span><span 9</span> <span echo</span> '$a<<4='.(<span $a</span><<4).'<br>'<span ; </span><span 10</span> <span echo</span> '$b>>4='.(<span $b</span>>>4).'<br>'<span ; </span><span 11</span> ?>
7.字符串运算符
(.)和(.=)以及{}
8.数组运算符(略)
9.类型运算符
类型运算符用instanceof常用来判断某个变量是否属于某个类的实例。
<span 1</span> <?<span php </span><span 2</span> <span class</span><span A{ </span><span 3</span> <span 4</span> <span } </span><span 5</span> <span class</span><span B{ </span><span 6</span> <span 7</span> <span } </span><span 8</span> <span 9</span> <span $myclass</span>=<span new</span><span A; </span><span 10</span> <span var_dump</span>((<span $myclass</span> instanceof A));<span echo</span> '<br>'<span ; </span><span 11</span> <span var_dump</span>((<span $myclass</span> instanceof B));<span echo</span> '<br>'<span ; </span><span 12</span> ?>
10.错误控制运算符@