-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.php
More file actions
169 lines (130 loc) · 4.17 KB
/
Copy pathphp.php
File metadata and controls
169 lines (130 loc) · 4.17 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
// PHP 5 syntax
// single-line comment
# also a single-line comment
/* multiple-lines comment block */
# all key-words, functions and classes are NOT case-sensitive
# all variable names are case-sensitive
# basic data types, PHP does not declare variable data types
$my_string = "a string";
$my_int = 10;
$my_float = 12;
$my_bool = true;
$my_array = array('first', 'second', 'third');
$my_null = null;
# PHP will parse a variable in a string
$txt = "W3Schools.com";
# output: I love W3Schools.com!
echo "I love $txt!";
# is equivalent to the line below
echo "I love " . $txt . "!";
# scopes
# a function is NOT allowed to access a global variable
$x = 5;
function my_test() {
# cannot access x here, this will rise an error
# echo $x;
# use GLOBALS array to access/mutate global variable
echo $GLOBALS['x'];
# static variable will store information cross function calls
# the example below will remember how many times it's called
# note: static variable is still local
static $function_called = 0;
$function_called++;
echo $function_called;
}
my_test();
# class and object
class Car {
# constructor
function Car() {
$this->model = "BMW";
}
}
# create an object
$my_car = new Car();
# access a property
echo $my_car->model;
# string functions
strlen("123");
# count number of words
str_word_count("Hello world!"); // outputs 2
# reverse a string
strrev("123");
# serach a word
# return the index of the beginning of the word
# return false if word is not found
echo strpos("Hello world!", "world"); // outputs 6
# replace a word in a string
str_replace("replaced word", "new word", "base string");
str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
# complete reference
# https://www.w3schools.com/php/php_ref_string.asp
# constants
# all constants are global
define("var_name", "value"); // var name is case-sensitive
define("var_name2", "value", true); // var name is case-insensitive
# logical operators
# PHP supports xor
# and === &&
# or === ||
# use ! to negate a value
# string operators
# to concatenate two strings
# use . and .=
# "123" . "456" outputs "123456"
# $txt1 .= $txt2
# foreach loop
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
# define functions
# last_name is an optional parameter
function full_name($first_name, $last_name = "Smith") {
return "$first_name $last_name";
}
echo full_name("John", "Smith");
# arrays
# indexed array
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars); // return the length of this array
# for loop
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
# associative array
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
foreach($age as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
# complete array reference
# https://www.w3schools.com/php/php_ref_array.asp
# superglobals
# Returns the filename of the currently executing script
echo $_SERVER['PHP_SELF']; //
# Returns the version of the Common Gateway Interface (CGI) the server is using
echo $_SERVER['SERVER_NAME'];
# complte reference
# https://www.w3schools.com/php/php_superglobals.asp
# submit and handle form request
/*
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
*/
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
$json = array('abc'=>'123', 'ac'=>'adc');
?>