">

PHP basic study notes (12)

WBOY
Release: 2016-08-08 09:27:22
Original
1061 people have browsed it

php基本语法

<?<span>php
    </span><span>//</span><span>这个是php单行注释</span>
    <span>/*</span><span>
        这是多行注释
    </span><span>*/</span>
    <span>//</span><span>每条php语句使用英文分号结束&mdash;&mdash;必须使用!
    </span><span>//</span><span>php是一门区分大小写的语言&mdash;&mdash;但函数名除外
    </span><span>//</span><span>php的任何一个变量名都要使用一个$符开头。</span>
?>
Copy after login

变量和常量

php中,变量使用一个$符号开头。变量通常是直接赋值(此时如果是第一次赋值也就表示定义)。

常量:设定一个其值不会也不应该改变的数据标识符(数据名字),比如数学上的“pi”。

$pi = 3.14;        //这是变量

define(“PI”, 3.14);    //这是定义一个常量,名字为“PI”,值为3.14,这里也就类似赋值

常量定义的同时也就是赋值,且只是这一次性的!

常量和变量的区别:

  • 常量前面没有美元符号($)
  • 常量只能用 define() 函数定义,而不能通过赋值语句赋值
  • 常量可以不用理会变量范围的规则而在任何地方定义和访问
  • 常量一旦定义就不能被重新定义或者取消定义
  • 常量的值只能是标量(即整数,浮点数,字符串,布尔)

数据类型:

         基本类型(标量类型):

                   布尔类型(bool,boolean): 只有两个数据:true  false

                   字符类型(string):可以是单引号,也可以是双引号,还可以是“定界符”格式

                   整数类型(int,integer):

                            $v1 = 10;

                            $v2 = 012;                  //以0开始的数字表示“8进制”整数,这里相当于10进制的10

                            $v3 = 0x1F;                //以0x开始的数字表示16进制整数,这里相当于10进制的31

                   浮点类型(float,double)

                            $v4 = 12.34;

                            $v5 = 12.34e5;                   //12.34乘以10的5次方

         复合类型:

                   数组类型:

                   对象类型:

         特殊类型:

                   资源类型:表示某种“外界数据”的类型。

                   空类型(null):只有一个数据,就是“null”

3种字符串类型的区别:

$str1 = <span>"</span><span>abc\ndefg1</span><span>"</span>;    <span>//</span><span>双引号字符串中可以识别以下转义字符:\"  \n  \r  \t  \\  \$</span>
$str2 = <span>'</span><span>a\bc\ndefg2\\</span><span>'</span>;        <span>//</span><span>单引号字符串中只能识别以下转义字符:\'  \\。其中"\\"通常只用于最后一个字符
</span><span>//</span><span>下述为定界符字符串,其中的“ABCD”是随意命名的一个符号。字符串也就使用该符号结尾。
</span><span>//</span><span>定界符字符串中可以识别以下转义字符:\n  \r  \t  \\  \$</span>
$str3 = <<<<span>ABCD
abc\nde</span><span>"</span><span>f'g3</span>
<span>这叫做定界符字符串
这个范围的任何内容都是属于str3这个字符串的内容
换行也可以直接换
还可以写html和js等等。。。比如:
</span><hr />
<script><span>
    alert(</span><span>"</span><span>dddd</span><span>"</span><span>);
</span></script><span>
ABCD;
</span><span>//</span><span>特别注意:上述一行定界符字符串的结束符只能出现该名字本身和一个分号,不能有任何其他内容,比如空格,缩进(tab符)

</span><span>//</span><span>注意:在双引号字符串和定界符字符串中,可以直接识别变量并用该该变量的值来填充其内容。
</span><span>//</span><span>如果想要不被识别,则需要使用"\$"来将该$符号进行转义</span>
    $i = <span>5</span><span>;
    echo </span><span>"</span><span>$i=</span><span>"</span> . $i ;
Copy after login

传值方式:

         值传递(赋值传值):将一个变量的值拷贝一份,然后赋值给另一个变量,此时两个变量的值相等(相同),但两个变量是完全独立的没有关联的变量。

                   默认情况下,在php中,简单数据类型和数组以及空类型使用值传递。

用 Reference to pass (reference value): Copy one copy represented by one variable multiple representative and assign a value to another variable, which is equivalent to two variables to point (corresponding) one of the same address — this address is also the address. Represents the location of data. At this time, when the values ​​of the two variables are equal, it also means that the two variables actually refer to a common piece of data.

                                            By default, in php, object types and resource types are passed by reference.

However:

                                                                                                                                                                                                                                                                       to be passed by reference can also be used. The syntax is:

Variable 1 = & Variable 2;

                              Example:

                      $v1 = 10;

                                                                                                                                                                                                                         $v2

                    $v1++;

                        echo $v2;

Compare js:

In js, there is only the default value transfer method, and there is no artificial setting method

In js: The types of default value transfer are: numbers, characters, Boolean, two special types

值 The type of referenced value by default is: array, object

Operator:

l Arithmetic operators: + - * / % ++ --

The

+ sign only does "arithmetic operations" and has no double meaning.

    ++ --The symbol meaning and usage are exactly the same as js.
  • l is written before the variable, then: add (decrement) first, and then do other operations (such as assignment)
l is written after the variable, then: do other operations (such as assignment) first, and then add (subtract) yourself

The % sign only performs the AND operation on integers. If it is not an integer, it will be automatically converted to an integer first and then the remainder will be taken.

  • l Comparison operators: == != > >= < <= ===(all equal) !==(not all equal)

== usually means "fuzzy equality", === means exact equality (only if the data type and data value are the same)

    For various situations of fuzzy equality, please refer to the manual: Appendix〉Type Comparison Table
  • l Conditional operator: (expr1) ? (expr2) : (expr3), example:

Meaning: Evaluate and judge the expression exp1. If the judgment result is true, the result of the entire expression is exp2, otherwise the result of the entire expression is exp3

    $v1 = $fs>60 ? "Pass" : "Make-up exam" ; //Then v1 has two situations based on the value of fs
  • Supplementary knowledge: The unary operators are: !, ++, --, ~,, the binary operator is the most commonly used operator, and the ternary operator is this one.
  • l Logical operators: &&(AND) ||(OR) ! (NOT)

Logical AND &&: Only when both data are true, the result is true

    Logical OR||: As long as one data is true, the result is true
  • Logical negation! : Take the opposite value of logical data.
  • l String operator: . (i.e. English period)   .= (i.e. similar to “+=")

.= is just a "shorthand", $v1 = $v1 . "abc"; è $v1 .= "abc";

  • l Bit operators: &(AND) |(OR) ~(NOT) ^(XOR) <<(left shift) >>(right shift)

Bitwise operators are all performed on the binary system of numbers.

    Bitwise AND &: The result is 1 only when the numbers on the two corresponding bits are both 1
  • Bitwise OR|: As long as one number in the corresponding bit is 1, the result is 1
  • Bitwise NOT~: negation, that is, 1 becomes 0, and 0 becomes 1
  • Bitwise XOR^: When the numbers on the two corresponding bits are different, the result is 1
  • l Assignment operator: = += -= *= /= %= .=
Operator priority issue: basically the same as js.

Data type conversion

在js中,基本没有数据类型转换的概念,实际的转换都是“自动发生”的——默认转换。

         js中有两个“类似”类型转换的函数: parseInt(….)  parseFloat(…)

php中,既有默认转换,也有强制转换。

默认转换举例:

$v1 = <span>10</span> - “<span>3</span>”;        <span>//</span><span>7;</span>
    $v2 = <span>10</span> + “<span>3</span>”;        <span>//</span><span>13;</span>
    $v3 = <span>10</span> + “3abc”;    <span>//</span><span>13;</span>
    $v4 = <span>10</span> + “<span>3</span>.5abc”;    <span>//</span><span>13.5;</span>
    $v5 = <span>10</span> + “abc3”;    <span>//</span><span>10;</span>
    $v5 = <span>10</span> + “abc”;    <span>//</span><span>10</span>
    $v5 = “<span>10</span>” + “abc”;    <span>//</span><span>10</span>
    $v5 = “10ABC” + “5abc”;    <span>//</span><span>15</span>
    $v5 = “ABC10” + “abc5”;    <span>//</span><span>0</span>
    $v5 = “ABC” + “abc”;    <span>//</span><span>0</span>
Copy after login

………………….凡是使用算术运算符进行运算,都会“看成”数字,如果实在转不成一个数字,也会当做0来进行计算。

强制转换:

<span>语法: (要转成的目标类型)数据;
    举例:
    $v1 </span>= (<span>int</span>) “<span>3</span>.5abc”;    <span>//</span><span>3;int类型</span>
    $v1 = (<span>float</span>) “<span>3</span>.5abc”;    <span>//</span><span>3.5;    </span><span>//</span><span>这里其实是float类型</span>
    $v1 = (<span>string</span>) <span>3</span>;        <span>//</span><span>”3”;</span>
    $v1 = (<span>bool</span>) “<span>3.5</span>”;        <span>//</span><span>true</span>
Copy after login

——特别推荐将一个数据转换为bool类型的时候:查手册:附录〉类型比较表〉对变量 $x 进行比较 〉 if($x)列

以上就介绍了php基础学习笔记(12),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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