Write in front
The contact about PHP was limited to the course design at that time. Now, the old people and things have long gone with the wind, but the young blog content has remained. There is no enthusiasm for learning and exploration in those years. Just re edit these and talk about remembering the past.
PHP variable scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable refers to the part of the script where the variable can be referenced / used
PHP variable rules
- The variable starts with the $symbol and is followed by the variable name
- Variable names must start with letters or underscores
- Variable names cannot start with numbers
- Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and)
- Variable names are case sensitive ($Y and $y are two different variables)
- Note: PHP variable names are case sensitive!
PHP has three different variable scopes
- Local (local)
- global
- static
- Local and Global scopes
- Variables declared outside the function have Global scope and can only be accessed outside the function
- Variables declared inside the function have LOCAL scope and can only be accessed inside the function.
-
For example:
echo "hello world!<br>"; echo "where are you?<br>"; $text = "eleven"; //echo $text function echoText(){ //$text = "11"; echo $text; }
-
The result is:
hello world!
where are you? -
After removing the note:
echo "hello world!<br>"; echo "where are you?<br>"; $text = "eleven"; echo $text function echoText(){ //$text = "11"; echo $text; }
-
The result is:
hello world!
where are you?
eleven
foreach syntax
-
The first is to traverse the given array statement array_expression array. In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward one step (so the next cell will be obtained in the next loop).
foreach(array_expression as $value)
-
The second is the same as above. At the same time, the key name of the current cell will also be assigned to the variable $key in each cycle.
foreach(array_expression as $key => $value)
-
One dimensional ordinary array and foreach
$a = array('meet','Acquaintance','Acquaintances','Forget'); foreach($a as $value){ echo $value."<br/>" } echo "-------------<br/>" foreach($a as $key => $value){ echo $key.','.$value."<br/>" }
-
The result is:
meet
Acquaintance
Acquaintances
Forget0. Meet
1. Acquaintance
2. Acquaintances
3. ForgetSummary: there is an additional $key. The value of $key is serial number 1, 2, 3, 4
-
One dimensional associative array and foreach
$b = array('one' => 'meet','two' => 'Acquaintance','three' => 'Acquaintances','four' => 'Forget'); foreach($b as $value){ echo $value."<br/>" } echo "-------------<br/>" foreach($b as $key => $value){ echo $key.','.$value."<br/>" }
-
The result is:
meet
Acquaintance
Acquaintances
Forget1, Meet
2, Acquaintance
3, Acquaintances
4, ForgetSummary: in a one-dimensional associative array, $key is the serial number of the association, that is, the corresponding one, two, three and four.
-
Two dimensional ordinary array and foreach
$c = array( array('1','meet'), array('2','Acquaintance'), array('3','Acquaintances'), array('4','Forget'), ); foreach($c as $value){ print_r($value) echo "<br/>"; } echo "-------------<br/>" foreach($c as $key => $value){ echo '$key='.$key."<br/>" print_r($value) echo "<br/>"; }
-
The result is:
Array ([0] = > 1 [1] = > meet)
Array ([0] = > 2 [1] = > acquaintance)
Array ([0] = > 3 [1] = > acquaintances)
Array ([0] = > 4 [1] = > forgetting)$key=0
Array ([0] = > 1 [1] = > meet)
$key=1
Array ([0] = > 2 [1] = > acquaintance)
$key=2
Array ([0] = > 3 [1] = > acquaintances)
$key=3
Array ([0] = > 4 [1] = > forgetting)Summary: it can be seen from the above that in the basic two-dimensional array, $key is the serial number, such as 0, 1, 2, 3
-
Associative 2D array and foreach
$d = array( array('id' => '1','name' => 'meet'), array('id' => '2','name' => 'Acquaintance'), array('id' => '3','name' => 'Acquaintances'), array('id' => '4','name' => 'Forget'), ); foreach($d as $value){ print_r($value) echo "<br/>"; } echo "-------------<br/>" foreach($d as $key => $value){ echo '$key='.$key."<br/>" print_r($value) echo "<br/>"; }
-
The result is:
Array ([ID] = > 1 [name] = > meet)
Array ([ID] = > 2 [name] = > acquaintance)
Array ([ID] = > 3 [name] = > acquaintances)
Array ([ID] = > 4 [name] = > forgetting)$key=0
Array ([ID] = > 1 [name] = > meet)
$key=1
Array ([ID] = > 2 [name] = > acquaintance)
$key=2
Array ([ID] = > 3 [name] = > acquaintances)
$key=3
Array ([ID] = > 4 [name] = > forgetting)Summary: $key is still 0, 1, 2, 3
practice
- Associative two-dimensional array becomes ordinary one-dimensional array
$d = array( array('id' => '1','name' => 'meet'), array('id' => '2','name' => 'Acquaintance'), array('id' => '3','name' => 'Acquaintances'), array('id' => '4','name' => 'Forget'), ); foreach($d as $key => $value){ echo ($value['name']) echo "<br/>"; } echo "-------------<br/>" //Gets the name column as a one-dimensional array $nameArr = array(); //name column foreach($d as $key => $value){ $nameArr[] = $value['name']; } print_r($nameArr );
- The result is:
meet
Acquaintance
Acquaintances
ForgetArray ([0] = > meet [1] = > know [2] = > know [3] = > forget)
var_dump() returns the data type and value of the variable
$x = 5985; var_dump($x); echo "<br/>"; $x = -345; //negative var_dump($x); echo "<br/>"; $x = 0x8C; //Hexadecimal number var_dump($x); echo "<br/>"; $x = 047; //Octal number var_dump($x); echo "<br/>"; $x = 10.365; var_dump($x); echo "<br/>"; $x = 2.4e3; var_dump($x); echo "<br/>"; $x = 8E-5; var_dump($x); echo "<br/>"; $cars = array("Volvo","BMW","SAAB");//array var_dump($cars );
- The result is:
int 5985
int -345
int 140
int 39
float 10.365
float 2400
float 8.0E-5
array (size=3)
0 => string 'Volvo' (length=5)
1=> string 'BMW' (length=3)
2 => string 'SAAB' (length=4)
PHP object
- An object is a data type that stores data and information about how data is processed.
- In PHP, objects must be explicitly declared.
- First, declare the class of the object with the class keyword. A class is a structure that contains properties and methods
-
Define a data type in an object class and then use it in an instance of that class.
class Car{ var $color; function Car ($color = "green") { $this->color = $color; } function what_color () { return $this->color; } } function print_vars ($obj) { foreach(get_object_vars($obj) as $prop => $val) { echo "\t$prop = $val\n"; } } //instantiate one object $herbie = new Car("white"); //show herbie properties echo "\therbie = Properties\n"; print_vars($herbie);
The result is:
herbie: Properties color = white
Note:
-
Public and var have similar functions, because if the variables defined by var are not protected or private, they are public by default
-
VaR is generally used in PHP4, and public is generally used in PHP5
-
Now, public is basically used instead of var
var is the of defining variables; public defines the visibility of property and method
-
PHP constants
-
A constant is an identifier (name) of a single value. The value cannot be changed in a script
-
Valid constant names begin with a character or underscore (constant names are not preceded by the $symbol)
-
Unlike variables, constants are automatic and global throughout the script
-
define() function for setting PHP constants - it uses three parameters:
- The first parameter defines the name of the constant
- The second parameter defines the value of the constant
- The optional third parameter specifies whether the constant name is case insensitive. The default is false.
//Defines constants that are case sensitive define("SAD","Everlasting longing for each other"); echo SAD; echo "<br/>"; //Constant values that will not be output echo sad; echo "<br/>"; echo "<--------------------<br/>"; define("SAD","Everlasting longing for each other",true);//Case insensitive value is true echo SAD; echo "<br/>"; echo sad;
The result is:
Everlasting longing for each other
Notice:use of undefined constant sad-assumed 'sad' in ...\test.php on line 7Time Memory Function Location 0.0010 133800 (main)() ...\test.php:() sad
Everlasting longing for each other
Everlasting longing for each other
Write it at the back
It's finally finished, but things are different after all
Like before, it's not like before.