PHP Basics Tutorial 6: Functions and Constants

黄舟
Release: 2023-03-06 08:50:01
Original
1209 people have browsed it

What is explained in this section

  • include and include_once

  • require and require_once

  • Constant

  • Introducing file and constant combination case

  • Variable operation function

  • Output statement

Preface

In the previous article, we discussed The application of the function is only made in one file. When we want to call the function in different files, the content of the previous section cannot be realized, but the PHP language provides the concept of introduction. Let this problem be solved perfectly.

File reference

If a file, such as a.php file, wants to use the function of b.php file, this requires us to put the b file in The function is introduced into the a file. In PHP, we provide four ways to achieve the goal, namely include, include_once, require, require_once.

Although there are four methods in total, each introduction method is different.

include and include_once

As you can see from the name, these two imports have include. The common feature of both of them is that when the imported file has an error ( Because you cannot guarantee that every imported file is correct) will not terminate the program, but will continue to execute.

<?php
    include &#39;a.php&#39;;

    echo &#39;上面引入的文件出错了&#39;;
Copy after login

Result:
PHP Basics Tutorial 6: Functions and Constants

include

##include, a form of introducing files, has two characteristics:

  1. When an error occurs in the file introduced by include, the program will not stop immediately, it will continue to execute.

  2. If the file has been imported, it will be imported repeatedly.

The first feature has been discussed above, and the second feature, When include is used repeatedly in code to introduce the same

file, it will be introduced repeatedly.

<?php

/**
 * a.php文件的内容,一个求和的函数
 */
function sum($a,$b){
    return $a + $b;
}

//func.php文件的内容
<?php
    include &#39;a.php&#39;;
    include &#39;a.php&#39;;
    echo &#39;重复的引入文件&#39;;
Copy after login
Result:


PHP Basics Tutorial 6: Functions and ConstantsAs you can see above, if you use include at this time, it will report an error, which means that you have introduced the file before and cannot introduce it again.

include_once

include_once It is very similar to include, except that it will continue to execute when an error occurs, and another thing is that after the file has been introduced, it will not be introduced again.

<?php
    include_once &#39;a.php&#39;;
    include_once &#39;a.php&#39;;
    echo &#39;引入一次文件。&#39;;
.....结果......
引入一次文件。
Copy after login

You can see that the above code does not report an error.

require and require_once

require and require_once are another way to introduce files. The characteristics they share are:

When an error occurs in the imported file, the program will be terminated and will not be executed downwards.

<?php
    require &#39;b.php&#39;;
    echo &#39;这句话不会执行&#39;;
Copy after login

Result:


PHP Basics Tutorial 6: Functions and Constants##require

##require, a form of introducing files, has two characteristics:

When an error occurs in the file introduced by require, the program will stop immediately.

  1. If the file has been imported, it will be imported repeatedly.

  2. The first feature has been discussed above, and the second feature, When require is used repeatedly in code to introduce the same

    file, it will be introduced repeatedly.
  3. <?php
    
    /**
     * a.php文件的内容,一个求和的函数
     */
    function sum($a,$b){
        return $a + $b;
    }
    
    //func.php文件的内容
    <?php
        require &#39;a.php&#39;;
        require &#39;a.php&#39;;
        echo &#39;引入一次文件。&#39;;
    Copy after login
Result:

As you can see above, if you use require at this time, it will report an error, which means that you have introduced the file before and cannot introduce it again.


PHP Basics Tutorial 6: Functions and Constantsrequire_once

require_once It is very similar to require, except that introducing an error will terminate the program, and another thing is that after the file has been introduced, it will not be introduced again.

<?php
    require_once &#39;a.php&#39;;
    require_once &#39;a.php&#39;;
    echo &#39;引入一次文件。&#39;;
.....结果......
引入一次文件。
Copy after login
You can see that the above code does not report an error.

The system provides us with four methods, but how do we know which introduction method to choose?

If we are introducing certain files (such as obtaining a database connection, opening a file, etc. It is recommended to use _once to introduce files) to prevent resource waste. We often use require and require_once in development.


It is more recommended to use require_once. Because using this method can save resources and avoid errors caused by repeated definitions.

File introduction mechanism

In-depth understanding of file introduction: In PHP, when another file is introduced into a file, the imported file will be executed. We can return a value in the imported file, or we can not return it until the end of the file.

<?php

    echo &#39;这是引入的文件<br>&#39;;
    require_once &#39;b.php&#39;; //引入b文件
    echo &#39;当执行完引入的文件后执行这句话<br>&#39;;
b.php文件
<?php
    echo &#39;这是另外一个文件<br>&#39;;
    return;
......结果......
这是引入的文件
这是另外一个文件
当执行完引入的文件后执行这句话
Copy after login
Looking at the above code carefully, you can see that the return termination statement is used in the imported file and does not terminate the termination of the main file. Although they run in the same memory, they do not affect their respective operations.

The return problem of imported files

There are three points in total about the return problem:

  • 在我们引入一个文件时,在默认的情况下,成功返回1

  • 我们也可以根据实际情况返回数据,比如一个数组。

  • 在引入文件过程中,当遇到被引入文件的return语句时,引入过程将终止,返回主文件,继续执行。


常量

在我们的开发过程中,往往需要一些全局性的值,就是已经确定并且我们在以后不会修改他们,比如说网站的根目录路径,这时候我们已经不能够使用变量,因为变量我们是可以修改的。在这里就介绍PHP中另外一种常量

常量:常量可以理解成是一种特殊的变量,一旦被定义,就不能再改变或者取消定义[即: 不能unset常量].

那我们用什么来定义常量呢?PHP中提供了两种方法来定义常量。

  • define(string $name ,常量的值);定义一个常量

    1. 第一个参数就是我们的常量名。

    2. 第二个参数是常量的值;仅允许标量和 null 。标量的类型是 integer , float , string 或者 boolean 。 也能够定义常量值的类型为 resource ,但并不推荐这么做,可能会导致未知状况的发生。

  • const 在 PHP 5.3.0 以后,可以使用 const 关键字在类定义之外定义常量。

我们在定义常量的时候,这两个都可以使用。

<?php
    define(&#39;PI&#39;,3.1415926);
    echo PI . &#39;<br>&#39;;
    const TAX = 0.012;
    echo TAX;
    ......结果......
    3.1415926
    0.012
Copy after login

从上面的代码可以看出常量的定义和变量使不一样的,它们两个是有区别的。

常量和变量的区别:

  1. 常量前面没有美元符号($);

  2. 常量只能用 define() 函数定义,而不能通过赋值语句;

  3. 常量可以不用理会变量的作用域而在任何地方定义和访问;

  4. 常量一旦定义就不能被重新定义或者取消定义;

  5. 常量的值只能是标量。

  6. 常量的名字一般都是大写。

处理我们自己定义的常量外,PHP系统还给我们定义了一些常量,比如我们在讲整型的时候用到的PHP_INT_MAX,获取整型的最大值。

魔术常量

PHP语言可以说有一个特色,它的语法中有魔术这个概念。在系统常量中就有魔术这个概念,PHP称之为魔术常量,那什么是魔术常量?

魔术常量:PHP 向它运行的任何脚本提供了大量的预定义常量。不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了。(一大堆看不懂的话-_-);

简单的来说就是系统总共提供了八个魔术常量,它们的值随着它们在代码中的位置改变而改变。例如 __LINE__ 的值就依赖于它在脚本中所处的行来决定。这些特殊的常量不区分大小写。

PHP的魔术常量:

  1. __LINE__ 文件中的当前行号。

  2. __FILE__ 文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起, __FILE__ 总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。

  3. __DIR__ 文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录,否则目录中名不包括末尾的斜杠。(PHP 5.3.0中新增) =

  4. __FUNCTION__ 函数名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。

  5. __CLASS__ 类的名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。类名包括其被声明的作用区域(例如 Foo\Bar)。注意自 PHP 5.4 起 __CLASS__ 对 trait 也起作用。当用在 trait 方法中时,__CLASS__ 是调用 trait 方法的类的名字。

  6. __TRAIT__ Trait 的名字(PHP 5.4.0 新加)。自 PHP 5.4 起此常量返回 trait 被定义时的名字(区分大小写)。Trait 名包括其被声明的作用区域(例如 Foo\Bar)。

  7. __METHOD__ 类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。

  8. __NAMESPACE__ 当前命名空间的名称(区分大小写)。此常量是在编译时定义的(PHP 5.3.0 新增)。

示例:

<?php
    define(&#39;PI&#39;,3.1415926);
    echo PI . &#39;<br>&#39;;
    const TAX = 0.012;
    echo TAX . &#39;<br>&#39;;

    echo __DIR__; //使用魔术常量来获取当前文件所在的绝对路径。
    ......结果......
    3.1415926
    0.012
    D:\mywamp\Apache24\htdocs\zendstudio\yunsuanfu
Copy after login

可以看到在使用__DIR__这个魔术常量的时候,在运行后,自动获取文件当前的绝对路径。

上面的内容就是常量的使用,当然还是多练。当我们在开发中怎么利用魔术常量和文件引入开发呢?


引入文件和魔术常量的最佳示例

在开发中我们在引入文件的时候,文件的路径是一个关键,我们有两种选择,一种是相对路径,一种是绝对路径。开发中我们往往选择是绝对路径,也就是文件所在的路径比如c:/mywamp/apache24/…,这种格式就可以理解为绝对路径。

在这里我们来对引入文件和魔术常量进行一个综合应用:

PHP Basics Tutorial 6: Functions and Constants

在上面使我们在开发中可能用到的文件夹,今天我们只是简单的进行使用,就是后面的流程

  • 在lib文件夹下有一些f函数文件,里面有我们使用的函数,我们可以定义为function.php

  • 在lib的init.php中使用魔术常量来定义一些常量,用来表示文件的绝对路径,就算你把项目的路径换了,也是可以的,同时在init.php中引入函数库

  • 在index.php中引入init.php文件。

function.php:

<?php

    function getSum($a,$b){ //计算两个数的和
        return $a + $b;
    }

    function getSub($a,$b){ //计算两个数的差
        return $a - $b;
    }
Copy after login

init.php

<?php

    //定义项目的根目录
    define(&#39;WEB_ROOT_PATH&#39;,dirname(__DIR__) . &#39;/&#39;);

    //定义css的路径
    define("CSS_PATH",WEB_ROOT_PATH . &#39;css/&#39;);

    //定义js的路径
    define(&#39;JS_PATH&#39;,WEB_ROOT_PATH . &#39;js/&#39;);

    //定义图片的路径
    define(&#39;IMAGE_PATH&#39;,WEB_ROOT_PATH . &#39;image/&#39;);

    //定义存放模板的路径
    define(&#39;TEMPLATE_PATH&#39;,WEB_ROOT_PATH . &#39;template/&#39;);

    //定义lib的路径
    define(&#39;LIB_PATH&#39;,WEB_ROOT_PATH . &#39;lib/&#39;);

    //定义model的路径
    define(&#39;MODEL_PATH&#39;,WEB_ROOT_PATH . &#39;model/&#39;);


    //我们使用lib的绝对路径来引入function.php文件
    require LIB_PATH . &#39;function.php&#39;;
Copy after login

在init中我们使用魔术常量__DIR__来动态的获取文件的所在目录,然后通过dirname()(说明给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名),得到项目的根目录,然后根据根目录得到各个文件夹的绝对路径,在最后通过绝对路径引入函数文件。

index.php

<?php
    require_once &#39;./lib/init.php&#39;; //这里通过相对路径来获取初始化文件

    //上面的表示已经引入了初始化文件,而我们在初始化中又引入了函数文件,所以可以在这里直接用函数文件里面的函数。

    $sum = getSum(10,23);
    $sub = getSub(10,5);
    echo $sum;
    echo &#39;<br>&#39;;
    echo $sub;
    ......结果......
    33
    5
Copy after login

可以看到在index.php文件中可以正常的使用lib下的函数文件里面的函数。

上面的就是引入文件和魔术常量的最基本的用法。

函数的结尾

通过上面的介绍,算是基本介绍完了函数的使用和常量的使用,学完函数,我们在来回顾回顾以前不知道的。

操作变量的函数

  • isset() : 检测变量是否设置,未定义或值为null时返回假

  • unset() : 销毁指定的变量。你可以在函数中传入变量进行销毁。

  • empty() : 判断一个变量是否被认为是空的。当一个变量并不存在,或者它的值等同于 FALSE ,那么它会被认为不存在。

  • is_int() : is系列有很多函数,用来判断变量的类型,如is_int,当变量是整型类型的,返回true,否则返回false。

PHP输出语句

echo

echo 实际上不是一个函数,是PHP语句,所以后面可以不适用括号,当你想用echo输出多个值得时候,可以用逗号隔开。echo并没有返回值。

print

print 和echo用法一样,但是echo的速度要比print的速度快,print也不是一个函数,它是有返回值得,总是返回1.

print_r

print_r(变量)打印关于变量的易于理解的信息。如果变量是string,integer,float将会直接输出其值,如果变量是一个数组,则会输出一个格式化后的数组。

printf

printf函数返回一个格式化后的字符串,它的语法是

printf(format,arg1,arg2)
Copy after login

var_dump

var_dump()一般我们是输出变量的内容,类型和字符串的内容,类型,长度,从而我们可以看到变量的类型是什么。

总结

通过这两节的讲解,函数和常量的知识算是说完了,函数的调用过程和操作在以后开发中我们一定要了然于胸。

 以上就是PHP基础教程六之函数、常量的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!