<?<span>php </span><span>//</span><span>这个是php单行注释</span> <span>/*</span><span> 这是多行注释 </span><span>*/</span> <span>//</span><span>每条php语句使用英文分号结束——必须使用! </span><span>//</span><span>php是一门区分大小写的语言——但函数名除外 </span><span>//</span><span>php的任何一个变量名都要使用一个$符开头。</span> ?>
php中,变量使用一个$符号开头。变量通常是直接赋值(此时如果是第一次赋值也就表示定义)。
常量:设定一个其值不会也不应该改变的数据标识符(数据名字),比如数学上的“pi”。
$pi = 3.14; //这是变量
define(“PI”, 3.14); //这是定义一个常量,名字为“PI”,值为3.14,这里也就类似赋值
常量定义的同时也就是赋值,且只是这一次性的!
常量和变量的区别:
基本类型(标量类型):
布尔类型(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”
$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 ;
值传递(赋值传值):将一个变量的值拷贝一份,然后赋值给另一个变量,此时两个变量的值相等(相同),但两个变量是完全独立的没有关联的变量。
默认情况下,在php中,简单数据类型和数组以及空类型使用值传递。
Pass by reference (pass by value): Copy the "address" represented by one variable and assign it to another variable, which is equivalent to two variables pointing to (corresponding to) the same address at the same time - this address It also represents the location of the 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:
We can also artificially transfer the data that is passed by assignment by default and use reference to pass by value. The syntax is:
Variable 1 = & Variable 2;
Example:
$v1 = 10;
$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 passed by default values are: numbers, characters, Boolean, two special types
The default reference-passed value types are: array, object
Operator:
The
l is written after the variable, then: do other operations (such as assignment) first, and then add (subtract) itself
Operator priority issue: basically the same as js.
在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>
………………….凡是使用算术运算符进行运算,都会“看成”数字,如果实在转不成一个数字,也会当做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>
——特别推荐将一个数据转换为bool类型的时候:查手册:附录〉类型比较表〉对变量 $x 进行比较 〉 if($x)列