4zhvml8 发表于 2024-10-4 19:56:17

「PHP8入门指南」语法基本与变量


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">前言:</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP是一门解释性弱类型的高级编程语言,自诞生<span style="color: black;">败兴</span>就<span style="color: black;">始终</span>秉着大道至简,一切从易的原则。<span style="color: black;">因此</span>PHP<span style="color: black;">针对</span>编程入门<span style="color: black;">来讲</span>,是一门<span style="color: black;">非常</span>容易上手且能<span style="color: black;">火速</span>投入<span style="color: black;">研发</span>工作中的编程语言。<span style="color: black;">亦</span>正<span style="color: black;">由于</span>如此,<span style="color: black;">引起</span>PHP的从业者们良莠不齐,<span style="color: black;">亦</span>就产生了“PHP是世界上最好的语言”的调侃。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">基本</span>语法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">文件格式</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP的代码文件是以 .php 结尾的,如 test.php</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">代码区声明</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP的代码<span style="color: black;">是由于</span>一对&lt;?php 和 ?&gt; <span style="color: black;">包裹</span>起来的,即:</span></p>&lt;?php
    echo "hello world\n";
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:?&gt; 尾标签<span style="color: black;">能够</span>忽略不写,不影响程序的执行。头标签必须写!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">输出方式</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP<span style="color: black;">重点</span>有两个输出语法:echo 与 print。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">echo 和 print 区别:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1. echo - <span style="color: black;">能够</span>输出一个或多个字符串</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2. print - 只<span style="color: black;">准许</span>输出一个字符串,返回值总为 1</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">提示:echo 输出的速度比 print 快, echo <span style="color: black;">无</span>返回值,print有返回值1。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例:</span></p>&lt;?php
    echo "hello world\n";
    print "hello world\n";<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">输出结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d09f7a2634524341a3f03a42710fa3fe~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=MCAfclamfzGc6%2BaH%2FAbqPoBXvZI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">变量</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">介绍</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">变量其实<span style="color: black;">能够</span>理解为是一个代数,类似<span style="color: black;">咱们</span>从小学习的方程式。例:x + y = 3; x = 1;求y?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在计算机编程语言中,变量<span style="color: black;">是用于存储信息的“容器”;</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">变量<span style="color: black;">能够</span>是很短的名<span style="color: black;">叫作</span>(如 x 和 y)<span style="color: black;">或</span>更具描述性的名<span style="color: black;">叫作</span>(<span style="color: black;">例如</span> man girl tom)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">PHP 变量规则:</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. </strong><span style="color: black;"><strong style="color: blue;">变量以 $ 符号<span style="color: black;">起始</span>,后面跟着变量的名<span style="color: black;">叫作</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. </strong><span style="color: black;"><strong style="color: blue;">变量名必须以字母<span style="color: black;">或</span>下划线字符<span style="color: black;">起始</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3. </strong><span style="color: black;"><strong style="color: blue;">变量名只能<span style="color: black;">包括</span>字母、数字以及下划线(A-z、0-9 和 _ )</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4. </strong><span style="color: black;"><strong style="color: blue;">变量名<span style="color: black;">不可</span><span style="color: black;">包括</span>空格</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5. </strong><span style="color: black;"><strong style="color: blue;">变量名是区分<span style="color: black;">体积</span>写的($y 和 $Y 是两个<span style="color: black;">区别</span>的变量)</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">重视</span>:PHP的变量是区分<span style="color: black;">体积</span>写的!</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">变量<span style="color: black;">运用</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>PHP弱类型语言的特性,在声明<span style="color: black;">运用</span>变量时,<span style="color: black;">咱们</span>无需关注即将要声明的变量的类型。将<span style="color: black;">咱们</span>想要存储的数据直接声明并赋值给变量,PHP会自动解析变量类型。而在强类型的编程语言中,<span style="color: black;">咱们</span>必须在<span style="color: black;">运用</span>变量前先声明(定义)变量的类型和名<span style="color: black;">叫作</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例:</span></p>&lt;?php
    $a = 1; // 整型
    $f = 1.0; // 浮点型
    $boolean = true; // 布尔型
    $string1 = hello; // 字符串
    $string2 = "hello";
    $array = ; // 数组
    $test = null; // null型<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:代码片段中的 // 为注释标签,不影响程序运行。相同的还有 /** **/</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">变量<span style="color: black;">功效</span>域</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在所有函数<span style="color: black;">外边</span>定义的变量,<span style="color: black;">持有</span>全局<span style="color: black;">功效</span>域。除了函数外,全局变量<span style="color: black;">能够</span>被脚本中的任何部分<span style="color: black;">拜访</span>,要在一个函数中<span style="color: black;">拜访</span>一个全局变量,需要<span style="color: black;">运用</span> global 关键字。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在 PHP 函数内部声明的变量是局部变量,仅能在函数内部<span style="color: black;">拜访</span>。</span></p>&lt;?php
    $a = 1; //全局变量

    function test() {
    $b = 2; // 局部变量
    global $a; //<span style="color: black;">经过</span>global关键字来<span style="color: black;">拜访</span>全局变量
    echo "函数内部<span style="color: black;">运用</span>局部变量是 {$b}\n";
    echo "函数内部<span style="color: black;">运用</span>全局变量是 {$a}\n";
    }

    test(); //运行函数
    echo "函数<span style="color: black;">外边</span><span style="color: black;">运用</span>局部变量是 {$b}\n";
    echo "函数<span style="color: black;">外边</span><span style="color: black;">运用</span>全局变量是 {$a}\n";<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/20fe205d0a64495c943932b6e9ff517e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=d7qwjJt2WYtGixrJeJ%2BxnCV%2BsF4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看出,在函数<span style="color: black;">外边</span>去<span style="color: black;">拜访</span>函数内部创建的变量$b时,程序代码会报错提示未定义的变量$b,但<span style="color: black;">拜访</span>$a不会报错。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">重视</span>:你<span style="color: black;">能够</span>在<span style="color: black;">区别</span>函数中<span style="color: black;">运用</span>相同的变量名<span style="color: black;">叫作</span>,<span style="color: black;">由于</span>这些函数内定义的变量名<span style="color: black;">叫作</span>是局部变量,只<span style="color: black;">功效</span>于该函数内。</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">Global关键字</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">global 关键字用于函数内<span style="color: black;">拜访</span>全局变量。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在函数内调用函数外定义的全局变量,<span style="color: black;">咱们</span>需要在函数中的变量前加上 global 关键字。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP 将所有全局变量存储在一个名为 $GLOBALS 的数组中。 index <span style="color: black;">保留</span>变量的名<span style="color: black;">叫作</span>。这个数组<span style="color: black;">能够</span>在函数内部<span style="color: black;">拜访</span>,<span style="color: black;">亦</span><span style="color: black;">能够</span>直接用来更新全局变量。</span></p>&lt;?php
    $x = 15;
    $y = 101;
    function test()
    {
    $GLOBALS = $GLOBALS + $GLOBALS;
    }
    test();
    echo $y;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/39f4386302ec4bef827e1f6e1a026d72~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=JoYlU6qadbmuX2QJlG6xkfkv1BQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">Static关键字</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当一个函数完成时,它的所有变量<span style="color: black;">一般</span>都会被删除。然而,有时候您<span style="color: black;">期盼</span>某个局部变量不要被删除。<span style="color: black;">此时</span><span style="color: black;">咱们</span>就<span style="color: black;">能够</span><span style="color: black;">运用</span>static关键字,不过由static声明的局部变量始终还是局部变量,并<span style="color: black;">不可</span>在函数<span style="color: black;">外边</span>进行<span style="color: black;">拜访</span>。相较于<span style="color: black;">无</span>static的局部变量而言,只是不会在函数完成后被删除<span style="color: black;">罢了</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例:</span></p>&lt;?php

    function test() {
    static $a = 0;
    $a = $a + 1;
    echo $a."\n";
    }
    test();
    test();
    test();
    test();<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">输出结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/85a657c10b3444c5bc2f713b2506518a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=vF7OBlLVRqQGcTM4SeqT2ZxPqvs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>看到当<span style="color: black;">咱们</span>执行四次test()函数,static(静态)变量$a<span style="color: black;">亦</span>就累加了四次。静态变量$a在每次test()函数执行完后都<span style="color: black;">无</span>被删除,而是继续存在,当<span style="color: black;">咱们</span>再次调用test()函数时,PHP就会取出静态变量$a用于函数内部的执行。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">那<span style="color: black;">倘若</span><span style="color: black;">咱们</span>去掉static会是怎么样呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例:</p>&lt;?php

    function test() {
    $a = 0;
    $a = $a + 1;
    echo $a."\n";
    }
    test();
    test();
    test();
    test();<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出结果:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/e45e90d2b9a141c2861989409f75fafc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=JmEqD7tvfwxkoo7896q79ZWAzuY%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看出,当<span style="color: black;">咱们</span>去除static关键字后,test()函数的每次执行都是一次“崭新”的执行。变量$a在每次执行<span style="color: black;">起始</span>时都会重新创建,执行结束后又被删除,并不会像静态变量那样继续留存在内存中。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">关于PHP的<span style="color: black;">基本</span>语法与变量的声明和<span style="color: black;">运用</span>,<span style="color: black;">咱们</span>就学完了。基于弱类型语言的特点,<span style="color: black;">咱们</span>在PHP中<span style="color: black;">运用</span>变量时无需像在C语言JAVA语言以及Go语言这类强类型编程语言中那样去关注变量的数据类型;PHP会<span style="color: black;">按照</span><span style="color: black;">咱们</span>的赋值自动推导出变量类型;</span><span style="color: black;">这<span style="color: black;">亦</span>就<span style="color: black;">表率</span>着有着相同变量名的变量<span style="color: black;">能够</span>相互覆盖声明而不报错,在<span style="color: black;">咱们</span>编程过程中需要避免这种重复覆盖声明<span style="color: black;">运用</span>的<span style="color: black;">状况</span>。</span></p>&lt;?php
    $a = 1;
    $a = "hello";
    echo $a; //<span style="color: black;">咱们</span>之前声明的变量a就被后面声明的同名变量所覆盖<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/ff9bdebcb3ca496fb0acf235d0a1eeb0~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728281962&amp;x-signature=bm1LM2qytIi6t9%2BCKu%2ByZ4oJmNs%3D" style="width: 50%; margin-bottom: 20px;"></div>




b1gc8v 发表于 5 天前

感谢楼主分享,祝愿外链论坛越办越好!
页: [1]
查看完整版本: 「PHP8入门指南」语法基本与变量