zoukankan      html  css  js  c++  java
  • PHP基础点滴

    PHP基础点滴

    双冒号::的用法:

    双冒号操作符即作用域限定操作符Scope Resolution Operator可以访问静态、const和类中重写的属性与方法。

    伪类型(pseudo-types) 是 PHP 文档里用于指示参数可以使用的类型和值。 请注意,它们不是 PHP 语言里原生类型。 所以不能把伪类型用于自定义函数里的类型约束(typehint)。

    mixed 说明一个参数可以接受多种不同的(但不一定是所有的)类型。

    number 说明一个参数可以是 integer 或者 float。

    $GLOBALS内置全局变量的含义:它不同于超全局变量(如 $_SERVER等),仅仅只是针对当前页面而言。在当前页面下,所定义的变量如果为全局作用域,则会自动加入到$GLOBALS之中,形成关联数组,名称为变量名,值为变量的值。

    超全局变量:无论在哪个页面,哪个作用域内都可以直接使用。

    获取PHP版本信息

    1)超全局变量PHP_VERSION 表示当前PHP版本,其实该变量的值不可人为修改,只可系统操作,可以把它当做是系统的一个内置常量。

    2)phpversion()方法返回一个string变量

    版本比较:version_compare(PHP_VERSION, '5.3.0', '>=');

    version_compare()方法返回一个minxed类型的变量,实际上该结果不能直接打印输出,只能用作判断条件。

    echo __DIR__; //打印出当前php脚本所在的文件物理路径(全),并非项目的绝对或相对路径。

    die()语句:执行到这里,服务器会中断后续的语句执行,如果为die('some string');则会同时输出string内容到页面,作为提示信息。

    mixed constant ( string $name )  :  Return the value of the constant indicated by name. 举个例子:

    <?php
    namespace my
    ame; 
    const MYCONST = 1;
    $d = namespaceMYCONST; 
    echo $d,'<br>';  //输出 1
    $d = __NAMESPACE__ . 'MYCONST';
    echo $d,'<br>';   //输出 my
    ameMYCONST
    echo constant($d),'<br>';  //输出 1
    ?>
    

    PHP的namespace看似与Java的package很相似,但实际上二者差别非常大。Java的package的作用是编译的时候对字节码的存储通过创建文件夹(包括子文件夹)的方式进行分类保存,在运行的时候则会自然而然的在这些文件夹之中去搜索并调用对应的class文件。这样做最大的好处是,让程序员可以忽略细节以及最终的引入结果中的层次关系。但是,PHP无法做到这点,它所涉及的namespace仅仅只是针对单个文件内部而言的,即便是通过include引入外部的文件,但最终还是会让程序员自己来思考融合之后的当前文件内的多个namespace之间的关系,并且不得不去面对一个事实——那就是namespace之间的冲突和引用之间的“混乱”,这种局面极易造成使用上的失误。

    常量__NAMESPACE__的值是包含当前命名空间名称的字符串。在全局的,不包括在任何命名空间中的代码,它包含一个空的字符串。

    关键字namespace默认为当前namespace。

    此外,与namespace紧密相关还有use操作符,它用于导入(import) 或者使用别名(aliase) 。如:

    <?php
    use MyFullClassname as Another, MyFullNSname;
    
    $obj = new Another; // 实例化 MyFullClassname 对象
    NSnamesubnsfunc(); // 调用函数 MyFullNSnamesubnsfunc
    ?>
    

    PHP在使用new操作符实例化的时候,如果没有传入构造函数参数,那么可以省略构造函数名后的();

    PHP的权限只有三种:public,private,protected。而Java却还有default。PHP中的缺省权限就是public。

    PHP中通过const定义的常量,一旦定义值不能更改。而且定义const常量名称不能以$开头,因为对于PHP而言,$就是变量的代名词。

    <?php
    //const $a = 5; //Parse error: syntax error, unexpected '$a'
    const a = 5;
    echo a;   //输出 5
    //a = 3; //Parse error: syntax error, unexpected '=' in line 5
    ?>
    

      

    define定义常量:bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

     同样,常量的名称最好不要使用$开头,虽然不会报错,但是后续运行很可能会出现异常结果(不会提示异常)。一旦定义,便不能修改其值。值得一提的是,如果第三个参数为true便会忽略大小写,默认为false。此外,从PHP7开始,支持值为数组类型。以下是例子:

    <?php
    define("CONSTANT", "Hello world.");
    echo CONSTANT; // 输出 "Hello world."
    echo Constant; // 输出 "Constant" 并导致 Notice
    
    define("GREETING", "Hello you.", true);
    echo GREETING; // 输出 "Hello you."
    echo Greeting; // 输出 "Hello you."
    
    //  PHP 7 起就可以运行了
    define('ANIMALS', array(
        'dog',
        'cat',
        'bird'
    ));
    echo ANIMALS[1]; // 输出 "cat"
    ?>
    

      

     array_change_key_case() 将 array 数组中的所有键名改为全小写或大写。本函数不改变数字索引。

    array array_change_key_case ( array $array [, int $case = CASE_LOWER | CASE_UPPER ] )

    strpos — 查找字符串首次出现的位置。API格式:mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

    <?php
    
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $findme);
    
    // 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
    // 因为 'a' 是第 0 位置上的(第一个)字符。
    if (!$pos) {
        echo "The string '$findme' was not found in the string '$mystring'",'<br>';
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    
    if ($pos == false) {
        echo "The string '$findme' was not found in the string '$mystring'",'<br>';
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    
    if ($pos === false) {
        echo "The string '$findme' was not found in the string '$mystring'";
    } else {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
    
    
    ?>
    

      输出结果:

    The string 'a' was not found in the string 'abc'
    The string 'a' was not found in the string 'abc'
    The string 'a' was found in the string 'abc' and exists at position 0

    在php中定义的作用域有self和parent,在php6中提供了static作用域
    self:表示当前类的作用域,与this不通的是,它不表示类的某个特定实例,在类之外的代码中不能使用self,而且它不能识别自己在继承中层次的位置。也就是说,当在扩展类中使用self时,它调用的不是父类的方法,而是扩展类的重载的方法。
    parent:表示当前类父类的作用域,其余的跟self特性一样。

    "::"的操作符,这个是作用域限定操作符,是用一个双冒号"::"表示,它用来置顶类中不同作用域的级别。左边是作用域,右边是访问作用域的成员。

    双冒号操作符即作用域限定操作符Scope Resolution Operator可以访问静态、const和类中重写的属性与方法。

    在类定义外使用的话,使用类名调用。在PHP 5.3.0,可以使用变量代替类名。

    public 表示全局,类内部外部子类都可以访问;
    private表示私有的,只有本类内部可以使用;
    protected表示受保护的,只有本类或子类或父类中可以访问;

    use与namespace的区别:

    namespace ThinkModel;
    use ThinkModel;

    use中的Model指的是class,namespace中的Model指的是path的一部分,是路径,或者说是文件夹,而不是具体的class文件。

    数组

    $arr = array();
    $arr[] = $value;
    效果等价于:
    $arr = array();
    array_push($arr, $value);
    但是从性能来看,array_push在每次推入大量数据时会比较好,而$arr[]赋值则相反。

     

     ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行/都会有"Header had all ready send by"的错误,这时可以先用ob_start()打开缓冲区PHP代码的数据块和echo()输出都会进入缓冲区而不会立刻输出.当然打开缓冲区的作用很多,只要发挥你的想象。ob_end_flush()输出全部内容到浏览器。ob_get_clean 的意思是先得到 ob_get_contents 的内容,然后再删除缓冲区的内容 (ob_end_clean)。

    “Without output buffering, PHP sends data to your web server as soon as it is ready - this might be line by line or code block by code block. Not only is this slow because of the need to send lots of little bits of data, but it also means you are restricted in the order you can send data. Output buffering cures these ills by enabling you to store up your output and send to send it when you are ready to - or to not send it at all, if you so decide.”

    为了提高性能,通过buffer整体输出来替代分步输出。

    {}大括号可用作变量边界

    // Calls the method specified by the action
    $output = $this->{$this->actions[$action]}();

    require()通常来导入静态的内容,而include()则适合用导入动态的程序代码。 

    http://www.cnblogs.com/wangzehuaw/p/6144764.html

    http://www.jb51.net/article/22467.htm

  • 相关阅读:
    将SqlServer的数据导出到Excel/csv中的各种方法 .
    SqlServer: 单用户模式下查杀相关进程实现单/多用户转换 .
    SQL Server游标的使用【转】
    由几行代码浅析C#的方法参数传递
    脑力风暴之小毛驴历险记(1)好多胡萝卜(下)
    关于sql_mode=only_full_group_by问题的解决方法
    如何同一个controller下添加新页面
    UNIAPP全局变量的实现方法
    Ztree点击节点选中复选框的相关操作
    一沙框架(yishaadmin) 前端引入VUE的实现方法
  • 原文地址:https://www.cnblogs.com/ioveNature/p/6483021.html
Copyright © 2011-2022 走看看