PHP Basics

Author: Joyce_BY, all rights reserved.
Contact by email: [email protected]


Start

php scripts start by: <?php
end by: ?>
include files like in C/C++;
all contents end with ‘;’

<?php
// code
?>
comment

same as in C/C++
single line: //
multiple lines: /**/
 

Variables

start by: $
php是弱类型语言,命名规则参照C/Python
function field: local, global, static, parameter

$x = 10
output - echo and print

echo: can print multiple strings;, no return value
print: can only print one string, return value 1
both can output variables

<?php
$txt = "test";
$cars = array{"Volvo","BMW"};
echo "this is echo<br>", "$txt<br>","{$cars[0]}";
print "only one $txt string";
?>
EOF(heredoc)
  1. start by: ‘<<< EOF’
    end by: ‘EOF;’ and a blank line
  2. .PHP 定界符 EOF 的作用就是按照原样输出内容;
  3. ‘EOF’ can change to other marks such as ‘EOT\EOD’, but must make sure that the mark does not appear in text.
  4. In heredoc, variables don’t need to connect by ‘.’ or comma. Only variables but FUNCTIONS can be interpreted between marks.
<?php
echo <<< EOF 
	<h1> first header </h1>
	<p> first paragraph</p>
EOF;
?>
// a single line without blank space
data type
  1. variables
    var_dump() 函数返回变量的数据类型和值
    string, 整型, 浮点型, bool(true, false), array, class, null

  2. constants
    常量是global的,常量名不需要加 $ 修饰符
    use define() to set a constant

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] );
// 该函数有三个参数:
// name[必选参数]:名称
// value[必选参数]: 常量的值。
// case_insensitive[可选参数: TRUE-大小写不敏感; 默认false,大小写敏感。
  1. superglobals
    超级全局变量在一个脚本的全部作用域中都可用,不需要特别说明
    ($GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION)

  2. 魔术常量
    PHP 向它运行的任何脚本提供了大量的预定义常量,很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现。
    有八个魔术常量它们的值随着它们在代码中的位置改变而改变:

__LINE__ //文件中的当前行号
__FILE__ // 文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名
__DIR__ // 文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录
__FUNCTION__ // 函数名称
__CLASS__ // 类的名称
__TRAIT__ // Trait 的名字,Trait 名包括其被声明的作用区域
__METHOD__ // 类的方法名
__NAMESPACE__ // 当前命名空间的名称(区分大小写
  1. string
    给文本加单引号或双引号
    唯一的并置运算符 ‘.’ 连接两个string 变量
    functions
    strlen(str) returns the length of the testing string
    strpos(str, target) returns the place where target matches the str, if no match returns false.
    find more in string handbook
<?php 
$t1 = 'hello world!';
$t2 = 'what a nice day!'; 
echo $t1.' '.$t2
echo strlen("Hello world!");
echo strpos("Hello world!","world");  // output will be 6
?>
  1. array
    count() returns the length of the array
    数值数组
$cars=array("Volvo","BMW","Toyota");
// 遍历
for($x=0;$x<$arrlength;$x++)

关联数组

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
// or:
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
// 遍历
foreach($age as $x=>$x_value)

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

For more array functions

运算符

算术运算符(+ - * / %)
递增/递减运算符(++ --)
比较运算符(> < == !=…)
逻辑运算符( and && or || xor !)
数组运算符(+ != …)
三元运算符( a ? b : c )
NULL 合并运算符 ?? (PHP7+)
组合比较符<=> (PHP7+)

// 比较运算符,也可用在数组中
===// 绝对等于,value and type are same
!== // 绝对不等于,value or type is different
<> // same as !=

//NULL 合并运算符 ??
$username = $_GET['user'] ?? 'nobody';
// 如果 $_GET['user'] 不存在返回 'nobody',否则返回 $_GET['user'] 的值

// 组合比较符<=>
$c = $a <=> $b;
/* 如果$a > $b, $c 的值为1
	如果$a == $b, $c 的值为0
	如果$a < $b, $c 的值为-1
*/
条件语句

if ()
if()…else
if()…elseif()…else
switch()-case :-default:

循环

while
do…while
for
foreach ( arr as value ) // similar to python “for value in arr:”

functions

grammar: function func_name() {}

namespace
  1. Grammar
    命名空间必须是程序脚本的第一条语句,所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前,例如<html>等;
    将命名空间使用大括号括起来;将全局的非命名空间中的代码与命名空间中的代码组合在一起,只能使用大括号形式的语法,全局代码必须用一个不带名称的 namespace 语句加上大括号括起来。
<?php 
namespace MySpace {
// code in MySpace namespace1;
}
namespace {  // global
// code in MySpace namespace2;
}
?>

在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句。

declare(encoding='UTF-8'); //定义多个命名空间和不包含在命名空间中的代码
  1. Subspace 子命名空间
    命名空间的名字可以使用分层次的方式定义,与目录和文件的关系很像。
<?php
namespace MySpace\Sub\Level
?>
  1. Using
  • 非限定名称,或不包含前缀的类名称

$a=new foo(); //OR
foo::staticmethod();

  • 限定名称,或包含前缀的名称

$a = new subnamespace\foo(); // OR
subnamespace\foo::staticmethod();

  • 完全限定名称,或包含了全局前缀操作符的名称

$a = new \currentnamespace\foo(); // OR
\currentnamespace\foo::staticmethod();

访问任意Global classes, functions or constants,都可以使用完全限定名称,例如 \strlen() 或 \Exception 或 \INI_ALL

OOP
  1. class的定义和调用与C++类似,拥有以下一些特点:
    $this pointer
    __construct($args) with new (子类构造方法不能自动调用父类的构造方法)
    __destruct(void)
  2. Inherentance
    use ‘extends’

class Child_Site extends Site

  1. Access Control
    public, protected, private
  2. Interface 接口
    通过 interface 关键字定义,类似定义一个,但其中定义所有的方法都是空的;
    接口中定义的所有方法都必须是公有的
    使用 implements 操作符实现一个接口;类中必须实现接口中定义的所有方法,否则会报一个致命错误;类可以实现多个接口,用逗号来分隔多个接口的名称。
// 声明一个'iTemplate'接口
interface iTemplate {...}
// 实现接口
class Template implements iTemplate {...}
  1. Abstract Class
    So long as one method in the class is declared as abstract, then this class is abstract, thus can’t be instantiated.
    Abstract methods only declare parameters, no detailed function implementations.
    When inheriting an abstract class, the child classes must define all abstract methods in its parent(s). In addition, the access control of abtract methods must be the same or looser than in the parent class.
  2. keyword - static
    If we declare class attributes or methods as static, we could access them without instantiating;
    $this cannot be used in static methods;
    static attributes cannot be accessed by operator ‘->’.
  3. keyword - final
    if a method in parent class is declared as final, then this method cannot be covered in the child classes;
    If a class is declared as final, then this class cannot be inherited.

猜你喜欢

转载自blog.csdn.net/qq_35649764/article/details/82953947
今日推荐