PHP入门

· · 个人记录

开始、结束

PHP语言通常已<?php开始,?>结束。 例如:

<?php
    //主体
?>

begin and end

PHP usually starts with <?php and ends with ?>.

for example:

<?php
    //main
?>

输入、输出

在洛谷中,PHP如果想输入题目数据可以使用fgets(STDIN);来达到数据内容,如果一行中有多个数据,可以用list(变量1,变量2……)=explode(分段的字符,数据);来获得一行中的变量。

PHP可以使用echo来输出,例如:

<?php
    echo "hello,world!";
?>

会输出hello,world

input and output

In Luogu, if PHP wants to input question data, you can use fgets(STDIN); to get the data content. If there are multiple data in a line, you can use list(variable 1, variable 2...) = explode(segmented characters, data); to get the variables in a line.

PHP can use echo to output, for example:

<?php
    echo "hello,world!";
?>

注释

PHP可以使用///* */#来注释,例如:

<?php
    //注释1
    /*注释2
    注释2的第2行*/
    #注释3
?>

notes

PHP can use //, /* */, # to comment, for example:

<?php
    //Notes1
    /*Notes2
    line2 of Notes2*/
    #Notes3
?>

变量

PHP的变量有很多类型,如下表:

数据类型 对应的中文 English
boolen 布尔型 Boolean
string 字符串 String
integer 整型 Integer
float 浮点型 Decimal
array 数组 Arrays
object 对象 Object

PHP可以用$变量名=变量的内容;来创建变量,例如:

<?php
    $a=1;//创建变量a,并赋值为1
?>

如果数据类型要强制转换,可以用$变量名=(数据类型)$变量名,例如:

<?php
    $a="123";
    $a=(integer)$a;//现在a等于123,不等于"123"
?>

variable

There are many types of PHP variables, as shown in the following table: Type English
boolen Boolean
string String
integer Integer
float Decimal
array Arrays
object Object

PHP can create variables using $variable name = variable content;, for example:

<?php
    $a=1;//Create variable a and assign it a value of 1
?>

If the data type needs to be converted, you can use $variable name=(data type)$variable name, for example:

<?php
    $a="123";
    $a=(integer)$a;//Now a is equal to 123, not equal to "123";
?>

数组

PHP的数组定义方法有很多,例如:

<?php
    arr1=Array(0=>1,1=>2,2=>3);
    arr2=Array(1,2,3);
    arr3={1,2,3};
    arr4=list(1,2,3);
?>

这些数组中的数字都为1、2、3。

PHP中可以用$数组名称[下标]来获得数组中的内容。

array

There are many ways to define arrays in PHP, such as:

<?php
    arr1=Array(0=>1,1=>2,2=>3);
    arr2=Array(1,2,3);
    arr3={1,2,3};
    arr4=list(1,2,3);
?>

The numbers in these arrays are all 1, 2, and 3.

In PHP, you can use $array name [subscript] to get the contents of the array.

函数

PHP可以用function 函数名($参数1,$参数2……){函数主体;return 返回值;}来定义函数(测试和返回值可以没有),例如:

<?php
    function add($a,$b){//创建函数add
        return $a+$b;//返回参数a+参数b
    }
    $a=1;$b=1;
    echo add($a,$b);//结果为2
?>

function

PHP can use function function name ($parameter1, $parameter2 ...) {function body; return return value;} to define a function (test and return value are optional), for example: 有),例如:

<?php
    function add($a,$b){//Create function add
        return $a+$b;//Return parameter a + parameter b
    }
    $a=1;$b=1;
    echo add($a,$b);//output:2
?>

运算符

运算符 说明 实例 等同于 解释
= 赋值 $a=b \$a=b 将b赋值给$a
+= $a+=b \a=a+b 将$a加上b
-= $a-=b \a=a-b 将$a减去b
*= $a*=b \a=a*b 将$a乘上b
/= $a/=b \a=a/b 将$a除以b
.= 连接字符串 $a.=b \a=a.b 将b连接在$a的后面
% 取余数 $a%b \a=a%b $a除以b的余数
运算符 说明 解释
&& 都真才真
|| 都假才假
! 取反

逻辑运算符请参考C++。

operators

Operators Illustrate Examples Equivalent to Explain
= Assignment $a=b \$a=b Assign b to $a
+= plus $a+=b \a=a+b Add $a to b
-= mins $a-=b \a=a-b Subtract b from $a
*= multiply $a*=b \a=a*b Multiply $a by b
/= Divide by $a/=b \a=a/b Divide $a by b
.= Connection String $a.=b \a=a.b Connect b to the end of $a
% Take the remainder $a%b \a=a%b $a divided by b
Operators illustrate explain
&& and Only true return true
|| or Only false return false
! not Settlement

For logical operators, please refer to C++.

if、else if和else

PHP中可以用if(条件1){}else if(条件2){}……else{}来做一些判断,例如:

<?php
    $a=56;
    if($a<10){
        echo "$a<10";
    }else if($a<100){
        echo "$a<100"
    }else{
        echo "$a>100"
    }
?>

输出:$a<100

if,else if,and else

In PHP, you can use if (condition 1) {} else if (condition 2) {} ... else {} to make some judgments, for example:

<?php
    $a=56;
    if($a<10){
        echo "$a<10";
    }else if($a<100){
        echo "$a<100"
    }else{
        echo "$a>100"
    }
?>

output: $a<100

for和while

PHP中的for和while跟C++的很相似,接下来就举两个例子。

例子1:

<?php
    $a={1,2,3,4,5,6,7,8,9,0};
    for($i=0;$i<10;$i++){
        echo $a[$i];
        echo " ";
    }
?>

输出:1 2 3 4 5 6 7 8 9 0

例子2:

<?php
    $i=0;
    while(i<=10){
        $i++;
        echo $i;
    }
?>

输出:12345678910

for and while

The for and while in PHP are very similar to those in C++. Here are two examples.

Example 1:

<?php
    $a={1,2,3,4,5,6,7,8,9,0};
    for($i=0;$i<10;$i++){
        echo $a[$i];
        echo " ";
    }
?>

output:1 2 3 4 5 6 7 8 9 0

Example 2:

<?php
    $i=0;
    while(i<=10){
        $i++;
        echo $i;
    }
?>

output:12345678910

PHP字符串函数

strlen

strlen() 函数返回字符串的长度,以字符计。

下例返回字符串 "Hello world!" 的长度:

<?php
    echo strlen("Hello world!");
?>

输出:12

str_word_count()

PHP str_word_count() 函数对字符串中的单词进行计数,例如:

<?php
    echo str_word_count("Hello world!");
?>

输出:2

strrev()

PHP strrev() 函数反转字符串,例如:

<?php
    echo strrev("Hello world!");
?>

输出:!dlrow olleH

strpos()

strpos() 函数用于检索字符串内指定的字符或文本。

如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。

下例检索字符串 "Hello world!" 中的文本 "world",例如:

<?php
    echo strpos("Hello world!","world");
?>

输出:6

提示:上例中字符串 "world" 的位置是 6。是 6(而不是 7)的理由是,字符串中首字符的位置是 0 而不是 1。

replace()

PHP str_replace() 函数用一些字符串替换字符串中的另一些字符。

下面的例子用 "Kitty" 替换文本 "world":

<?php
    echo str_replace("world", "Kitty", "Hello world!");
?>

输出:Hello Kitty!

完整手册

函数 描述
addcslashes() 返回在指定的字符前添加反斜杠的字符串。
addslashes() 返回在预定义的字符前添加反斜杠的字符串。
bin2hex() 把 ASCII 字符的字符串转换为十六进制值。
chop() 删除字符串右侧的空白字符或其他字符。
chr() 从指定的 ASCII 值返回字符。
chunk_split() 把字符串分割为一系列更小的部分。
convert_cyr_string() 把字符串由一种 Cyrillic 字符集转换为另一种。
convert_uudecode() 解码 uuencode 编码字符串。
convert_uuencode() 使用 uuencode 算法对字符串进行编码。
count_chars() 返回有关字符串中所用字符的信息。
crc32() 计算字符串的 32 位 CRC。
crypt() 单向的字符串加密法(hashing)。
echo() 输出一个或多个字符串。
explode() 把字符串打散为数组。
fprintf() 把格式化的字符串写入到指定的输出流。
get_html_translation_table() 返回由 htmlspecialchars() 和 htmlentities() 使用的翻译表。
hebrev() 把希伯来文本转换为可见文本。
hebrevc() 把希伯来文本转换为可见文本,并把新行(\n)转换为 <br>。
hex2bin() 把十六进制值的字符串转换为 ASCII 字符。
html_entity_decode() 把 HTML 实体转换为字符。
htmlentities() 把字符转换为 HTML 实体。
htmlspecialchars_decode() 把一些预定义的 HTML 实体转换为字符。
htmlspecialchars() 把一些预定义的字符转换为 HTML 实体。
implode() 返回由数组元素组合成的字符串。
join() implode() 的别名。
lcfirst() 把字符串的首字符转换为小写。
levenshtein() 返回两个字符串之间的 Levenshtein 距离。
localeconv() 返回本地数字及货币格式信息。
ltrim() 移除字符串左侧的空白字符或其他字符。
md5() 计算字符串的 MD5 散列。
md5_file() 计算文件的 MD5 散列。
metaphone() 计算字符串的 metaphone 键。
money_format() 返回格式化为货币字符串的字符串。
nl_langinfo() 返回特定的本地信息。
nl2br() 在字符串中的每个新行之前插入 HTML 换行符。
number_format() 以千位分组来格式化数字。
ord() 返回字符串中第一个字符的 ASCII 值。
parse_str() 把查询字符串解析到变量中。
print() 输出一个或多个字符串。
printf() 输出格式化的字符串。
quoted_printable_decode() 把 quoted-printable 字符串转换为 8 位字符串。
quoted_printable_encode() 把 8 位字符串转换为 quoted-printable 字符串。
quotemeta() 引用元字符。
rtrim() 移除字符串右侧的空白字符或其他字符。
setlocale() 设置地区信息(地域信息)。
sha1() 计算字符串的 SHA-1 散列。
sha1_file() 计算文件的 SHA-1 散列。
similar_text() 计算两个字符串的相似度。
soundex() 计算字符串的 soundex 键。
sprintf() 把格式化的字符串写入变量中。
sscanf() 根据指定的格式解析来自字符串的输入。
str_getcsv() 把 CSV 字符串解析到数组中。
str_ireplace() 替换字符串中的一些字符(对大小写不敏感)。
str_pad() 把字符串填充为新的长度。
str_repeat() 把字符串重复指定的次数。
str_replace() 替换字符串中的一些字符(对大小写敏感)。
str_rot13() 对字符串执行 ROT13 编码。
str_shuffle() 随机地打乱字符串中的所有字符。
str_split() 把字符串分割到数组中。
str_word_count() 计算字符串中的单词数。
strcasecmp() 比较两个字符串(对大小写不敏感)。
strchr() 查找字符串在另一字符串中的第一次出现。(strstr() 的别名。)
strcmp() 比较两个字符串(对大小写敏感)。
strcoll() 比较两个字符串(根据本地设置)。
strcspn() 返回在找到某些指定字符的任何部分之前,在字符串中查找的字符数。
strip_tags() 剥去字符串中的 HTML 和 PHP 标签。
stripcslashes() 删除由 addcslashes() 函数添加的反斜杠。
stripslashes() 删除由 addslashes() 函数添加的反斜杠。
stripos() 返回字符串在另一字符串中第一次出现的位置(对大小写不敏感)。
stristr() 查找字符串在另一字符串中第一次出现的位置(大小写不敏感)。
strlen() 返回字符串的长度。
strnatcasecmp() 使用一种"自然排序"算法来比较两个字符串(对大小写不敏感)。
strnatcmp() 使用一种"自然排序"算法来比较两个字符串(对大小写敏感)。
strncasecmp() 前 n 个字符的字符串比较(对大小写不敏感)。
strncmp() 前 n 个字符的字符串比较(对大小写敏感)。
strpbrk() 在字符串中查找一组字符的任何一个字符。
strpos() 返回字符串在另一字符串中第一次出现的位置(对大小写敏感)。
strrchr() 查找字符串在另一个字符串中最后一次出现。
strrev() 反转字符串。
strripos() 查找字符串在另一字符串中最后一次出现的位置(对大小写不敏感)。
strrpos() 查找字符串在另一字符串中最后一次出现的位置(对大小写敏感)。
strspn() 返回在字符串中包含的特定字符的数目。
strstr() 查找字符串在另一字符串中的第一次出现(对大小写敏感)。
strtok() 把字符串分割为更小的字符串。
strtolower() 把字符串转换为小写字母。
strtoupper() 把字符串转换为大写字母。
strtr() 转换字符串中特定的字符。
substr() 返回字符串的一部分。
substr_compare() 从指定的开始位置(二进制安全和选择性区分大小写)比较两个字符串。
substr_count() 计算子串在字符串中出现的次数。
substr_replace() 把字符串的一部分替换为另一个字符串。
trim() 移除字符串两侧的空白字符和其他字符。
ucfirst() 把字符串中的首字符转换为大写。
ucwords() 把字符串中每个单词的首字符转换为大写。
vfprintf() 把格式化的字符串写到指定的输出流。
vprintf() 输出格式化的字符串。
vsprintf() 把格式化字符串写入变量中。
wordwrap() 打断字符串为指定数量的字串

PHP String Functions

strlen

The strlen() function returns the length of a string, in characters.

The following example returns the length of the string "Hello world!":

<?php
    echo strlen("Hello world!");
?>

output:12

str_word_count()

The PHP str_word_count() function counts the words in a string, for example:

<?php
    echo str_word_count("Hello world!");
?>

output:2

strrev()

The PHP strrev() function reverses a string, for example:

<?php
    echo strrev("Hello world!");
?>

output:!dlrow olleH

strpos()

The strpos() function is used to retrieve specified characters or text within a string.

If a match is found, the character position of the first match is returned. If no match is found, FALSE is returned.

The following example retrieves the text "world" from the string "Hello world!", for example:

<?php
    echo strpos("Hello world!","world");
?>

output:6

Hint: In the above example, the position of the string "world" is 6. The reason it is 6 (and not 7) is that the position of the first character in a string is 0, not 1.

replace()

The PHP str_replace() function replaces some characters in a string with some other characters. The following example replaces the text "world" with "Kitty":

<?php
    echo str_replace("world", "Kitty", "Hello world!");
?>

output:Hello Kitty!

Full manual

Function Description
addcslashes() Returns a string with a backslash added to the beginning of the specified character.
addslashes() Returns a string with a backslash added to the beginning of a predefined character.
bin2hex() Converts a string of ASCII characters to their hexadecimal values.
chop() Removes whitespace or other characters from the right side of a string.
chr() Returns the character from the specified ASCII value. chunk_split() Splits a string into a series of smaller parts.
convert_cyr_string() Converts a string from one Cyrillic character set to another.
convert_uudecode() Decodes a uuencode-encoded string. convert_uuencode() Encodes a string using the uuencode algorithm. count_chars() Returns information about the characters used in a string. crc32() Calculates a 32-bit CRC of a string.
crypt() One-way string encryption (hashing).
echo() Outputs one or more strings. explode() Explodes a string into an array.
fprintf() Writes a formatted string to the specified output stream.
get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities().
hebrev() Converts Hebrew text to visible text.
hebrevc() Converts Hebrew text to visible text and converts newlines (\n) to <br>.
hex2bin() Converts a string of hexadecimal values to ASCII characters.
html_entity_decode() Converts HTML entities to characters.
htmlentities() Converts characters to HTML entities.
htmlspecialchars_decode() Converts some predefined HTML entities to characters.
htmlspecialchars() Converts some predefined characters to HTML entities.
implode() Returns a string composed of the elements of an array.
join() Alias for implode().
lcfirst() Converts the first character of a string to lowercase.
levenshtein() Returns the Levenshtein distance between two strings.
localeconv() Returns locale numeric and currency formatting information.
ltrim() Remove whitespace or other characters from the left side of a string.
md5() Computes the MD5 hash of a string.
md5_file() Computes the MD5 hash of a file.
metaphone() Computes the metaphone key of a string.
money_format() Returns a string formatted as a money string.
nl_langinfo() Returns locale-specific information.
nl2br() Inserts HTML line breaks before each newline in a string.
number_format() Formats a number with thousands groups.
ord() Returns the ASCII value of the first character in a string.
parse_str() Parses a query string into a variable.
print() Prints one or more strings.
printf() Prints a formatted string.
quoted_printable_decode() Converts a quoted-printable string to an 8-bit string.
quoted_printable_encode() Converts an 8-bit string to a quoted-printable string.
quotemeta() Quotes metacharacters.
rtrim() Removes whitespace or other characters from the right side of a string.
setlocale() Sets locale information.
sha1() Computes the SHA-1 hash of a string.
sha1_file() Calculates the SHA-1 hash of a file.
similar_text() Calculates the similarity of two strings.
soundex() Calculates the soundex key of a string.
sprintf() Writes a formatted string to a variable.
sscanf() Parses input from a string according to a specified format.
str_getcsv() Parses a CSV string into an array.
str_ireplace() Replaces some characters in a string (not case sensitive).
str_pad() Pads a string to a new length. str_repeat() Repeats a string a specified number of times.
str_replace() Replaces some characters in a string (not case sensitive).
str_rot13() Performs ROT13 encoding on a string.
str_shuffle() Randomly shuffles all the characters in a string.
str_split() Splits a string into an array.
str_word_count() Counts the number of words in a string. strcasecmp() Compares two strings (not case sensitive).
strchr() Finds the first occurrence of a string within another string. (Alias of strstr().)
strcmp() Compares two strings (case sensitive).
strcoll() Compares two strings (according to locale settings).
strcspn() Returns the number of characters to search for in a string before finding
strip_tags() Strips HTML and PHP tags from a string. stripcslashes() Removes backslashes added by the addcslashes() function.
stripslashes() Removes backslashes added by the addslashes() function.
stripos() Returns the position of the first occurrence of a string within another string (not case sensitive).
stristr() Finds the position of the first occurrence of a string within another string (not case sensitive).
strlen() Returns the length of a string.
strnatcasecmp() Compares two strings using a "natural sorting" algorithm (not case sensitive).
strnatcmp() Compares two strings using a "natural sorting" algorithm (not case sensitive).
strncasecmp() Compares the first n characters of a string (not case sensitive).
strncmp() Compares the first n characters of a string (not case sensitive).
strpbrk() Search a string for any character in a set of characters.
strpos() Returns the position of the first occurrence of a string within another string (not case sensitive).
strrchr() Finds the last occurrence of a str
strrev() Reverses a string.
strripos() Finds the position of the last occurrence of a string within another string (not case sensitive).
strrpos() Finds the position of the last occurrence of a string within another string (case sensitive).
strspn() Returns the number of a specific character contained in a string.
strstr() Finds the first occurrence of a string within another string (case sensitive).
strtok() Splits a string into smaller strings.
strtolower() Converts a string to lowercase.
strtoupper() Converts a string to uppercase.
strtr() Converts specific characters within a string.
substr() Returns a portion of a string.
substr_compare() Compares two strings starting at a specified starting position (binary safe and selectively case sensitive).
substr_count() Counts the number of occurrences of a substring within a string.
substr_replace() Replaces a portion of a string with another string.
trim() Removes whitespace and other characters on both sides of a string.
ucfirst() Converts the first character
\textcolor{white}{https://note.ms/luoguzhuanlanPHP}