Points of PHP

Syntax

PHP Code

1
2
3
<?php
...
?>

Note All statements end with a semicolon ;
Note Everything except variable names are NOT case sensitive.

Comments

// and # are used for single-line comment
/* ... */ can be used for multi-line comment

Variables

Usage

$variable_name = "anything"

Note: PHP is a loosely typed language

Scope

  • local
  • global
  • static

Global

global keyword can be used to make variable global.
$GLOBALs['index'] can be used to access global variables.

Static

Static variable is local. It won’t be deleted when function completed.
static keyword can be used to make variable static.

Constant

1
2
define("SHI", "Hello world!"[, case_insensitive = false]);
echo SHI;

Data Type

var_dump($x); can be used to show the type of a variable.

String

Variables can be integrated directly into strings:

1
echo "<h1>$txt</h1>";

Note: echo and print can be used with or without parentheses

. can be used to concatenate strings.
.= to append.

Array

1
2
3
4
5
6
7
8
$a = array(1, 2, 3);
$a[0];

$b = array("a"=>1, "b"=>2, "c"=>3);
$b['a'];
foreach($b as $bi => $bi_value) {
echo "Key=" . $bi . ", Value=" . $bi_value;
}

Function

1
2
3
4
function fu($a, $b=0) {
...
return 0
}

Object

1
2
3
4
5
6
7
8
9
10
class Bi {
function Bi(){
$this->shi = "shi";
}
}

// Create an object
$b = new Bi();

echo $b->shi;

Condition Statement

If

1
2
3
4
5
6
7
if() {
...
} elseif() {
...
} else{
...
}

Switch

1
2
3
4
5
6
7
8
switch (n) {
case label1:
...
break;
...
default:
...
}

Loops

While

1
2
3
4
5
6
7
while() {
...
}

do {
...
} while();

For

1
2
3
4
5
6
7
for(...;...;...) {
...
}

for ($i as $arr) {
...
}

Skills

Redirection

1
2
header("Location: ".$url, true, $permanent ? 301 : 302);
die();