Basic understanding of PHP code

PHP basics

language tag

<h1>My Name is XIU!</h1>
<script>
console.log("This message is from info.php!")
</script>
<?php // PHP 的开始标记,表示从此标记开始,进入PHP 模式。
phpinfo(); // PHP 代码
// ;是PHP 的结束标记,表示从开始标记到结束标记,之间的内容是
PHP 模式
// 之后的内容依然被PHP 解释引擎认为是纯文本字符串。
?>

illustrate:

In PHP files, it can be mixed with HTML and JavaScript.

Start tag <?php logo to enter PHP mode,

The closing tag ?> indicates exiting PHP mode.

Content outside PHP mode will be output to the browser as characters.

HTML or JavaScript are just plain text strings to PHP.

<?php
phpinfo();//小括号表示调用函数
?>//所有内容在<?php?>//标签中编写

Security issues: If an online website can see the phpinfo page, it means there is a sensitive information leakage vulnerability. This vulnerability needs to be reflected in the subsequent penetration test report.

PHP script starts with <?php** and ends with **?>:

<?php
 // PHP 代码
 ?>

The default file extension for PHP files is ".php".

PHP files usually contain HTML tags and some PHP script code.

basic grammar

command separator

;Indicates the end of a PHP code

Comment

single line comment

phpinfo(); // 该函数会显示PHP 配置信息

Multi-line comments

/*
这是多行注释!
以下语句表示PHP 配置信息
*/
phpinfo();

variable

variable rules

  • Variables start with a $ sign followed by the name of the variable
  • Variable names must start with a letter or underscore character
  • Variable names can only contain letters, numbers, and underscores (Az, 0-9, and _ )
  • Variable names cannot contain spaces
  • Variable names are case-sensitive ($y and $Y are two different variables)

Naming rules

When naming variables, it is recommended to use camel case naming or underline naming so that the variable name can be "understood by its name".

$firstName;
$LastName;
$first_name;
$last_name;

Variable declaration and initialization

Directly assign value and use directly.

echo $username; 
$username = "XIU";
echo $username;

variable release

The unset() function releases the specified variable.

$username "XIU";
echo $username;
unset($username);
echo $username;

mutable variable

Variable variables, the name of a variable can be dynamically set and used,A mutable variable is a special variable whose name is determined by the value of another variable.$ is a feature of PHP and one of the reasons for variable coverage vulnerabilities in PHP.

<?php

    $username = "xujie";	//定义了一个变量叫$username,将$username这个变量的值设为字符串的xujie

    $xujie = "芮城";		//定义了一个变量叫作$xujie将他的值设置为芮城

    echo $$username;    //$$username就是可变变量,在已声明的变量$username前又加上了一个变量符
    // $($username) -> $xujie
?>

Variable type

<?php
$name = "XIU";		//string类型
$sex = true;		//bool类型
$age = 24;			//int类型
$score = 59.9;		//float类型

var_dump ($name);	//输出变量类型
?>
bool type

Boolean type: represents true or false,

int type

Integer type, stores integers

float type

Floating point type, also called double, refers to a number containing decimals.

string type

A string is an ordered sequence of characters. Strings can be defined using three methods: single quotes, double quotes, and delimiters.

Single quotes define strings:

Single quotes cannot be included in the string defined by single quotes. If they are included, \' needs to be escaped.

Variables enclosed in single quotes will not be recognized.

$name = 'XIU';
$msg = 'Welcome, ';
echo $msg.$name;

Double quotes define strings:

Double quotes can contain single quotes, and double quotes contain double quotes, which need to be escaped with \".

PHP will parse variables between double quotes, it is best to surround the variable name with {}.

$username = "XIU";
$message = "Welcome, {
      
      $username}";
echo $message;

Delimiter definition string:

When you need to define a large string to output, and there are too many special characters in the string, including single quotes, double quotes, etc.

Single and double quotation marks in the string defined by the delimiter, as well as most special characters, do not need to be escaped.

The delimiter starts with the identifier <<< + flag. Capital letters are recommended for the flag and have certain semantics.

The delimiter ends the identifier flag + ;. After the end identifier, there can only be a line feed and carriage return.

The naming rules for logos include letters, numbers, and underscores, and cannot start with a number.

The $ in the delimiter will also be recognized as a variable, and the variable name must be enclosed in {}.

$name = "XIU";
$msg = <<<XIU
<h1>My Name is <span style = "color:pink">{
      
      $name}</span>!</h1>
<script>
console.log('This message is from info.php!')
</script>
XIU;
echo $msg;

constant

A constant is an identifier for a simple value, as its name implies. During PHP script execution a constant once

Once defined, it cannot be changed or undefined. The scope of constants is global. Constants contain data of bool, int, float, and string types.

Definition and use

The naming of constants is similar to variables, and also follows the naming rules of PHP identifiers. By convention, constant identifiers are always uppercase.

// define.php
define("USERNAME", "XIU");
// USERNAME = 'XIU';
// unset("USERNAME");
echo USERNAME;

Variables usually start with a $ symbol, and constants are usually capitalized.

Predefined constants

PHP has defined constants that can be used directly, generally representing special meanings. Magic constants are a special type of predefined constants.

constant name constant value
FILE Current file name, full path
LINE current line number
FUNCTION current function name
CLASS current class name
METHOD The method name of the current object
PHP_OS Operating system type
PHP_VERSION Current PHP version
DIRECTORY_SEPARATOR Directory separator

operator

arithmetic operator

example name result
-$a reconciliation Negative value of $a
$a + $b add up a and a andsum of a and b
$a - $b Subtract a and a anddifference between a and b
$a * $b Multiply a and a andProduct of a and b
$a / $b divide a divided by a divided byQuotient of a divided by b
$a % $b Take the modulus, take the remainder a divided by a divided byRemainder of dividing a by b
++ $a Add before The value of a is increased by one, and then the value of a is increased by one, and then returnedAdd one to the value of a , then return a
$a ++ add later return a , then a , thena , then add one to the value of a
– $a Subtract before The value of a is decremented by one, and then returned. The value of a is decremented by one, and then returnedDecrement the value of a by one, and then return a
$a – Subtract return a , then a , thena , then the value of a is reduced by one

String operators

The main work performed by string operators is string concatenation.
In JS language, string concatenation uses + operator. Its function is to access the properties or methods in the object.
In PHP, use .string concatenation. .Also called connection operator in PHP .

<meta charset "utf-8">
<h1>字符串连接符</h1>
<?php
$name="XIU";
$str = "Hello,".Sname;
//$str "Hello,"+Sname;
var_dump($str);
?>

assignment operator

The operand on the left must be a variable, and the operand on the right can be an expression, a value, a variable, etc.

Function: Assign the value of the expression on the right to the variable on the left.

operator explain
= Assignment
+= a+=b:a=a+b
-= a-=b:a=a-b
*= a*=b:a=a*b
/= a/=b:a=a/b
%= a%=b:a=a%b
.= a.=b: a=aconnectionb

comparison operator

After comparing the operands (according to the requirements and rules of the comparison operator), the return value (boolean) is obtained.
If the comparison result meets the requirements of the comparison operator, the result is true (true, established, satisfied), otherwise it is false (false, not established, not satisfied
).

operator illustrate Remark
$a == $b equal
$a === $b congruent Values ​​are equal and data types are the same
$a != $b range
$a <> $b 不等
$a !== $b 不全等
$a < $b 小于
$a > $b 大于
$a <= $b 小于等于
$a >= $b 大于等于

PHP 中规定:使用echo 输出布尔类型值的时候 echo true ; 它在页面中会输出1。echo false ; 它会在页面中什么都不输出。建议使用var_dump() 来输出更明了的结果。

逻辑运算符

运算符 例子 解释 运算符法则
and && $a and $b $a && $b 逻辑与运算
or || $a or $b a ∣ ∣ a|| a∣∣b 逻辑或运算
xor $a xor $b 逻辑异或运算 相同为假,不同为真
!$a 逻辑非运算

其他运算符

?:
$a = 10;
$b = 20;
$max = $a > $b ? $a : $b;
echo $max;
反引号

自动适配系统命令,调用系统命令。

$cmd = "whoami";
$cmd = "ipconfig";
$cmd = "net user";
echo "<pre>".`$cmd`;
@

屏蔽表达式可能发生的错误。

流程控制

顺序执行

自上而下的执行即可,这也是PHP 语句的默认执行过程。对这个执行过程没有控制。

echo "This is first echo";
echo "<br />";
echo "This is second echo";
echo "<br />";
echo "This is third echo";

分支执行

分支执行可以根据表达式的值是否为True 来选择执行某些代码,PHP 的分支执行主要通过if 和switch 来实现。

if语句

if(){
    
    //判断条件,如果判断条件的返回值为True,则执行语句块1,否则执行语句块2
//语句块1
}else{
    
    
//语句块2
}
单向条件
// if.php
$score = 59.9;
if($score >= 60 ){
    
    
echo "congratulations";
}
双向条件
// if.php
$score = 99.9;
if($score >= 60 ){
    
    
echo "congratulations";
}else{
    
    
echo "Sorry, you may need help!";
}
多向条件

成绩定级脚本。

switch语句

功能与if 语句类似,当需要多向分支结构时,并且判断条件是具体的某个值,此时大多使用switch 结构。

<?php
$day = 7;
switch($day){
    
    
case 1:
echo "Monday";
break;
case 2 :
echo "Tuesday";
break;
case 3 :
echo "Wednesday";
break;
case 4 :
echo "Thursday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday!";
break;
case 7:
echo "Sunday!";
break;
default:
echo "Error!";
}
?>

注意:

表达式的值最好是整形或者字符串。

不要忘记break 语句,用来跳出switch 语句。每个case 语句后面都加上break。

如果某个case 语句后⾯并没有接语句块,那么就说明这个语句块的内容是同下。

case 后面的语句块是不需要 {} 括起来的。

判断条件

在编写if 语句时,要求判断条件是⼀个布尔类型的值。但是实际应用中,是无法保障

这⼀点。如果判断条件是非布尔类型的值,就需要强制转换成布尔类型的值。如下:

// if.php
$flag = 0;
$flag = 0.0;
$flag = "";
$flag = "0";
$flag = array();
$flag = NULL;
$flag = "XIU";
$flag = true;
$flag = 24;
$flag = 59.9;
if($flag){
    
    
echo var_dump($flag)." means Ture";
}else{
    
    
echo var_dump($flag)." means False";
}

循环执行

while循环

while(表达式){
    
    //当表达式的值为真的时候,执行循环体。
//循环体
}

当表达式的值为true,就执行下面的循环体。当循环体执行结束之后,继续判断表达式的值是true 还是false,如果还是true,那么就继续执行循环体。直到,这个表达式的值为false,那么这个while 语句就执行结束了。

do while循环

// do-while.php
$a = 1;
do{
    
    
echo $a."<br />";
$a ++;
}while ($a <= 10);

for循环

for(;;){
    
    //计数器;判断条件;自增或自减
//循环体
}

特殊流程

break

break 用于switch,for,while,do…while,foreach 等的中断。后面可以接上⼀个数字来表示跳出几层循环。默认不加就是跳出当前循环语句。

for ($i=1; $i <= 100; $i++) {
    
    
if ($i % 17 == 0) {
    
    
break;
}
echo $i."<br />";
}
echo "PHP is DONE!";

continue

continue 只能用在循环语句,轮空本次循环,并不是结束整个循环语句。

// continue.php
for ($i=1; $i <= 100; $i++) {
    
    
if ($i % 17 == 0) {
    
    
continue;
}
echo $i."<br />";
}
echo "PHP is DONE!";

exit

exit 表示结束当前整个PHP 脚本的执行。同die() 语句。

// exit.php
for ($i=1; $i <= 100; $i++) {
    
    
if ($i % 17 == 0) {
    
    
// exit;
// exit("PHP is OVER");
// die("PHP is OVER");
die();
}
echo $i."<br />";
}
echo "PHP is DONE!";

Guess you like

Origin blog.csdn.net/weixin_58954236/article/details/132443988