Machine Translated by Google
PHP course
,Machine Translated by Google
Contents
1 Introduction................................................ .................................................. ..................................... 7
1.1 Definition........ .................................................. .................................................. .....................7
1.2 History............................... .................................................. .................................................. .....7
1.3 Practical use .................................................. .................................................. .........................
7 1.4 Static pages vs dynamic pages ............................... .................................................. ..................
8 1.5 CGI Scripts............................... .................................................. .................................................. ..
8 1.6 Dynamic pages and PHP............................................... .................................................. ..............8
1.7 Why prefer PHP to other languages such as ASP?........................................... ..................8 1.8
What you should already know ............................... .................................................. ..................9
1.9 The necessary server.......................... .................................................. .....................................
9 1.10 The customer kit........ .................................................. .................................................. ......
9 1.11 Compliance with standards .................................. .................................................. ...................9
2 Basics:Presentation.......................... .................................................. ..............................................11
2.0.1 Basic syntax.................................................. .................................................. ............... 11
2.1 Integrating with HTML............................... .................................................. ..................................11
2.2 Instruction separator......... .................................................. ..................................................12
2.3 Instruction block................................................ .................................................. ..................12
2.4 Comments........................... .................................................. ............................................ 12 3
Working with variables................................................ .................................................. ........ 13 3.1
Declaring a variable .................................. .................................................. ........................... 13 3.2
Scope of variables .............. .................................................. ........................................... 13 3.3
Predefined variables.... ..................................... .................................................. .................. 14 3.4
Dynamic variables .................................. .................................................. ..................................15
3.5 Types of variables .................. .................................................. .................................................. .....15
3.5.1 Booleans.................................... .................................................. ................................. 15
3.5.2 Integers............ .................................................. .................................................. ..............
15 3.5.3 Floating point .............................. .................................................. ...............................
16 3.5.4 Character strings .............. .................................................. ........................................ 16
3.5.5 Tables... .................................................. ..................... ........................................... 17 3.5.6
The object variable .................................................. .................................................. ...........18
3.6 Constants.................................... .................................................. ..................................... 18 3.7
Obtaining the type of a variable.... .................................................. ............................................ 19
3.8 Defining and deleting a variable................................................. ........................................... 19 4
Operators... .................................................. .................................................. ...........................20 4.1
The precedence of operators .................. .................................................. ............................... 20
4.2 Arithmetic operators ............................... .................................................. ........................... ..............
20 4.3 Assignment operators .................................. .................................................. .........................
20 4.4 Bitwise Operators............................... .................................................. ...........................................20
4.5 Comparison operators.............. .................................................. .................................................21
4.6 Error operator.................................................. .................................................. .................. 21
4.7 Execution operator............................... .................................................. .................................. 21
4.8 Increment/Decrement Operators .................. .................................................. ..................21 4.9
Logical operators............................... .................................................. .................................. 21 4.10
String Operators .................................................. .................................................. ........ 22 4.11
Array operator.................................... .................................................. ..................... 22 5 The notion
of expression .................. .................................................. ...........................................23 6 The
structures of control................................................. .................................................. ............. 25 6.1
if.................................. .................................................. .................................................. .........25 6.2
while...................................... .................................................. .................................................25
S. ROHAUT PHP Programming Course Page 2/93
,Machine Translated by Google
6.2.1 classic while........................................... .................................................. .................. 25
6.2.2 do ... while....................... .................................................. ...............................................25
6.3 for .................................................. .................................................. ........................................26
6.4 foreach...... .................................................. .................................................. ...........................
26 6.5 break and continue.................... .................................................. ..................................................
27 6.6 switches............................................. .................................................. ..................................27
6.7 return......... .................................................. .................................................. ............. ..............28
6.8 require and include (_once)............................ .................................................. ...........................
28 7 The functions.................. .................................................. .................................................. ..............29
7.1 Syntax and scope.................................. .................................................. ...................................29
7.2 Arguments............. .................................................. .................................................. ..................29
7.3 Return value .................................. .................................................. ........................................ 30
7.4 Internal functions............... .................................................. .................................................. ..............30
8 Managing forms in PHP.................................... .................................................. .......... .........31 8.1
GET and POST.................................... .................................................. .....................................31
8.2 Recovery by table......... .................................................. ..................................................31 8.3
Direct recovery ................................................ .................................................. .............. 32 8.4
Using tables ............................... .................................................. ..................................32 9 Date
and time .................. .................................................. .................................................. ...................34
9.1 Location.................................. .................................................. ..................................................34
9.2 Retrieving the date and time ............................................ .................................................. .........
34 9.2.1 date()........................................... .................................................. ...................................
34 9.2.2 strftime() local date...... .................................................. ............................................ 35
9.2 .3 Return by array getdate().............................................. .................................................. 36
9.2.4 The localtime() time .......................................... .................................................. ..................
37 9.3 Retrieve any date .................................. .................................................. .....................37 10
MySQL.......................... .................................................. .................................................. .............. 38
10.1 Presentation............................... .................................................. ..................................38 10.2
Tools........ .... .................................................. .................................................. ..............38 10.2.1
PhpMyAdmin....................... .................................................. ........................................38 10.2.2
MysqlCC..... .................................................. .................................................. ............... 39 10.3
Creating a database............................... .................................................. ...........................................39
10.4 Users and rights..... .................................................. .................................................. ......39
10.5 Creating and modifying tables.................................... .................................................. ..............
40 10.5.1 Numeric Types............................... .................................................. ..................40
10.5.1.1 Integer types .................. ........... .................................................. ...........................40
10.5.1.2 Real (floating point) types............... .................................................. ............................
40 10.5.1.3 Boolean types............... .................................................. .....................................
40 10.5.2 Character types....... .................................................. .................................................. ..
40 10.5.2.1 CHAR-types.......................................... .................................................. ..............
41 10.5.2.2 TEXT-types.............................. .................................................. ...........................
41 10.5.2.3 Type ENUM................. .................................................. ........................................
41 10.5.2.4 Type SET.... .................................................. .................................................. ........42
10.5.3 Date types................................................ .................................................. .................. 42
10.5.3.1 Supported formats .................. .................................................. ...........................43
10.5.3.2 DATETIME type................... .................................................. ..............................43
10.5.3.3 DATE type............... .................................................. ............................................ 43
10.5.3.4 TIMESTAMP type .................................................. ..............................................43
10.5.3.5 TIME-Type 44 ..................................................
10.5.3.6 YEAR- .................................................. ........
Type............................................... .................................................. ................44 10.5.4
Creation: SQL Syntax............................... .................................................. ........................44
S. ROHAUT PHP Programming Course Page 3/93
, Machine Translated by Google
10.5.5 Types of tables (MyISAM and InnoDB).......................................... ...................................
45 10.5.6 FULLTEXT Index......... .................................................. ...............................................46
10.5. 6.1 Creation................................................ .................................................. ..................
46 10.5.6.2 Search .................................. .................................................. ..................................
46 10.5.6.3 Boolean search .................. .................................................. ..................................47
10.6 MySQL and PHP............... .................................................. .................................................. .....
47 10.6.1 Connecting to a database............................................... ...............................................47
10.6. 1.1 Connecting to the server.............................................. ..................... ..................47
10.6.1.2 Choosing a base.......................... .................................................. ..................................48
10.6.1.3 Closing a connection..... .................................................. .............................. 48
10.6.1.4 Complete sequence for a base .............. .................................................. .............48
10.6.2 Queries ................................ .................................................. ..................................48
10.6.2.1 Executing a query.............. .................................................. .................................. 48
10.6.2.2 Number of lines affected............... .................................................. ...........................
49 10.6.2.3 Number of rows returned............... .................................................. ..................49
10.6.2.4 Retrieving the results........................... ................................... ..............................49
10.6.2.5 Insertion with auto-increment .............. .................................................. ...................
50 10.6.3 Error recovery .................................. .................................................. ...................... 50
11 The file system ................................. .................................................. ............................................51
11.1 Working with files .................................................. .................................................. ...........51
11.1.1 Opening............................... .................................................. ..................................51
11.1.2 Playback............ .................................................. .................................................. ............51
11.1.2.1 fgets().............................. .................................................. .....................................51
11.1.2.2 frea d();................................................ .................................................. ....................
51 11.1.2.3 fscanf()....................... .................................................. ...........................................52
11.1.2.4 fgetc() .................................................. .................................................. .................
52 11.1.2.5 fgetss().......................... .................................................. ........................................
52 11.1.2.6 fgetcsv()... .................................................. .................................................. ...........52
11.1.2.7 Returning a file to standard output .................................. ..................................52
11.1.3 Writing........... .................................................. .................................................. .............53
11.1.4 Movement.................................. ....................... .................................................. .........53
11.1.4.1 fseek().................................. .................................................. .................................53
11.1.4.2 ftell().......... .................................................. .................................................. .........53
11.1.4.3 rewind().................................. .................................................. .............................. 53
11.1.5 End of file .............. .................................................. .................................................. 53
11.1.6 Closing ............................................... .................................................. ...........................
54 11.2 Working with directories .................. .................................................. ..................................54
11.2.1 Current directory.............. .................................................. ............................. ...................
54 11.2.2 Moving ........................... .................................................. ........................................ 54
11.2.3 Open, list, close .................................................. .................................................. .. 54
11.2.4 Retrieve all entries .................................................. .................................................. 54
11.3 Working on the file system ............................................... .................................................. 55
11.3.1 File information .................................................. .................................................. ..55
11.3.2 Actions on files and directories............................................... .................................. 56
11.4 Uploading files............... .................................................. .................................................. ..57
12 Work on chains.................... .................................................. .................................................. .59
12.1 Functions for HTML and MySQL code............................................... ........................................59
12.1.1 Management of locked characters.. .................................................. ..............................59
12.1.2 HTML tags and special characters...................... .................................................. ..................59
12.2 General text functions .................................. .................................................. ...........................60
12.2.1 Cutting a chain................... .................................................. ................................. 60
S. ROHAUT PHP Programming Course Page 4/93
PHP course
,Machine Translated by Google
Contents
1 Introduction................................................ .................................................. ..................................... 7
1.1 Definition........ .................................................. .................................................. .....................7
1.2 History............................... .................................................. .................................................. .....7
1.3 Practical use .................................................. .................................................. .........................
7 1.4 Static pages vs dynamic pages ............................... .................................................. ..................
8 1.5 CGI Scripts............................... .................................................. .................................................. ..
8 1.6 Dynamic pages and PHP............................................... .................................................. ..............8
1.7 Why prefer PHP to other languages such as ASP?........................................... ..................8 1.8
What you should already know ............................... .................................................. ..................9
1.9 The necessary server.......................... .................................................. .....................................
9 1.10 The customer kit........ .................................................. .................................................. ......
9 1.11 Compliance with standards .................................. .................................................. ...................9
2 Basics:Presentation.......................... .................................................. ..............................................11
2.0.1 Basic syntax.................................................. .................................................. ............... 11
2.1 Integrating with HTML............................... .................................................. ..................................11
2.2 Instruction separator......... .................................................. ..................................................12
2.3 Instruction block................................................ .................................................. ..................12
2.4 Comments........................... .................................................. ............................................ 12 3
Working with variables................................................ .................................................. ........ 13 3.1
Declaring a variable .................................. .................................................. ........................... 13 3.2
Scope of variables .............. .................................................. ........................................... 13 3.3
Predefined variables.... ..................................... .................................................. .................. 14 3.4
Dynamic variables .................................. .................................................. ..................................15
3.5 Types of variables .................. .................................................. .................................................. .....15
3.5.1 Booleans.................................... .................................................. ................................. 15
3.5.2 Integers............ .................................................. .................................................. ..............
15 3.5.3 Floating point .............................. .................................................. ...............................
16 3.5.4 Character strings .............. .................................................. ........................................ 16
3.5.5 Tables... .................................................. ..................... ........................................... 17 3.5.6
The object variable .................................................. .................................................. ...........18
3.6 Constants.................................... .................................................. ..................................... 18 3.7
Obtaining the type of a variable.... .................................................. ............................................ 19
3.8 Defining and deleting a variable................................................. ........................................... 19 4
Operators... .................................................. .................................................. ...........................20 4.1
The precedence of operators .................. .................................................. ............................... 20
4.2 Arithmetic operators ............................... .................................................. ........................... ..............
20 4.3 Assignment operators .................................. .................................................. .........................
20 4.4 Bitwise Operators............................... .................................................. ...........................................20
4.5 Comparison operators.............. .................................................. .................................................21
4.6 Error operator.................................................. .................................................. .................. 21
4.7 Execution operator............................... .................................................. .................................. 21
4.8 Increment/Decrement Operators .................. .................................................. ..................21 4.9
Logical operators............................... .................................................. .................................. 21 4.10
String Operators .................................................. .................................................. ........ 22 4.11
Array operator.................................... .................................................. ..................... 22 5 The notion
of expression .................. .................................................. ...........................................23 6 The
structures of control................................................. .................................................. ............. 25 6.1
if.................................. .................................................. .................................................. .........25 6.2
while...................................... .................................................. .................................................25
S. ROHAUT PHP Programming Course Page 2/93
,Machine Translated by Google
6.2.1 classic while........................................... .................................................. .................. 25
6.2.2 do ... while....................... .................................................. ...............................................25
6.3 for .................................................. .................................................. ........................................26
6.4 foreach...... .................................................. .................................................. ...........................
26 6.5 break and continue.................... .................................................. ..................................................
27 6.6 switches............................................. .................................................. ..................................27
6.7 return......... .................................................. .................................................. ............. ..............28
6.8 require and include (_once)............................ .................................................. ...........................
28 7 The functions.................. .................................................. .................................................. ..............29
7.1 Syntax and scope.................................. .................................................. ...................................29
7.2 Arguments............. .................................................. .................................................. ..................29
7.3 Return value .................................. .................................................. ........................................ 30
7.4 Internal functions............... .................................................. .................................................. ..............30
8 Managing forms in PHP.................................... .................................................. .......... .........31 8.1
GET and POST.................................... .................................................. .....................................31
8.2 Recovery by table......... .................................................. ..................................................31 8.3
Direct recovery ................................................ .................................................. .............. 32 8.4
Using tables ............................... .................................................. ..................................32 9 Date
and time .................. .................................................. .................................................. ...................34
9.1 Location.................................. .................................................. ..................................................34
9.2 Retrieving the date and time ............................................ .................................................. .........
34 9.2.1 date()........................................... .................................................. ...................................
34 9.2.2 strftime() local date...... .................................................. ............................................ 35
9.2 .3 Return by array getdate().............................................. .................................................. 36
9.2.4 The localtime() time .......................................... .................................................. ..................
37 9.3 Retrieve any date .................................. .................................................. .....................37 10
MySQL.......................... .................................................. .................................................. .............. 38
10.1 Presentation............................... .................................................. ..................................38 10.2
Tools........ .... .................................................. .................................................. ..............38 10.2.1
PhpMyAdmin....................... .................................................. ........................................38 10.2.2
MysqlCC..... .................................................. .................................................. ............... 39 10.3
Creating a database............................... .................................................. ...........................................39
10.4 Users and rights..... .................................................. .................................................. ......39
10.5 Creating and modifying tables.................................... .................................................. ..............
40 10.5.1 Numeric Types............................... .................................................. ..................40
10.5.1.1 Integer types .................. ........... .................................................. ...........................40
10.5.1.2 Real (floating point) types............... .................................................. ............................
40 10.5.1.3 Boolean types............... .................................................. .....................................
40 10.5.2 Character types....... .................................................. .................................................. ..
40 10.5.2.1 CHAR-types.......................................... .................................................. ..............
41 10.5.2.2 TEXT-types.............................. .................................................. ...........................
41 10.5.2.3 Type ENUM................. .................................................. ........................................
41 10.5.2.4 Type SET.... .................................................. .................................................. ........42
10.5.3 Date types................................................ .................................................. .................. 42
10.5.3.1 Supported formats .................. .................................................. ...........................43
10.5.3.2 DATETIME type................... .................................................. ..............................43
10.5.3.3 DATE type............... .................................................. ............................................ 43
10.5.3.4 TIMESTAMP type .................................................. ..............................................43
10.5.3.5 TIME-Type 44 ..................................................
10.5.3.6 YEAR- .................................................. ........
Type............................................... .................................................. ................44 10.5.4
Creation: SQL Syntax............................... .................................................. ........................44
S. ROHAUT PHP Programming Course Page 3/93
, Machine Translated by Google
10.5.5 Types of tables (MyISAM and InnoDB).......................................... ...................................
45 10.5.6 FULLTEXT Index......... .................................................. ...............................................46
10.5. 6.1 Creation................................................ .................................................. ..................
46 10.5.6.2 Search .................................. .................................................. ..................................
46 10.5.6.3 Boolean search .................. .................................................. ..................................47
10.6 MySQL and PHP............... .................................................. .................................................. .....
47 10.6.1 Connecting to a database............................................... ...............................................47
10.6. 1.1 Connecting to the server.............................................. ..................... ..................47
10.6.1.2 Choosing a base.......................... .................................................. ..................................48
10.6.1.3 Closing a connection..... .................................................. .............................. 48
10.6.1.4 Complete sequence for a base .............. .................................................. .............48
10.6.2 Queries ................................ .................................................. ..................................48
10.6.2.1 Executing a query.............. .................................................. .................................. 48
10.6.2.2 Number of lines affected............... .................................................. ...........................
49 10.6.2.3 Number of rows returned............... .................................................. ..................49
10.6.2.4 Retrieving the results........................... ................................... ..............................49
10.6.2.5 Insertion with auto-increment .............. .................................................. ...................
50 10.6.3 Error recovery .................................. .................................................. ...................... 50
11 The file system ................................. .................................................. ............................................51
11.1 Working with files .................................................. .................................................. ...........51
11.1.1 Opening............................... .................................................. ..................................51
11.1.2 Playback............ .................................................. .................................................. ............51
11.1.2.1 fgets().............................. .................................................. .....................................51
11.1.2.2 frea d();................................................ .................................................. ....................
51 11.1.2.3 fscanf()....................... .................................................. ...........................................52
11.1.2.4 fgetc() .................................................. .................................................. .................
52 11.1.2.5 fgetss().......................... .................................................. ........................................
52 11.1.2.6 fgetcsv()... .................................................. .................................................. ...........52
11.1.2.7 Returning a file to standard output .................................. ..................................52
11.1.3 Writing........... .................................................. .................................................. .............53
11.1.4 Movement.................................. ....................... .................................................. .........53
11.1.4.1 fseek().................................. .................................................. .................................53
11.1.4.2 ftell().......... .................................................. .................................................. .........53
11.1.4.3 rewind().................................. .................................................. .............................. 53
11.1.5 End of file .............. .................................................. .................................................. 53
11.1.6 Closing ............................................... .................................................. ...........................
54 11.2 Working with directories .................. .................................................. ..................................54
11.2.1 Current directory.............. .................................................. ............................. ...................
54 11.2.2 Moving ........................... .................................................. ........................................ 54
11.2.3 Open, list, close .................................................. .................................................. .. 54
11.2.4 Retrieve all entries .................................................. .................................................. 54
11.3 Working on the file system ............................................... .................................................. 55
11.3.1 File information .................................................. .................................................. ..55
11.3.2 Actions on files and directories............................................... .................................. 56
11.4 Uploading files............... .................................................. .................................................. ..57
12 Work on chains.................... .................................................. .................................................. .59
12.1 Functions for HTML and MySQL code............................................... ........................................59
12.1.1 Management of locked characters.. .................................................. ..............................59
12.1.2 HTML tags and special characters...................... .................................................. ..................59
12.2 General text functions .................................. .................................................. ...........................60
12.2.1 Cutting a chain................... .................................................. ................................. 60
S. ROHAUT PHP Programming Course Page 4/93