Home > php教程 > php手册 > body text

速战速决 (1) - PHP: 概述, 常量, 变量, 运算符, 表达式, 控制语句 - webabcd

WBOY
Release: 2016-05-20 10:14:58
Original
1444 people have browsed it

[源码下载]


速战速决 (1) - PHP: 概述, 常量, 变量, 运算符, 表达式, 控制语句



作者:webabcd


介绍
速战速决 之 PHP

  • 概述
  • 常量
  • 变量
  • 运算符
  • 表达式
  • 控制语句



示例
1、概述
basic/summary.php

<!--<span style="color: #000000;">
    PHP 概述
</span>-->

<span style="color: #000000;">php
</span><span style="color: #0000ff;">echo</span> "我在 php 开始标记和结束标记之间"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span>?>

<?php <span style="color: #0000ff;">if (<span style="color: #008080;">time</span>() % 2 == 0): ?><span style="color: #000000;">
    我是 html(用于演示 php 和 html 混编)
    </span><br>
<?php <span style="color: #0000ff;">else: ?><span style="color: #000000;">
    这里是 html(用于演示 php 和 html 混编)
    </span><br>
<?php <span style="color: #0000ff;">endif; ?>


<span style="color: #000000;">php
</span><span style="color: #0000ff;">echo</span> "可以没有 php 结束标记"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>";
Copy after login


2、常量
basic/constant.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 常量
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 用 define 来定义常量</span>
<span style="color: #008080;">define</span>("MyConst1", "MyConst1"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> MyConst1;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 在 PHP 5.3.0 之后支持用 const 来定义常量</span>
<span style="color: #0000ff;">const</span> MyConst2 = "MyConst2"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> MyConst2;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;



</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 魔术常量(magic constant)
 *
 * 魔术常量,在不同的场景下会有不同的值
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> __LINE__ - 文件中的当前行号, 由其在脚本中所处的行来决定</span>
<span style="color: #0000ff;">echo</span> <span style="color: #ff00ff;">__LINE__</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> __FILE__ - 当前文件的完整路径和文件名,如果用在被包含文件中,则返回被包含的文件名</span>
<span style="color: #0000ff;">echo</span> <span style="color: #ff00ff;">__FILE__</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> __DIR__ - 文件所在的目录,如果用在被包括文件中,则返回被包括的文件所在的目录</span>
<span style="color: #0000ff;">echo</span><span style="color: #000000;"> __DIR__;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> __FUNCTION__ - 函数名称
// __CLASS__ - 类名称
// __METHOD__ - 方法名称
// __NAMESPACE__ - 命名空间名称
// __TRAIT__ - Trait 名称</span>
Copy after login


3、变量
basic/variable.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</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;">$a</span> = "a"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 $ 使用变量</span>
<span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> output: a</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 $ 使用变量,即使外面加了双引号也同样会使用变量</span>
<span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$a</span>"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: a</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 {} 界定变量名称,比如"$ab",php会使用变量$ab,而实际上我们想要使用的是$a,此时就可以通过 {} 来界定变量名称,如下:</span>
<span style="color: #0000ff;">echo</span> "{<span style="color: #800080;">$a</span>}b"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: ab</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 外面加双引号,但是不想使用变量的话,可以通过转移符“\”来将“$”变成字符串</span>
<span style="color: #0000ff;">echo</span> "\$a"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: $a</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 外面加单引号的话就按字符串处理了</span>
<span style="color: #0000ff;">echo</span> '$a'; <span style="color: #008000;">//</span><span style="color: #008000;"> output: $a</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;



</span><span style="color: #008000;">//</span><span style="color: #008000;"> 变量之间赋值</span>
<span style="color: #800080;">$b</span> = <span style="color: #800080;">$a</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 & 定义引用。下面的例子是让 $c 引用 $a</span>
<span style="color: #800080;">$c</span> = &<span style="color: #800080;">$a</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 为 $a 重新赋值</span>
<span style="color: #800080;">$a</span> = "aa"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$b</span>"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: a</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$c</span>"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: aa</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;



</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f1()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 这里的 $a 引用的是局部变量 $a,然而之前在 f1 这个局部范围内并没有定义 $a,所以你要使用 $a 的话它是不会有任何内容的</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output:</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
f1();

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f2()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 global 关键字标记 $a,则在此局部范围之后,再使用 $a 时将会使用全局版本的 $a</span>
    <span style="color: #0000ff;">global</span> <span style="color: #800080;">$a</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: aa</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
f2();

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f3()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 $GLOBALS 直接使用指定变量的全局版本</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$GLOBALS</span>['a']; <span style="color: #008000;">//</span><span style="color: #008000;"> output: aa</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
f3();

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f4()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 static 来定义静态变量,其常驻内存</span>
    <span style="color: #0000ff;">static</span> <span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$i</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

    </span><span style="color: #800080;">$i</span>++<span style="color: #000000;">;
}
f4(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> output: 0</span>
f4(); <span style="color: #008000;">//</span><span style="color: #008000;"> output: 1</span>
f4(); <span style="color: #008000;">//</span><span style="color: #008000;"> output: 2</span>


<span style="color: #800080;">$x</span> = "aaa"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> variable variables(可变变量),将变量的值作为变量名
// 下面这句等同于 $aaa = "bbb";(因为 $x 的值是 aaa,所以 $$x 等同于 $aaa)</span>
$<span style="color: #800080;">$x</span> = "bbb"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$aaa</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: bbb</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> $<span style="color: #800080;">$x</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: bbb</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;



</span><span style="color: #800080;">$s</span> = "12345"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 大括号的作用: 用于指示字符串变量中的单个字符(下标从 0 开始)</span>
<span style="color: #800080;">$s</span>{1} = 'x'<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 1x345</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 大括号的作用: 用于界定变量的名称(比如下面使用的变量是 $s,而不是 $ss)</span>
<span style="color: #0000ff;">echo</span> "{<span style="color: #800080;">$s</span>}s"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: 1x345s</span>
<span style="color: #0000ff;">echo</span> "<br>";
Copy after login


4、运算符, 表达式, 控制语句
basic/statement.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</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;">$s1</span> = "hello "<span style="color: #000000;">;
</span><span style="color: #800080;">$s2</span> = "webabcd"<span style="color: #000000;">;
</span><span style="color: #800080;">$s3</span> = <span style="color: #800080;">$s1</span> . <span style="color: #800080;">$s2</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s3</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #800080;">$s1</span> .= "wanglei"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello wanglei</span>
<span style="color: #0000ff;">echo</span> <span style="color: #800080;">$s1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;


</span><span style="color: #008000;">//</span><span style="color: #008000;"> == 和 === 的区别</span>
<span style="color: #800080;">$a1</span> = "1"; <span style="color: #008000;">//</span><span style="color: #008000;"> 字符串</span>
<span style="color: #800080;">$b1</span> = 1; <span style="color: #008000;">//</span><span style="color: #008000;"> 整型</span>
<span style="color: #800080;">$c1</span> = "1"; <span style="color: #008000;">//</span><span style="color: #008000;"> 字符串</span>
<span style="color: #0000ff;">if</span> (<span style="color: #800080;">$a1</span> == <span style="color: #800080;">$b1</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 两个不同类型的数据做 == 比较,系统会自动转换</span>
<span style="color: #000000;">{
    </span><span style="color: #0000ff;">echo</span> '$a1 == $b1'; <span style="color: #008000;">//</span><span style="color: #008000;"> output: $a1 == $b1</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$a1</span> === <span style="color: #800080;">$b1</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 两个不同类型的数据做 === 比较,系统不会自动转换</span>
<span style="color: #000000;">{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 不成立</span>
<span style="color: #000000;">}
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$a1</span> === <span style="color: #800080;">$c1</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> '$a1 === $c1'; <span style="color: #008000;">//</span><span style="color: #008000;"> output: $a1 === $c1</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对于 {} 方式的或者省略了 {} 方式的 if/else 语句来说,elseif 和 else if 是一样的</span>
<span style="color: #0000ff;">if</span> (1 == 2<span style="color: #000000;">)
    </span><span style="color: #0000ff;">echo</span> "1 == 2"<span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (1 == 1<span style="color: #000000;">)
    </span><span style="color: #0000ff;">echo</span> "1 == 1"<span style="color: #000000;">;
</span><span style="color: #0000ff;">elseif</span> (2 == 2<span style="color: #000000;">)
    </span><span style="color: #0000ff;">echo</span> "2 == 2"<span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span>
    <span style="color: #0000ff;">echo</span> "else"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对于 : 方式的 if/else 语句来说,只能用 elseif 而不用能 else if</span>
<span style="color: #0000ff;">if</span> (1 == 2):
    <span style="color: #0000ff;">echo</span> "1 == 2"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> else if (1 == 1):
  //  echo "1 == 1";</span>
<span style="color: #0000ff;">elseif</span> (2 == 2):
    <span style="color: #0000ff;">echo</span> "2 == 2"<span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span>:
    <span style="color: #0000ff;">echo</span> "else"<span style="color: #000000;">;
</span><span style="color: #0000ff;">endif</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;


</span><span style="color: #008000;">//</span><span style="color: #008000;"> foreach 语句的用法</span>
<span style="color: #800080;">$array1</span> = <span style="color: #0000ff;">array</span>(1, 2, 3, 4<span style="color: #000000;">);
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array1</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$value</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array1</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$key</span> . " - " . <span style="color: #800080;">$value</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}


</span><span style="color: #008000;">//</span><span style="color: #008000;"> try catch finally 的用法</span>
<span style="color: #0000ff;">try</span><span style="color: #000000;">
{
    </span><span style="color: #0000ff;">echo</span> "try"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

    </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>("error message", 999); <span style="color: #008000;">//</span><span style="color: #008000;"> 要想写自定义的 Exception 的话,就继承 Exception 就好</span>
<span style="color: #000000;">}
</span><span style="color: #0000ff;">catch</span> (<span style="color: #0000ff;">Exception</span> <span style="color: #800080;">$ex</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> "catch"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

    </span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">sprintf</span>("message: %s, code: %d, file: %s, line: %d, trace: %s", <span style="color: #800080;">$ex</span>->getMessage(), <span style="color: #800080;">$ex</span>->getCode(), <span style="color: #800080;">$ex</span>->getFile(), <span style="color: #800080;">$ex</span>->getLine(), <span style="color: #800080;">$ex</span>-><span style="color: #000000;">getTraceAsString());
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
finally
{
    </span><span style="color: #0000ff;">echo</span> "finally"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}</span>
Copy after login



OK
[源码下载]

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