This article brings you PHP introductory notes. The knowledge points recorded in it are very detailed. As a language program, the specificity of PHP language gradually becomes apparent in the application process. I hope everyone can feel the charm of PHP from it. ,I hope everyone has to help.
PHP (hypertext preprocessor) was originally the abbreviation of Personal Home Page and has been officially renamed "PHP :Hypertext Preprocessor". Since the domestic Internet began to develop in the 1990s, Internet information has covered almost all areas of knowledge in our daily activities, and has gradually become an indispensable part of our lives, studies, and work. According to statistics, since 2003, the size of my country's web pages has basically maintained a doubling growth rate, and is showing an upward trend. As the most popular website program development language today, PHP language has the advantages of low cost, fast speed, good portability, and rich built-in function libraries. Therefore, it is used by more and more companies in website development. However, with the continuous upgrading of the Internet, many problems have arisen in the PHP language.
According to the requirements of dynamic websites, as a language program, the specificity of PHP language gradually appears in the application process, and its technical level will directly affect the operating efficiency of the website. Its characteristics are that it has an open source code and is highly similar to general-purpose languages such as C language in terms of programming. Therefore, it is easy to understand and has strong operability during operation. At the same time, the PHP language has a high level of data transmission, processing and output, and can be widely used in Windows systems and various types of Web servers. If the amount of data is large, the PHP language can also broaden the link surface and connect to various databases to alleviate the pressure of data storage, retrieval and maintenance. With the development of technology, PHP language search engines can also be tailored to provide personalized services, such as classifying, collecting and storing data according to customer preferences, which greatly improves data operation efficiency.
(1) Open source and free nature
Since the source code of the PHP interpreter is public, the security factor is relatively high High-end websites can change the PHP interpreter themselves. In addition, the use of the PHP runtime environment is also free.
(2) Quickness
PHP is a language that is very easy to learn and use. Its syntax features are similar to C language, but it does not have the complex address operations of C language. Moreover, the concept of object-oriented is added, and it has concise grammatical rules, making it very simple to operate and edit, and very practical.
(3) Extensibility of database connections
PHP can establish connections with many mainstream databases, such as MySQL, ODBC, Oracle, etc. PHP uses different compiled functions to establish connections with these databases For connection purposes, PHPLIB is a commonly used base library provided for general transactions.
(4) Use process-oriented and object-oriented together
In the use of PHP language, you can use process-oriented and object-oriented respectively, and you can mix PHP process-oriented and object-oriented together. , which is something that many other programming languages cannot do.
(1) Popular and easy to use
PHP is currently the most popular programming language, there is no doubt about it. It drives more than 200 million websites around the world, and more than 81.7% of the world's public websites use PHP on the server side. PHP's commonly used data structures are all built-in. It is convenient and simple to use, not complicated at all, and its expression ability is quite flexible.
(2) There are many development positions
In server-side website programming, PHP will more easily help you find a job. Many Internet-related companies are using the PHP development framework, so it can be said that the market demand for PHP development programmers is still relatively large.
(3) Still developing
PHP is constantly compatible with technologies such as closures and namespaces, while taking into account performance and currently popular frameworks. After version 7, it has been providing higher performance applications.
(4) Strong implantability
During the patch vulnerability upgrade process of PHP language, the core part of the PHP language is easy to implement and fast to implant.
(5) Strong scalability
During the database application process, the PHP language can retrieve various types of data from the database and has high execution efficiency.
(1) PHP’s interpretation and operation mechanism
In PHP, all variables are page-level, whether they are global variables , or static members of the class, will be cleared after the page is executed.
(2) Design flaws and lack of attention PHP is called an opaque language because there is no stack trace and various fragile inputs. There is no clear design philosophy. Early PHP was influenced by Perl, the standard library with out parameters was introduced from C language, and the object-oriented part was learned from C and Java.
(3) Poor support for recursion
PHP is not good at recursion. The limit on the number of recursive functions it can tolerate is significantly smaller than that of other languages.
$array=NAME; Variable names must start with letters or underscores, not numbers, and no spaces in the middle! Case Sensitive!
$x=5; $X=7; The output is different results<?php $a = 1; $A = 2; echo $a . "<br>"; echo $A; ?>
<?php $a=1; //全局变量 function test() { //声明一个函数,名字为 test $a=15; //函数内的变量,为局部变量 echo "内部输出结果:".$a; } test(); echo "<br>"; echo "外部输出结果:".$a; ?>
<?php $a=1; function test() { //声明一个函数,名字为 test global $a; //在函数内声明并引用外部变量,注意这里是小写 global echo $a; echo "<br>"; } test(); //使用函数 echo $a; ?>
<?php $a=10; $b=20; $c=30; function test() { $a=100; $GLOBALS['a']=$GLOBALS['b']+$GLOBALS['c']; //引用全局变量,这里的 GLOBALS 必须大写 echo $a."<br>"; } Test(); //函数名不区分大小写 echo "<br>"; echo $a; ?>
<?php function test() { static $a = 0; //静态作用域,保留变量值 echo $a . ""; $a++; } test();//不使用static的话,每次输出都是 0 echo "<br>"; test(); echo "<br>"; test(); ?>
<?php $a = 10; $b = 20; function test() { $a = 30; $b = 40; $c = $a + $b; echo "函数内运算值:".$c; echo "<br>"; } test(); $c = $a + $b; echo "函数外运算值:".$c; ?>
<?php $a = "1111"; $b = 123; $c = null; echo var_dump($a) . "<br>"; echo var_dump($b) . "<br>"; echo var_dump($c) . "<br>"; $d = array('a', 1, abc); //数组 echo var_dump($d) . "<br>"; $e = true; echo var_dump($e); ?>
<?php $text1="ni hao"; $text2="hahaha"; echo $text1." ".$text2."<br>"; //引用多个变量用 点 . (英文)连接 为空格 $a = strlen($text1);//计算字符串长度数 echo $a; ?>//空格也算作一个字符
<?php $text1 = "ni hao"; echo "$text1" . "<br>";// “ ” 双引号输出会显示变量值 echo '$text1';// ‘’ 单引号会显示变量名本身 ?>
<?php $x = 10; echo ++$x;//先自增后输出 echo "<br>"; $a=15; echo --$a . "<br>";//先自减后输出 $y = 20; echo $y++ . "<br>";//先输出后自增 $b=30; echo $b--;//先自增后输出 ?>
<?php $x = 10; $y = 20; if ($x > $y) {//判断 x 和 y 的大小,如果满足 $x > $y 就输出 true echo "true"; }else if($x==$y) {//else if 如果上面的判断不满足,再次判断 $x==$y 是否相等 ,如果满足就输出 $x."等于"$y; echo $x."等于"$y; } else {//如果上面的判断都不满足就输出 false echo "false"; } ?>
<?php $x= (3>4)? "true":"false";//三元运算符 echo $x; ?>
<?php $a="abc"; switch ($a) { case 'red': echo "red"; break; //跳出整个循环体,continue跳出本次循环体,继续执行后面的循环体。 case 'green': echo "green"; break; case 'black': echo "black"; break; default: echo "not color"; } ?>
##Array
<?php $cars = array("BMW", "BinLi", "大众"); echo "I like " . "$cars[0]", " ", "$cars[1]", " ", "$cars[2]"; ?>
<?php $cars = array(); $cars[0] = "BMW"; $cars[1] = "BINlix"; $cars[2] = "大众"; echo "I like " . "$cars[0]", " ", "$cars[1]", " ", "$cars[2]"; ?>
<?php $cars = array(); $cars[0] = "BMW"; $cars[1] = "BINlix"; $cars[2] = "大众"; echo count($cars) . "<br>";// count 返回数组长度 echo "I like " . "$cars[0]", " ", "$cars[1]", " ", "$cars[2]"; ?>
count Get the length of the array
echo $arrayName[3]; //Boolean true prints 1
for Loop through the array:
<?php $cars = array("BMW", "BinLi", "大众"); $arrlength = count($cars); for ($x = 0; $x < $arrlength; $x++) { echo $cars[$x] . "<br>"; }//只输出值 ?>
<?php $cars = array("BMW", "BinLi", "大众"); $arrlength = count($cars); print_r($cars);//数据类型,下标值,值都打印出来 ?>
<?php $age = array("gao" => "30", "li" => "20", "zhang" => "10"); echo "gao is " . " " . $age['gao'] . " " . " years old."; ?>
<?php $age = array(); $age["sun"] = ["20"]; $age["liu"] = ["30"]; $age["zhang"] = ["40"]; print_r($age); ?>
多维数组
<?php $cars = array( array("111", 100, 50), array("222", 200, 100), array("333", 300, 150), ); echo $cars[1][0]; ?>
<?php $age = array(); $age["sun"] = ["20"]; $age["liu"] = ["30"]; $age["zhang"] = ["40"]; // print_r($age); foreach ($age as $key => $value) { echo "name is " . $key . " old " . $value . "<br>"; } ?>
排序:
<?php $cars = array("Blinli", "wzida", "muling"); $x = sort($cars); echo $x; print_r($cars); ?>
<?php $cars = array("Blinli", "wzida", "muling"); $x = rsort($cars); echo $x; print_r($cars); ?>
<?php $x = 10; $y = 29; function add() { // global $x, $y; // $z = $x + $y; // echo $z; $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; echo $GLOBALS['z']; } add(); ?>
PHP $_REQUEST 用于收集 HTML 表单提交的数据
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <form method="POST" action="request.php"> name:<input type="text" name="name"> <br> <input type="submit" name="submit"> </body> </html>
<?php $name = $_REQUEST['name']; echo $name; ?>
循环:
<?php $a = 0; while ($a <= 10) { echo "number is :" . $a . "<br>"; $a++; } ?>
do while 循环:
<?php $i = 0; do { //先循环 $i++; echo "number is :" . $i . "<br>"; } while ($i < 5);//再判断 ?>
<?php function add($x, $y) { $z = $x + $y; return $z; // echo $z; } // add(5, 15); echo "1+16" . "=" . add(1, 16); ?>
函数:
<?php function add() { $x = 10; $y = 20; $z = $x + $y; echo $z; } add(); ?> <?php function add($x, $y) { $z = $x + $y; echo $z; } add(5, 15); ?>
<?php class Person { //定义一个类,名称为 Person 使用驼峰命名法,不能用数字开头,类似于变量的命名方式。 var $name; //定义一个变量,值为空 function say() { //定义一个函数 echo "my name is ".$this->name; } } $xm=new Person; //实例化对象,具体的使用方式 $xm->name="小明"; //对象属性的赋值 $xm->say(); //访问对象的方法 ?>
超级全局变量:
<?php $string = "123"; //变量 define("DEMO", true); //常量 print_r($GLOBALS); //超级全局变量 ?>
$_SERVER['PHP_SELF']; //显示头部信息
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <!-- <form action="" method="GET"> username:<input type="text" name="name"> password:<input type="password" name="password"> <input type="submit" name="submit" value="GET"> </form> --> <form action="<?php echo $_SERVER['HTTP_SELF']; ?>" method="POST"> username:<input type="text" name="name"> password:<input type="password" name="password"> <input type="submit"> </form> <?php echo "提交的内容: <font style='color:red'>" . @$_POST["name"] . @$_POST["password"] . "</font>" ?> </body> </html> //在PHP中通过打印函数引入我们的HTML+CSS+JS
1、mysql PHP<5.5版本 2012年后不建议使用,安全性差
2、mysqli mysql的扩展,mysql的升级版,安全性高,只能用于操作mysql数据库
一、面向对象的方法 NEW
二、面向过程的使用方法
3、PDO PHP DATA OBJECT 目前支持链接数据库类型12种,安全性高,基于面向对象的使用方式。
<?php $hostname="127.0.0.1"; $name="root"; $pass="root"; $db="messagebox"; $conn=mysqli_connect($hostname,$name,$pass,$db); if (!$conn) { die("连接失败".mysqli_connect_error());/* mysqli_connect_error()该函数保存了连接数据库的错误信息 */ } /*create table students( uid int(15) not null auto_increment, name varchar(50) not null, gender char(2), message text, primary key(uid) )*/ $sql="select * from users where id=1 "; // mysqli_query($conn,$sql);执行成功返回true $query_result=mysqli_query($conn,$sql); if ($query_result) { /*如果执行成功就会执行如下的代码*/ $result_numbers=mysqli_num_rows($query_result);/*查询出数据库中记录条数,具体的数据没出来*/ if ($result_numbers>0) { $all_result=mysqli_fetch_all($query_result); /*mysqli_fetch_assoc将查询的结果转换为关联型数组*/ //mysqli_fetch_array()关联型和数字型 //mysqli_fetch_all()数字型 //var_dump($all_result); //$all_result它是一个mysqli_fetch_all处理之后的数据,该数据是一个数组,数组的长度取决于sql的执行 //如果结果是一个记录,则$all_result的长度是1 //如果结果是二个记录,则$all_result的长度是2 //$all_result保存的内容也是数组,需要遍历显示数据内容 foreach ($all_result as $key => $value) { foreach ($value as $key => $value) { echo $value."\n"; } } } else{ echo "没数据!"; } } else{ echo "查询失败".mysqli_error($conn); /* mysqli_error($conn)保存了执行SQL语句的错误信息 */ } mysqli_close($conn); ?>
解决乱码:
echo ""; //指定字符集
或者:
header("Content-Type:text/html;charset=utf-8");
<?php // echo "<meta charset='utf-8'>"; //指定字符集 header("Content-Type:text/html;charset=utf-8"); echo '<form method="POST" action="#"> name:<input type="text" name="name"> psswd:<input type="password" name="pwd"> <input type="submit" name="submit"> </form>'; $name = @$_POST['name']; //$_POST 对应 form method="POST" $pwd = @$_POST['pwd']; //接收数据 if ($name != "" && $pwd != "") { //判断当前提交的数据是否为空,不为空的情况下执行下面的代码 echo "UserName is : " . $name . "<br>"; echo "PassWord is : " . $pwd; } ?>
(isset($name) && isset($pwd))
isset 检测变量是否设置,不为NULL
如果检测的对象是""(空),返回值是 1 false
var_dump 判断数据类型
使用 define(变量的KEY(最好大写),变量的value,true/false,)
define("UNMBER","this is a hacker");//不能用 $ 数字 开头
<?php define("UNMBER", "this is a hacker", true); //定义常量 echo UNMBER . "<br>";//true 忽略大小写,默认是false echo unmber; ?>
<?php //结合函数使用 define("UNMBER", "this is a hacker", true); //定义常量 echo UNMBER . "<br>"; echo unmber . "<br>"; function test() { echo UNMBER; } test(); ?>
<?php $string="1234"; echo strlen($string); //计算字符串长度 echo $string[0]; //以下标方式取值 ?>
反转:
<?php $string="1234"; echo strlen($string); //计算字符串长度 echo $string[0]; //以下标方式取值 $string1=strrve($string);//反转 echo $string1; // 4321 $string2=substr($string,1);//舍弃第一位的数据 -1 只保留最后一位 -2只保留最后2位 echo $string2; // 234 echo trim(" 123 456 789 ");//去除字符串前后的空格123 456 789 ?>
cmd-->php~\php -r "phpinfo();"
php~\php -f ".php文件,可以拖拽到CMD窗口"
$str1 = "123";
echo $str1 .= "456";
PHP 表单
HTML
style 选择器:
.error{}
p{}
#ID {}
echo htmlspecialchars(""); //实体化代码输出,忽略代码本身输出内容
echo stripcslashes("i\\m abc"); //删除转义符
trim 去除左右两端的空格
echo trim(" 123 ");
preg_match( /匹配的表达式/(要用正则表达式),被匹配的字符串),返回值是 int (0|1) bool
匹配上,返回 1 false 否则为 true 0
echo preg_match("/php/", "you know the php is bester language");
!preg_match 取反
Delimiter must not be alphanumeric or backslash in 分隔符不能是字母数字和 反斜线 。
span 组合行内元素 class
文件包含:引入某些文件,去使用该文件的内容。
方式有4种:
1、include "文件路径/文件名";
如果引入文件错误,会报警告Warning,后续代码依旧会执行。
2、include_once "文件";
用法同 include 基本一致,只需包含一次,后续可一直使用。
3、require "文件";
包含文件时,如果找不到,会报错ERROR,后续的代码均无法执行。
4、require_once "文件";
用法同require一致,只需包含一次,后续可一直使用。
支持目录跳转,可以使用相对路径 ../../..file 或者绝对路径
copy 1.png/b+1.txt/a 2.png
a 表示是ASCII格式
b 表示该文件是二进制文件
把文本文件加装到图片文件中。
文件包含,引入某些文件,去使用该文件中内容,在PHP中文件包含的函数有4个:
(1)include "文件" /include("文件")
包含文件时,如果找不到被包含的文件会警告,后续的代码依旧知道
(2)include_once "文件"
用法和include用法基本一致,包含文件只需要一次即可,后续可以一直使用
(3)require "文件"
包含文件时,如果找不到会报致命错误,其后续的代码均不执行
(4)require_once "文件"
用法和require用法一致,包含文件时只需要包含一次即可
被包含过来的文件中,如果按照PHP的标准语法来写代码,那么就会执行该代码;如果不是PHP语法写的文件,就会读取出来
copy 1.png/b+1.txt/a 2.png
a 表示该文件是ASCII文本格式
b 表示该文件是二进制文件
Session: The method or way to transmit data between the server and the user. A channel needs to be established before the channel can transmit data. Sessions are managed through cookies on the client, and sessions are managed on the server through sessions.
Comparison of cookies and sessions:
1. Cookies are stored in the user's browser and are set by the server through set-cookie in the return package. The cookie represents the user's browser and The session state between servers. The cookie data can be obtained only after successful login. This data is generally time-sensitive. If it expires, the user needs to log in again. With cookie data, each user request will bring a cookie, and the server will verify the legality and timeliness of the cookie. The sending of cookies needs to follow the browser's same-origin policy;
2. Session is stored on the server side and represents the session state of the user and server time. Session and cookie are in one-to-one correspondence. The server also needs to remember which user logged in to the site. Generally, session is saved in the /tmp directory in the form of a file. , the format is: sess_XXXXX (for example: sess_nti62h7rrrnb5udpvfbad13cg5s9kqrm). The cookie assigned by the server to the browser at this moment is: nti62h7rrrnb5udpvfbad13cg5s9kqrm. As long as the value of the cookie is modified, the server will require the user to log in again.
How to view cookies:
1. Enter the URL of the currently logged in page, javascript:alert(document.cookie)
2. In the console, enter: alert(document.cookie) or document.cookie
3. In the network management tool that comes with the browser.
setcookie("key","123"); should be written before the HTML code.
setcookie("name","456",time() 5);
echo $_COOKIE["name"]."
";
setcookie("name","value",time()-3600);//Delete cookie
print_r($_COOKIE);
$_SEESION["id"]=1;
$_SEESION["demo"]=true;
unset($_SEESION["id"]);//Delete a certain value in seesion
session_destroy() ;//Clear all SEESION values
If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.
The above is the detailed content of The most comprehensive collection of PHP introductory notes in history (summary sharing). For more information, please follow other related articles on the PHP Chinese website!