• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP快速入门

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

整理一些PHP最基本的语法,旨在快速入门。

 

PHP的print

 1 <?php
 2 
 3      # First Example
 4 
 5      print <<<END
 6 
 7      This uses the "here document" syntax to output
 8 
 9      multiple lines with $variable interpolation. Note
10 
11      that the here document terminator must appear on a
12 
13      line with just a semicolon no extra whitespace!
14 
15      END;
16      # Second Example
17 
18      print "This spans
19 
20      multiple lines. The newlines will be
21 
22      output as well";
23 
24 ?>

对于打印多行的情况,可以使用上面的2种方法;另外PHP中的注释分为单行注释和多行注释,单行注释使用“#”或C语言的注释方法,多行注释使用C语言的注释方法。

 

PHP print和echo语句

echo 和 print 区别:

  • echo - 可以输出一个或多个字符串
  • print - 只允许输出一个字符串,返回值总为 1

提示:echo 输出的速度比 print 快, echo 没有返回值,print有返回值1。

 

PHP的变量

(1)所有变量在 PHP 标有一个美元符号($)。

(2)PHP 变量没有内在类型——一个变量事先不知道是否会用于存储数字或字符串。

(3)变量在第一次赋值给它的时候被创建,PHP 可以自动地从一个类型转换成另一个类型。

(4)PHP 一共有八种数据类型可以供我们用来构造变量:

  • 整型:是整数,没有小数点,像 4195。
  • 浮点型:浮点数,如 3.14159 或 49.1。
  • 布尔值:只有两个可能值或真或假。
  • 空:是一种特殊的类型只有一个值:空。
  • 字符串类型:字符序列,像\'PHP 支持字符串操作\'
  • 数组:有命名和索引所有值的集合。
  • 对象:是程序员定义类的实例化,可以打包其他类型的值和属于这个类的函数。
     1 <?php
     2 class Car
     3 {
     4     var $color;
     5     function Car($color="green") {
     6       $this->color = $color;
     7     }
     8     function what_color() {
     9       return $this->color;
    10     }
    11 }
    12 
    13 function print_vars($obj) {
    14    foreach (get_object_vars($obj) as $prop => $val) {
    15      echo "\t$prop = $val\n";
    16    }
    17 }
    18 
    19 // instantiate one object
    20 $herbie = new Car("white");
    21 
    22 // show herbie properties
    23 echo "\herbie: Properties\n";
    24 print_vars($herbie);
    25 
    26 ?>  

    运行结果:

    \herbie: Properties
            color = white
      

    get_object_var($object),返回一个数组。获取$object对象中的属性,组成一个数组

  • 资源:特殊变量持有引用外部资源到 PHP(如数据库连接)。

 

PHP的变量赋值多行内容

也可以用上面类似的方法给变量赋值多行的语句内容值。

 1 <?php
 2 
 3 $channel =<<<_XML_
 4     <channel>
 5     <title>What\'s For Dinner<title>
 6     <link>http://menu.example.com/<link>
 7     <description>Choose what to eat tonight.</description>
 8     </channel>
 9 _XML_;
10 
11 echo <<<END
12     This uses the "here document" syntax to output
13     multiple lines with variable interpolation. Note
14     that the here document terminator must appear on a
15     line with just a semicolon. no extra whitespace!
16     <br />
17 END;
18 
19 print $channel;
20 ?>

运行结果:

    This uses the "here document" syntax to output
    multiple lines with variable interpolation. Note
    that the here document terminator must appear on a
    line with just a semicolon. no extra whitespace!

    <channel>
    <title>What\'s For Dinner<title>
    <link>http://menu.example.com/<link>
    <description>Choose what to eat tonight.</description>

注意tab也是生效了的。

 

全局变量和静态变量

(1)全局变量:通过将关键字 GLOBAL 放在变量前该变量可以被确认为全局变量。

$a 变量在函数外定义,无法在函数内使用,如果要在一个函数中访问一个全局变量,需要使用 global 关键字。该关键字用于函数内访问全局变量。

在函数内调用函数外定义的全局变量,我们需要在函数中的变量前加上 global 关键字

 1 <?php
 2 
 3 $a = 22;
 4 
 5 function change()
 6 {
 7         GLOBAL $a;
 8         $a += 2;
 9 }
10 
11 change();
12 
13 print $a;
14 ?>

运行结果:

24

PHP 将所有全局变量存储在一个名为 $GLOBALS[index] 的数组中。 index 保存变量的名称。这个数组可以在函数内部访问,也可以直接用来更新全局变量。

上面的代码也可以写为下面这种形式:

 1 <?php
 2 
 3 $a = 22;
 4 
 5 function change()
 6 {
 7         $GLOBALS[\'a\'] += 2;
 8 }
 9 
10 change();
11 
12 print $a;
13 ?>

(2)静态变量:当一个函数完成时,它的所有变量通常都会被删除。然而,有时候希望某个局部变量不要被删除。要做到这一点,在第一次声明变量时使用 static 关键字

 1 <?php
 2 
 3 function change()
 4 {
 5         STATIC $a = 0;
 6         $a += 2;
 7         print $a;
 8         print "\n";
 9 }
10 
11 change();
12 change();
13 change();
14 
15 ?>

运行结果:

2
4
6

 

常量

常量和变量之间的区别:

  • 常量前面没有必要写美元符号($),在变量前必须编写一个美元符号。
  • 常量只能用 define() 函数定义,而不能通过简单的赋值语句。
  • 常量可以不用理会变量范围的规则而在任何地方定义和访问。
  • 常量一旦定义就不能被重新定义或者取消定义。
  • 可以使用函数 constant()读取一个常数的值。
 1 <?php
 2 
 3 define("MINSIZE", 50);
 4 
 5 echo MINSIZE;
 6 echo "\n";
 7 
 8 echo constant("MINSIZE"); // same thing as the previous line
 9 echo "\n";
10 
11 $value = constant("MINSIZE"); 
12 print $value;
13 echo "\n";
14 ?>

运行结果:

50
50
50

 

PHP7+ 版本新增整除运算符 intdiv()

1 <?php
2 
3 var_dump(intdiv(10, 3));
4 
5 echo intdiv(10, 3);
6 
7 ?>

运行结果:

int(3)
3

 

PHP比较运算符

除了下表列出的,其余的和C语言类似,不再赘述

运算符名称描述实例
x == y 等于 如果 x 等于 y,则返回 true 5==8 返回 false
x === y 绝对等于 如果 x 等于 y,且它们类型相同,则返回 true 5==="5" 返回 false
x != y 不等于 如果 x 不等于 y,则返回 true 5!=8 返回 true
x <> y 不等于 如果 x 不等于 y,则返回 true 5<>8 返回 true
x !== y 绝对不等于 如果 x 不等于 y,或它们类型不相同,则返回 true 5!=="5" 返回 true

 

结构控制

有2种方式:if和switch,和C语言类似。

 1 <?php
 2 
 3 $d = date("D");
 4 if ($d == "Fri")
 5         echo "Have a nice weekend!\n"; 
 6 elseif ($d == "Sun")
 7         echo "Fuck!\n"; 
 8 else
 9         echo "Have a nice day!\n"; 
10 
11 ?>

运行结果:

Fuck!

 

循环类型

有4种方式:for、while、do...while、foreach,前面3种和C语言中类似,不再赘述了。

foreach 语句用于循环遍历数组。每进行一次循环,当前数组元素的值就会被赋值给 value 变量,数组指针会逐一地移动以此类推。

1 <?php
2 
3 $array = array( 1, 2, 3, 4, 5);
4 foreach( $array as $value )
5 {
6         echo "Value is $value \n";
7 }
8 
9 ?>

运行结果:

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5

 

数组

有三种不同类型的数组,每一个数组的值可以通过一个被称为数组索引 ID 键来访问。

(1)数字数组:也成为索引数组,数组以一个数字作为索引。值在线性中存储和访问。这些数组可以存储数字、字符串和任何对象但是他们将数字作为索引。默认情况下,数组索引从 0 开始。

 1 <?php
 2 
 3 /* First method to create array. */
 4 $numbers = array( 1, 2, 3, 4, 5);
 5 foreach( $numbers as $value )
 6 {
 7         echo "Value is $value \n";
 8 }
 9 echo "------------------------- \n";
10 
11 /* Second method to create array. */
12 $numbers[0] = "one";
13 $numbers[1] = "two";
14 $numbers[2] = "three";
15 foreach( $numbers as $value )
16 {
17         echo "Value is $value \n";
18 }
19 echo "------------------------- \n";
20 
21 /* Third method to create array. */
22 $aa[0] = "aa0";
23 $aa[1] = "aa1";
24 $aa[2] = "aa2";
25 foreach( $aa as $value )
26 {
27         echo "Value is $value \n";
28 }
29 
30 ?>

运行结果:

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
------------------------- 
Value is one 
Value is two 
Value is three 
Value is 4 
Value is 5 
------------------------- 
Value is aa0 
Value is aa1 
Value is aa2

通过count()可以获取数组长度,并且用这个可以更方便的遍历数组

 1 <?php
 2 $cars = array("Volvo", "BMW", "Toyota");
 3 $arrlength = count($cars);
 4  
 5 for($x = 0; $x < $arrlength; $x++)
 6 {
 7     echo $cars[$x];
 8     echo "<br>";
 9 }
10 ?>

运行结果:

Volvo
BMW
Toyota

(2)关联数组:数组以字符串作为索引。这个数组存储元素值与键值不是一个严格的线性索引顺序。数值数组和关联数组功能非常的相似,他们只是有不同的索引。关联数组将字符串作为索引,这样就可以建立一个强大的键和值的结构体系。注意: 不要使关联数组在双引号内,否则打印它不会返回任何值。

可以将员工的工资存储在一个数组,用数字索引定义数组并不是最好的选择。相反,我们可以使用员工的名字作为关联数组的键,将工资作为键的值。

 1 <?php
 2 function print_fun($salaries)
 3 {
 4         echo "Salary of mohammad is ". $salaries[\'mohammad\'] . "\n";
 5         echo "Salary of qadir is ".  $salaries[\'qadir\']. "\n";
 6         echo "Salary of zara is ".  $salaries[\'zara\']. "\n";
 7 }
 8 
 9 function print_foreach($salaries)
10 {
11         var_dump($salaries);
12         foreach ($salaries as $value)
13         {
14                 var_dump($value);
15                 echo $value. "\n";
16         }
17 }
18 
19 /* First method to associate create array. */
20 $salaries = array( 
21                 "mohammad" => 2000, 
22                 "qadir" => 1000, 
23                 "zara" => 500
24                 );
25 print_fun($salaries);
26 
27 /* Second method to create array. */
28 $salaries[\'mohammad\'] = "high";
29 $salaries[\'qadir\'] = "medium";
30 $salaries[\'zara\'] = "low";
31 print_fun($salaries);
32 
33 /* third method to create array. */
34 $salaries[\'mohammad\'] = 1;
35 $salaries[\'qadir\'] = 2;
36 $salaries[\'zara\'] = 3;
37 print_foreach($salaries);
38 ?>

运行结果:

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
array(3) {
  ["mohammad"]=>
  int(1)
  ["qadir"]=>
  int(2)
  ["zara"]=>
  int(3)
}
int(1)
1
int(2)
2
int(3)
3

上面这种遍历方法很挫,可以用下面的方法来搞

 1 <?php
 2 function print_fun($salaries)
 3 {
 4         echo "Salary of mohammad is ". $salaries[\'mohammad\'] . "\n";
 5         echo "Salary of qadir is ".  $salaries[\'qadir\']. "\n";
 6         echo "Salary of zara is ".  $salaries[\'zara\']. "\n";
 7 }
 8 
 9 function print_foreach($salaries)
10 {
11         var_dump($salaries);
12         foreach ($salaries as $key => $value)
13         {
14                 var_dump($key);
15                 var_dump($value);
16                 echo "Key=" . $key . ", Value=" . $value. "\n";
17         }
18 }
19 
20 /* First method to associate create array. */
21 $salaries = array( 
22                 "mohammad" => 2000, 
23                 "qadir" => 1000, 
24                 "zara" => 500
25                 );
26 print_fun($salaries);
27 
28 /* Second method to create array. */
29 $salaries[\'mohammad\'] = "high";
30 $salaries[\'qadir\'] = "medium";
31 $salaries[\'zara\'] = "low";
32 print_fun($salaries);
33 
34 /* third method to create array. */
35 $salaries[\'mohammad\'] = 1;
36 $salaries[\'qadir\'] = 2;
37 $salaries[\'zara\'] = 3;
38 print_foreach($salaries);
39 ?>

运行结果:

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
array(3) {
 ["mohammad"]=>
 int(1)
 ["qadir"]=>
 int(2)
 ["zara"]=>
 int(3)
}
string(8) "mohammad"
int(1)
Key=mohammad, Value=1
string(5) "qadir"
int(2)
Key=qadir, Value=2
string(4) "zara"
int(3)
Key=zara, Value=3

(3)多维数组:包含一个或多个数组,数组值可以使用多个索引访问。在多维数组中,主数组中的每个元素也是一个数组。在子数组中的每个元素也可以是数组等等,多维数组中的值是使用多个索引访问。

 1 <?php
 2 
 3 function print_foreach($marks)
 4 {
 5         var_dump($marks);
 6         echo "================================\n";
 7         foreach ($marks as $value)
 8         {
 9                 var_dump($value);
10                 echo "------------------------------\n";
11                 foreach ($value as $value_inner)
12                 {
13                         echo $value_inner. "\n";
14                 }
15                 echo "\n\n";
16         }
17 }
18 
19 $marks = array( 
20                 "mohammad" => array
21                 (
22                  "physics" => 35,
23                  "maths" => 30,
24                  "chemistry" => 3
25                 ),
26                 "qadir" => array
27                 (
28                  "physics" => 30,
29                  "maths" => 32,
30                  "chemistry" => 29
31                 ),
32                 "zara" => array
33                 (
34                  "physics" => 31,
35                  "maths" => 22,
36                  "chemistry" => 39
37                 )
38               );
39 /* Accessing multi-dimensional array values */
40 echo "Marks for mohammad in physics : " ;
41 echo $marks[\'mohammad\'][\'physics\'] . "\n"; 
42 echo "Marks for qadir in maths : ";
43 echo $marks[\'qadir\'][\'maths\'] . "\n"; 
44 echo "Marks for zara in chemistry : " ;
45 echo $marks[\'zara\'][\'chemistry\'] . "\n"; 
46 
47 print_foreach($marks);
48 
49 ?>

运行结果:

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
array(3) {
  ["mohammad"]=>
  array(3) {
    ["physics"]=>
    int(35)
    ["maths"]=>
    int(30)
    ["chemistry"]=>
    int(3)
  }
  ["qadir"]=>
  array(3) {
    ["physics"]=>
    int(30)
    ["maths"]=>
    int(32)
    ["chemistry"]=>
    int(29)
  }
  ["zara"]=>
  array(3) {
    ["physics"]=>
    int(31)
    ["maths"]=>
    int(22)
    ["chemistry"]=>
    int(39)
  }
}
================================
array(3) {
  ["physics"]=>
  int(35)
  ["maths"]=>
  int(30)
  ["chemistry"]=>
  int(3)
}
------------------------------
35
30
3


array(3) {
  ["physics"]=>
  int(30)
  ["maths"]=>
  int(32)
  ["chemistry"]=>
  int(29)
}
------------------------------
30
32
29


array(3) {
  ["physics"]=>
  int(31)
  ["maths"]=>
  int(22)
  ["chemistry"]=>
  int(39)
}
------------------------------
31
22
39

重新使用遍历方法:

 1 <?php
 2 
 3 function print_foreach($marks)
 4 {
 5         var_dump($marks);
 6         echo "================================\n";
 7         foreach ($marks as $value_outer)
 8         {
 9                 var_dump($value_outer);
10                 echo "------------------------------\n";
11                 foreach ($value_outer as $key => $value_inner)
12                 {
13                         echo "key = " . $key . ", value = " . $value_inner. "\n";
14                 }
15                 echo "\n\n";
16         }
17 }
18 
19 $marks = array( 
20                 "mohammad" => array
21                 (
22                  "physics" => 35,
23                  "maths" => 30,
24                  "chemistry" => 3
25                 ),
26                 "qadir" => array
27                 (
28                  "physics" => 30,
29                  "maths" => 32,
30                  "chemistry" => 29
31                 ),
32                 "zara" => array
33                 (
34                  "physics" => 31,
35                  "maths" => 22,
36                  "chemistry" => 39
37                 )
38               );
39 /* Accessing multi-dimensional array values */
40 echo "Marks for mohammad in physics : " ;
41 echo $marks[\'mohammad\'][\'physics\'] . "\n"; 
42 echo "Marks for qadir in maths : ";
43 echo $marks[\'qadir\'][\'maths\'] . "\n"; 
44 echo "Marks for zara in chemistry : " ;
45 echo $marks[\'zara\'][\'chemistry\'] . "\n"; 
46 
47 print_foreach($marks);
48 
49 ?>

运行结果:

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
array(3) {
 ["mohammad"]=>
 array(3) {
   ["physics"]=>
   int(35)
   ["maths"]=>
   int(30)
   ["chemistry"]=>
   int(3)
 }
 ["qadir"]=>
 array(3) {
   ["physics"]=>
   int(30)
   ["maths"]=>
   int(32)
   ["chemistry"]=>
   int(29)
 }
 ["zara"]=>
 array(3) {
   ["physics"]=>
   int(31)
   ["maths"]=>
   int(22)
   ["chemistry"]=>
   int(39)
 }
}
================================
array(3) {
 ["physics"]=>
 int(35)
 ["maths"]=>
 int(30)
 ["chemistry"]=>
 int(3)
}
------------------------------
key = physics, value = 35
key = maths, value = 30
key = chemistry, value = 3
array(3) {
 ["physics"]=>
 int(30)
 ["maths"]=>
 int(32)
 ["chemistry"]=>
 int(29)
}
------------------------------
key = physics, value = 30
key = maths, value = 32
key = chemistry, value = 29
array(3) {
 ["physics"]=>
 int(31)
 ["maths"]=>
 int(22)
 ["chemistry"]=>
 int(39)
}
------------------------------
key = physics, value = 31
key = maths, value = 22
key = chemistry, value = 39

 

PHP数组排序函数

  • sort() - 对数组进行升序排列
  • rsort() - 对数组进行降序排列
  • asort() - 根据关联数组的值,对数组进行升序排列
  • ksort() - 根据关联数组的键,对数组进行升序排列
  • arsort() - 根据关联数组的值,对数组进行降序排列
  • krsort() - 根据关联数组的键,对数组进行降序排列

 

字符串

单引号串和双引号串在 PHP 中的处理是不相同的。双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符。

(1)字符串并置运算符:要把两个变量连接在一起,请使用这个点运算符 (.)

(2)使用 strlen() 函数:strlen() 函数用于计算字符串的长度。

(3)使用 strpos() 函数:strpos() 函数用于在字符串内检索一段字符串或一个字符。如果在字符串中找到匹配,该函数会返回第一个匹配的位置。如果未找到匹配,则返回 FALSE。

 1 <?php
 2 
 3 $txt1="Hello World" 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
HTML中利用JS调用PHP (以登录为例)发布时间:2022-07-10
下一篇:
php免杀木马的思路发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap