PHP program running sequence
PHP basic syntax
With <? php ...?> Structural frame
Fill in the code at
Use; End statement
echo multiple output
print word output
Comments / / multiline comments / **/
variable
1. Start with $
2. No space is allowed in the middle
3. Cannot start with a number
Common nomenclature: hello_world,HelloWord,
actual combat
1. For the mixed compilation of HTML and PHP files, you can change the code under the PHP file and display the web page. However, if you change the PHP code under the HTML file, the web page will not display the changed content.
2. The code is executed from top to bottom.
3. Variable names should be unique. If two variables are the same and close together, the next variable name will overwrite the previous variable.
PHP scalar type
Value of echo output data
var_dump prints the type and value of data, which is used in the test.
1. Boolean
Usually used to judge conditions
<?php $x = true; var_dump($x); echo '<hr>'; $y = flase; var_dump($y); ?>
2. Integer
- Cannot contain spaces
- No decimal point
- It can be positive or negative
- It can be specified in three formats: decimal, hexadecimal and octal.
<?php $number = 0; var_dump($number); echo '<hr>' $number = 67; var_dump($number); echo '<hr>'; $number = -322; var_dump($number); ?>
3. Floating point
<?php $number = 10.03; var_dump($number); echo '<hr>'; $number = 69.3223; var_dump($number); echo '<hr>'; $number = -32.099; var_dump($number); ?>
4. String
- Data in quotation marks
- It can be a single quotation mark or a double quotation mark
- Strings without quotation marks will report errors
- All scalar types quoted are strings
About how to solve Chinese input garbled code in PHP:
You only need to add one in the header of the php file:
<?php header('Content-Type: text/html; charset=utf-8'); ?>
Test:
<?php header('Content-Type: text/html; charset=utf-8'); $str = 'I'm Xiao Ming'; var_dump($str); echo '<hr>'; $str = 'my name is tom'; var_dump($str); ?>
If the quotation marks are removed, an error will be reported.
The difference between single quotation marks and double quotation marks:
Putting a variable in double quotation marks will output the value of the variable.
Single quotation marks will directly output the $variable
<?php header('Content-Type: text/html; charset=utf-8'); $str = 'I'm Xiao Ming'; var_dump($str); echo '<hr>'; $str = "$str my name is tom"; var_dump($str); echo '<hr>'; $str = '$str my name is tom'; var_dump($str); ?>
php composite data type
<1> . array type
1. Create an empty array:
<?php $arr = array(); var_dump($arr); $arrs = []; var_dump($arrs); ?>
Arrays are composed of keys and values. By default, the key starts from 0, but it can also be customized. After definition, the index starts from the key value of the next defined is 0. Similar to dict. In python
<?php $arr = array( 'hello', 'where' => 'where', 'how are you' ); echo $arr[0]; echo '<hr>'; echo $arr['where']; ?>
2. Output all data in the array.
<?php $arr = array( 'hello', 'where' => 'where', 'how are you' ); print_r($arr); ?>
3. Connector
echo 'I come from'.$arr['where'];
<2> Multidimensional array
1. Two dimensional array
<?php header('Content-Type: text/html; charset=utf-8'); $arr = array( array( 'watermelon' ), array( 'Peach', 'Durian' ), array( 'Grape', 'Apple' ) ); echo $arr; echo '<hr>'; print_r($arr); ?>
2. 3D array
Index of 3D array:
echo 'My name is:'.$arr[0]['name'].',My school is'.$arr[0]['school']; echo 'I will:'.$arr[0]['gongfu'][0].',Also'.$arr[0]['gongfu'][1];
<3> Array loop
1. Loop of one-dimensional array
<?php header('Content-Type: text/html; charset=utf-8'); $arr = array( 'xiagua' => 'watermelon', 'taozi' => 'Peach', 'liulian' => 'Durian', 'putao' => 'Grape', 'pingguo' => 'Apple' ); print_r($arr); echo '<hr>'; $num = 0; foreach ($arr as $k => $v) { echo $k.$v; echo '<hr>'; } ?>
2. Loop of multidimensional array
Principle of circulation:
<?php header('Content-Type: text/html; charset=utf-8'); $arr = array( array( 'watermelon', 'Mangosteen' ), array( 'Peach', 'Durian' ), array( 'Grape', 'Apple' ) ); foreach ($arr as $k => $v) { echo $v[0].$v[1]; echo '<hr>'; } ?>
<?php header('Content-Type: text/html; charset=utf-8'); $arr = array( array( 'watermelon', 'Mangosteen' ), array( 'Peach', 'Durian' ), array( 'Grape', 'Apple' ) ); foreach ($arr as $k => $v) { foreach ($v as $vv) { echo $vv; echo '<hr>'; } } ?>
3. Loop of 3D array
<?php header('Content-Type: text/html; charset=utf-8'); $arr = array( array( 'watermelon', 'Mangosteen', array( 'yummy', 'cheap' ), ), array( 'Peach', 'Durian', array( 'Fragrant', 'Head on' ), ), array( 'Grape', 'Apple', array( 'good-looking', 'delicious' ), ) ); ?>
PHP actual combat 01 (array loop)
1. Put the HTML code directly into the PHP code block.
<ul class="nav-menu"> <?php $menu = [ 'home page', 'The server', 'PHP', 'front end', 'Thinkphp', 'Layui', 'Applet' ]; ?> <?php foreach ($menu as $menu_v) { echo '<li>'; echo '<a href="/index.html">'.$menu_v.'</a>'; echo '</li>'; } ?>
Run results.
2. Intersperse PHP code with HTML code. Pay attention to the interspersion of PHP code. You need to use <? php ,.,.. ?> Package.
<?php $menu = [ 'home page', 'The server', 'PHP', 'front end', 'Thinkphp', 'Layui', 'Applet' ]; ?> <?php foreach ($menu as $menu_v) { ?> <li> <a href="/index.html"><?php echo $menu_v; ?></a> </li> <?php } ?>
Conditional judgment of PHP
Ternary operator
$name = 'Xiao Ming'; echo '<hr>'; echo $name ?'My name is little bee':'I don't know who I am!!!';
if
else
elseif
$name = 'Xiao Ming'; if($name){ echo $name; }elseif($name){ echo 'your are a pig !'; } else{ echo 'i don\'t konw who i am !'; }
switch case defult
break
$number='1123121'; switch($number){ case '1123': echo 'input 1123'; break; case '2': echo 'input 2'; break; case '3': echo 'input 3'; break; default: echo $number; };
match
$str = 'age'; echo match ($str) { 'name' => 'xioam', 'age' => '12', default => 'imasas', };
The difference between match and switch
1.match is an expression. The result can be put in the stored variable or returned.
2. The branch of match only supports single line expressions without terminals.
$str = 'age'; echo match ($str) { 'name' => 'xioam', echo '1212'; 'age' => '12', default => 'imasas', };
3.match for strict matching comparison.
PHP special data types
1.NULL
Indicates that the variable has no value
2. Resource type
Functions in PHP
1.String function
2. Array function
Official website Manual: php array related functions official website manual
3. User defined functions
It's all in the code:
Similar to C language functions.
$nm3 = 10 default parameters.
<?php $num2 = 90; function num($num1,$num2,$num3=10){ return $num1+$num2+$num3; } echo num(10,12); ?>
You can also reference global variables in custom functions.
<?php $num2 = 90; function num($num1,$num3=10){ global $num2; return $num1+$num2+$num3; } echo num(10); ?>
Named parameters:
- Only required parameters are specified. Skip optional parameters.
- Parameters are independent of sequence and have their own recording function.
Operation result: the position of c d does not affect the final result
PHP operators
Output results:
For the first execution of $num + +, execute the value of that num first, then add 1 to the value of num, and the second output will be $num+1.($num – the same)
++$num is the direct output of adding u first and then again. (– $sum)
Connector:
A variable connected to an integer must be preceded by a space.
Comparison operator:
===Similar to = =, in C language, it not only compares values, but also compares the types of values.
Comparison of string and number:
PHP8: 0 = = 'hello' / / false
When comparing with a numeric string, PHP8 uses numeric comparison, otherwise it converts the number into a numeric string for comparison.
Logical operators:
Priority is similar to C language
PHP practice 02 (function)
examp1:
The code in the following figure shows that if statement and function isset() are used to judge whether the title exists, then the title will be output.
example2:
The code in the figure below indicates that if the picture is not empty, that is, if there is a picture, the picture will be output. The empty () function determines whether the () is empty. If it is empty, it returns true, followed by another one! It means negative, that is, there are pictures. Input pictures under this condition.
The following figure shows the original code before modification for comparison.
examp3:
The isset() function will see 0 as true, that is, the output of the above code is: This is the isset output.
PHP loop
1.while
<?php $int = 1; while($int<10){ $int++; echo $int; } ?>A
2.do while
<?php $int = 1; do{ $int++; echo $int; echo '<hr>'; }while($int < 10) ?>
3.for
<?php $int = 1; for($int;$int<10;$int++){ echo $int; echo '<hr>'; } ?>
4.continue
End the current cycle and enter the next cycle.
5 and 6 are output together.
<?php $int = 1; for($int;$int<10;$int++){ echo $int; if($int==5){ continue; } echo '<hr>'; } ?>
5.break
At the end of the loop, you can jump out of the multi-layer loop.