当前位置:网站首页>PHP notes are super detailed!!!
PHP notes are super detailed!!!
2022-07-03 05:37:00 【GuJingnan~】
PHP Preliminary grammar
PHP Concept :PHP( Full name :PHP:Hypertext Preprocessor, namely "PHP: Hypertext preprocessor ") It is a kind of operation in Server side Open source scripting language , Can be inlaid into HTML in .
PHP file : Can contain text 、HTML、JavaScript Code and PHP Code ,PHP The code is executed on the server , The result is pure HTML Return the form to the browser , The default file extension is ".php".
PHP effect :
- Generate dynamic page content
- establish 、 open 、 Read 、 write in 、 Close files on the server
- Collect form data
- Send and receive cookies
- add to 、 Delete 、 Modify the data in your database
- Restrict users from accessing some pages on your website
- Encrypt data
PHP The advantages of :
- PHP Can run on different platforms (Windows、Linux、Unix、Mac OS X etc. )
- PHP Compatible with almost all the servers being used at present (Apache、IIS etc. )
- PHP Provides extensive database support
- PHP It's free. , From the official PHP Download resources
PHP Scripts can be placed anywhere in the document .PHP Script With <?php Start , With ?> end .
PHP Statement separator
PHP Every line of code in must be It ends with a semicolon . A semicolon is a separator , Used to separate instruction sets .
// Define content
<?php
$a=5;
echo "Hello World";
?>
Special instructions :
- PHP Middle marker Terminator ?> It has the effect of its own statement terminator , The last line PHP Code can have no statement delimiters .
- PHP Many of the code written in is not embedded in HTML in , It's about being alone .
PHP notes
classification | Line notes | Block annotation |
Symbol | // # | /* */ |
explain | //: Everything that follows is comments | /**/: In the middle until */ Before it appeared, it was all comments |
PHP Output
stay PHP There are two basic output methods in :echo and print.
The difference between the two :
/ | echo | |
The output characters | You can output one or more strings | Only one string is allowed to be output |
Output speed | Faster | slower |
Return value | No return value | The return value is 1 |
echo
Output string :
<?php
echo "Hello World";
echo "1","2","3";
?>
Output variables :
<?php
$a=TFboys;
$team=array("Karry","Roy","Jackson Yee");
echo "$a";
echo "$a It's a powerful singing and dancing group ";
echo "TFboys My captain is {$team[0]}";
?>
The output characters
<?php
print "TFboys It's my favorite combination ";
?>
Output variables
<?php
$a=TFboys;
$team=array("Karry","Roy","Jackson Yee");
print "$a";
print "$a It's a powerful singing and dancing group ";
print "TFboys My captain is {$team[0]}";
?>
PHP Variable
Variable concept : Variables are containers for storing data .
Variable naming rule :
- stay PHP The variable name in must be “$” Symbol start ;
- Names are made up of letters 、 Numbers and underscores “_” constitute , But you can't start with a number ;
- stay PHP Chinese variables are allowed in ( Don't suggest );
- Variable names are case sensitive ;
- Variable names cannot contain spaces .
Variable scope
PHP There are four different variable scopes :local、global、static、parameter.
Serial number | Scope | describe |
1 | local | Local scope |
2 | global | Global scope |
3 | static | Static scope |
4 | parameter | Function parameter scope |
PHP global keyword
global Keywords are used for Access global variables within functions .
Call global variables defined outside the function inside the function , We need to add... Before the variable in the function global keyword :
PHP Store all global variables in a file called $GLOBALS[index] In the array . index Is the name of the variable . This array can be accessed inside the function , It can also be used to update global variables directly , Therefore, the above code can also be written in this way :
static Scope
When a function is complete , All its variables will be deleted . If you want a local variable not to be deleted, you need to use static.
Use of variables
1. Definition : Add the corresponding variable name in the system ( In the memory )
2. assignment : You can assign data to variable names ( It can be done at the same time of definition )
3. The stored data can be accessed through the variable name
4. Variables can be removed from memory
// Defining variables : stay PHP You do not need any keywords to define variables in ( assignment )
$var1; // Defining variables
$var2 = 1; // Define and assign
// Access variables
echo $var2; // adopt $var2 Variable name find the stored content 1, Then the output .
// Modify variables
$var 2 = 2;
echo '<hr/>',$var2;
// Delete variables : Use unset( Variable name )
unset($var2);
echo $var2;
Predefined variables
Predefined variables : Variables defined in advance , Store a lot of data that needs to be used ( Predefined variables are arrays )
$_GET : Get all forms to get How to submit data
$_POST : POST All submitted data will be saved here
$_REQUEST : GET and POST Everything submitted will be saved
$GLOBALS : PHP All global variables in
$_SERVER : server information
$_SESSION : session Session data
$_COOKIE : cookie Session data
$_ENY : environmental information
$_FILES : File information uploaded by users
Volatile variables
Volatile variables : If the value stored in one variable happens to be the name of another variable , Then you can directly access one variable to get another variable ; Add one more before the variable $ Symbol .
PHP Constant
Constant :const/constant, It is a kind of program running , An unalterable quantity ( data ). Once a constant is defined , Usually the data cannot be changed ( User level ).
Constant definition form
1. Use define() function :
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
among :
name: Required parameters , Constant name , This is the identifier .
value: Required parameters , Value of constant .
case_insensitive : Optional parameters , If set to TRUE, The constant is case insensitive . The default is case sensitive .
Naming rules for constant names
- Constants don't need to use “$” Symbol , Once used, the system is considered a variable
- Constant names consist of letters 、 Numbers and underscores , Cannot start with a number
- Constant names are usually capitalized ( Distinguish from variables )
- Constant naming rules are looser than variables , You can use some special characters , This method can only use define Definition
Attention to detail :
- define and const The constants defined are different : The difference lies in the access rights
- Defining constants is usually case insensitive , But it can be distinguished from , You can refer to define The third argument to the function
Example :
1. Case sensitive :
<?php
// Case sensitive constant names
define("TFBOYS","Good!!!");
echo TFBOYS;
echo'<br>';
echo tfboys
?>
2. Case insensitive :
<?php
// Case sensitive constant names
define("TFBOYS","Good!!!",ture);
echo TFBOYS;
echo'<br>';
echo tfboys
?>
Constants are global
Constant after definition , The default is global , It can be used anywhere throughout the running script .
System constant
System constant : The system helps you define constants , Users can use .
Several common system constants :
PHP_VERSION:PHP Version number
PHP_INT-SIZE: Shaping size
PHP_INT-MAX: The maximum value that can be represented by shaping (PHP Middle integer is a negative number allowed : Signed )
stay PHP There are also some special constants in , They start with a double underline + Constant names + Double underline ends , This constant is called the system magic constant : The value of magic constant usually changes with the environment , But users can't change
--DIR--: The absolute path of the computer where the currently executed script is located
--FILE--: The absolute path of the computer where the currently executed script is located
--LINE--: The number of rows that currently belong to
--CLASS--: The current class
PHP data type
data type :data type, stay PHP Refers to the type of stored data itself , Not the type of variable .PHP Is a weakly typed language , The variable itself has no data type .
PHP Eight data types are supported :
- String( character string )
- Integer( integer )
- Float( floating-point )
- Boolean( Boolean type )
- Array( Array )
- Object( object )
- NULL( Null value )
- Resource( The resource type )
PHP character string
A string is a sequence of strings , It's like "Hello World", Usually used when outputting text , Wrap the text in double or single quotation marks .
<?php
$x = "TFBOYS";
echo $x;
echo "<br>";
$x = 'very good!!!';
echo $x;
?>
PHP integer
stay PHP There are four ways to define integers : Decimal definition , Binary definition , Octal definition , Hexadecimal definition
Decimal system : Meet 10 Into the 1, The number that can appear 0-9;
Binary system : Meet 2 Into the 1, The number that can appear is 0-1;
octal : Meet 8 Into the 1, The number that can appear is 0-7;
Hexadecimal : Meet 16 Into the 1, The number that can appear is 0-9 as well as a-f,a Express 10, And so on .
PHP floating-point
Floating point type : Decimal type and integer beyond the storage range of integer ( Accuracy is not guaranteed ), The accuracy range is about 15 About a significant number .
Floating point types are defined in two ways :
$f=1.23;
$f=1.23e10; // Scientific enumeration , among e Express 10
PHP Boolean type
Boolean type : Two values ture and false, Usually used to judge and compare
PHP Array
Arrays can store multiple values in a variable .
Example :
<?php
$a=TFboys;
$team=array("Karry","Roy","Jackson Yee");
?>
PHP object
Object data types can be used for data storage .
stay PHP in , Object must declare .
First , Use class Keyword declaration class object . A class is a structure that can contain properties and methods .
Then define the data type in the class , Use data types in instantiated classes .
PHP NULL value
NULL Value means that the variable has no value ,NULL The data type is NULL Value .
You can set the variable value to NULL To clear variable data :
<?php
$x="Hello world!";
$x=null;
var_dump($x); // Return to NULL
?>
PHP Operator
PHP Arithmetic operator
Operator | name | describe | example | result |
---|---|---|---|---|
x + y | Add | x and y And | 2 + 2 | 4 |
x - y | reduce | x and y Difference | 5 - 2 | 3 |
x * y | ride | x and y Product of | 5 * 2 | 10 |
x / y | except | x and y The business of | 15 / 5 | 3 |
x % y | model ( The remainder of division ) | x Divide y The remainder of | 5 % 2 10 % 8 10 % 2 | 1 2 0 |
- x | Take the opposite | x Take the opposite | <?php $x =2; echo -$x; ?> | -2 |
a . b | Juxtaposition | Connect two strings | "Hi" . "Ha" | HiHa |
intdiv(a,b)(PHP7+ edition ) | Rounding down | The first parameter is divided by the value of the second parameter and rounded ( Rounding down ) | var_dump(intdiv(10, 3)) | int(3) |
PHP Assignment operator
Operator | Equate to | describe |
---|---|---|
x = y | x = y | The left operand is set to the value of the expression on the right |
x += y | x = x + y | Add |
x -= y | x = x - y | reduce |
x *= y | x = x * y | ride |
x /= y | x = x / y | except |
x %= y | x = x % y | model ( The remainder of division ) |
a .= b | a = a . b | Connect two strings |
PHP Increasing / Decrement operator
Operator | name | describe |
---|---|---|
++ x | Pre increment | x Add 1, Then return x |
x ++ | After increasing | return x, then x Add 1 |
-- x | Pre decrement | x reduce 1, Then return x |
x -- | Post decline | return x, then x reduce 1 |
PHP Comparison operator
Operator | name | describe | example |
---|---|---|---|
x == y | be equal to | If x be equal to y, Then return to true | 5==8 return false |
x === y | Absolutely equal to | If x be equal to y, And they're the same type , Then return to true | 5==="5" return false |
x != y | It's not equal to | If x It's not equal to y, Then return to true | 5!=8 return true |
x <> y | It's not equal to | If x It's not equal to y, Then return to true | 5<>8 return true |
x !== y | Absolutely not equal to | If x It's not equal to y, Or they're not the same type , Then return to true | 5!=="5" return true |
x > y | Greater than | If x Greater than y, Then return to true | 5>8 return false |
x < y | Less than | If x Less than y, Then return to true | 5<8 return true |
x >= y | Greater than or equal to | If x Greater than or equal to y, Then return to true | 5>=8 return false |
x <= y | Less than or equal to | If x Less than or equal to y, Then return to true | 5<=8 return true |
PHP Logical operators
Operator | name | describe | example |
---|---|---|---|
x and y | And | If x and y All for true, Then return to true | x=6 y=3 (x < 10 and y > 1) return true |
x or y | or | If x and y At least one for true, Then return to true | x=6 y=3 (x==6 or y==5) return true |
x xor y | Exclusive or | If x and y There is and only one for true, Then return to true | x=6 y=3 (x==6 xor y==3) return false |
x && y | And | If x and y All for true, Then return to true | x=6 y=3 (x < 10 && y > 1) return true |
x || y | or | If x and y At least one for true, Then return to true | x=6 y=3 (x==5 || y==5) return false |
! x | Not | If x Not for true, Then return to true | x=6 y=3 !(x==y) return true |
PHP Array operators
Operator | name | describe |
---|---|---|
x + y | aggregate | x and y Set |
x == y | equal | If x and y With the same key / It's worth it , Then return to true |
x === y | Identity | If x and y With the same key / It's worth it , And the order is the same, the type is the same , Then return to true |
x != y | It's not equal | If x It's not equal to y, Then return to true |
x <> y | It's not equal | If x It's not equal to y, Then return to true |
x !== y | Not identical | If x It's not equal to y, Then return to true |
Ternary operator
The grammar format is : (expr1) ? (expr2) : (expr3)
Yes expr1 Evaluated as TRUE The value of time is expr2, stay expr1 Evaluated as FALSE The value of time is expr3.
Combination comparator (PHP7+)
grammar :$c = $a <=> $b;
- If $a > $b, be $c The value of is 1.
- If $a == $b, be $c The value of is 0.
- If $a < $b, be $c The value of is -1.
边栏推荐
- Win10 install pytullet and test
- Robot capture experiment demonstration video
- Source insight License Activation
- 【一起上水硕系列】Day 10
- Sophomore dilemma (resumption)
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- Brief introduction of realsense d435i imaging principle
- 32GB Jetson Orin SOM 不能刷机问题排查
- Communication - how to be a good listener?
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
猜你喜欢
一起上水硕系列】Day 9
Disassembly and installation of Lenovo r7000 graphics card
Go practice - gorilla / handlers used by gorilla web Toolkit
6.23星期四库作业
Brief introduction of realsense d435i imaging principle
Today, many CTOs were killed because they didn't achieve business
Map的扩容机制
Introduction to redis using Lua script
SimpleITK学习笔记
Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
随机推荐
Xaml gradient issue in uwp for some devices
牛客网 JS 分隔符
2022.7.2day594
Best practices for setting up altaro VM backups
Making coco datasets
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
Troubleshooting of 32GB Jetson Orin SOM failure to brush
Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
Interview question -- output the same characters in two character arrays
期末复习(Day5)
Final review (Day7)
ES 2022 正式发布!有哪些新特性?
Final review (Day6)
Robot capture experiment demonstration video
Go practice -- use JWT (JSON web token) in golang
Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
【一起上水硕系列】Day 7 内容+Day8
Deep embedding and alignment of Google | protein sequences
Introduction to deep learning (II) -- univariate linear regression
Why is go language particularly popular in China