Learnd from Codecademy
Several functions
- strlen
$length = strlen($example);
- substr
$subString = substr($example, 0, 4); // 0 is starting position; 4 is length
- strtoupper
$uppercase = strtoupper($example);
- strtolower the same with 3
- strpos
$position = strpos($example, "q");
- M_PI a PHP constant that is equal to pi
- round
$integerNum = round($floatNum);
$twoDecimalPlaces = round($floatNum, 2);
- rand
print rand(); // prints a number between 0 and 32767
print rand(1, 10); // prints a number between 1 and 10
- array
$anArray = array();
- array_push
array_push($anArray, "item");
- count
count($anArray); // return the number of elements of the array
- sort
sort($anArray); // sequense from small to big
- rsort
rsort($anArray); // sequense from big to small
- join
join(",", $anArray); // make an array into a string, splited by ","
Object Oriented PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html>
<head>
<title> Introduction to Object-Oriented Programming </title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<?php
// The code below creates the class
class Person {
// Creating some properties (variables tied to an object)
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
// Assigning the values
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
// Creating a method (function tied to an object)
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}
// Creating a new person called "boring 12345", who is 12345 years old ;-)
$me = new Person('boring', '12345', 12345);
// Printing out, what the greet method returns
echo $me->greet();
?>
</p>
</body>
</html>
- Create a new class
class Person {}
- Construct an instance of the class
$instance = new Person();
- print a public property from a class
1
2
3
4
5
6
7
class Person {
public $theValue = true;
}
$example = new Person();
print $example->theValue; // true
- methods: functions bundled into objects
1
2
3
4
5
6
7
8
class Person {
public function __construct($para1, $para2) {
this->num1 = $para1;
this->num2 = $para2;
}
}
$obj = new Person("one", "two");
print $obj->num1; // one
OOP PHP
1
2
3
4
5
6
7
8
9
class Person {
function __construct($name) {
$this->name = $name;
}
public function dance() {
return "I'm dancing!";
}
}
is_a()
: find out if a particular object is an instance of a given class
1
2
3
if (is_a($me, "Person")) {
echo "I'm a person";
}
property_exists()
: if an object has a given property
1
2
3
if (property_exists($me, "name")) {
echo "I have a name";
}
method_exists()
: if an object has a given method
1
2
3
if (method_exists($me, "dance")) {
echo "I know how to dance";
}
- inheritance
1
2
3
4
5
6
class Shape {
public $hasSides = true;
}
class Square extends Shape {
}
- override and the final word
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Vehicle {
final public function honk() {
return "HONK HONK!";
}
}
class Bicycle extends Vehicle {
public function honk() {
return "Beep beep!";
}
}
$bicycle = new Bicycle();
echo $bicycle->honk(); // Cannot override final method Vehicle::honk()
?>
const
is short for constant, which is not allowed to change
1
2
3
4
class Immortal extends Person {
// Immortals never die!
const alive = true;
}
- Scope Resolution 范围解析符 ::
1
2
3
4
5
class Person {
const life = "MAXIMUM";
}
echo Person::life; //MAXIMUM
static
is similar withconst
, but it can be a function for a class
arrays
$anArray = array(0, 'apple', '333');
echo $anArray[0]; // 0
- Associative Arrays
1
2
3
4
$anotherArray = array('color' => 'blue',
'sex' => 'male',
'year' => 2016);
echo $anotherArray['color']; // blue
- Iterating over Associative Arrays
1
2
3
4
5
6
7
$salad = array('lettuce' => 'with',
'tomato' => 'without',
'onions' => 'with');
foreach ($salad as $key => $value) {
echo $value . ' ' . $key; // with lettuce without tomato with onions
}
- Multidimensional Arrays
1
2
3
4
5
6
$deck = array(array('2 of Diamonds', 2),
array('5 of Diamonds', 5),
array('7 of Diamonds', 7));
echo 'You have the ' . $deck[2][1] . '!'; // You have the 7!
echo 'You have the ' . $deck[2] . '!'; // You have the Array!