PHP study notes (go forward)

foreword

Welcome to the first article of PHP learning (going forward): go straight ahead, nothing can stop you. Go forward bravely and fearlessly, the following PHP articles will continue to update relevant study notes, and look forward to learning and communicating with you!

easy to understand

insert image description here

PHP (PHP: Hypertext Preprocessor) is the "Hypertext Preprocessor", which is inService-TerminalExecution scripting language, especially suitable for web development and can be embedded in HTML. PHP grammar learned C language, absorbed the characteristics of multiple languages ​​​​of Java and Perl to develop its own characteristic grammar, and continued to improve and improve itself according to their strengths, such as java's object-oriented programming. The main goal of this language was to make Developers write high-quality web sites quickly. PHP supports both object-oriented and process-oriented development, and is very flexible in use.

Environment build

We can find xampp (site building integration software package) on the official website to download:
insert image description hereor click to open the uploaded resources

Then follow the steps to open:
insert image description here
Then click next to install:
insert image description here
Here you can choose to check according to your personal needs:
insert image description here
When we choose the installation path, it is best not to choose an overly deep path, and Chinese names are not allowed:
insert image description here
choose well next after the path:

insert image description here

If something similar to the following picture appears, close it:

insert image description here
Next start the installation:

insert image description here
Wait for a moment:
insert image description here
a pop-up window appears, just allow access:
insert image description here
click finish:

insert image description here
Set language, stand-alone save:

Click start to start the service: the first one is the Apache server, and the second one is the database:
insert image description here
when a green highlight appears, it means that the startup is normal:

insert image description here
[Solutions to problems encountered during startup]
1. Apache cannot be started: click the config button after apach, open the configuration file with ssl, change 443 to 444, and generally proceed to this step to start.
insert image description here
insert image description here
insert image description here
[If you still can't start here, please try the following operations]

2. Click config, select the httpd configuration file without ssl, find Listen 80, change it to Listen 8080, and restart the service 3. Test
insert image description here
whether the server is installed successfully: enter in the address bar: http://localhost:8080 Press Enter, (if the port number has not been changed, then enter: http://localhost:80 in the browser address bar) the following interface appears to prove that the installation is successful:
insert image description here

It should be noted before writing the program that because the server used is the Apache server, when using hbuilder to write, all the PHP programs we write need to be in the htdocs directory to run normally.
insert image description here

Programming

1. PHP writing method and precautions

<?php ?> 是PHP的标记对,之间用来存放PHP代码;

Then there is the naming convention of variables in PHP:

以$开头,后面跟标识符; 变量名需要区分大小写; 变量名遵循驼峰标记法或者是下划线标记法,比如:first_name; 大驼峰:FirstName;小驼峰firstName; 变量名的命名要简单明了,方便识记

2.echo and output statement

echo in PHP is a grammar or function that outputs a string. When it is directly followed by a space and a string, it is a grammar, which plays an output role:

<?php
echo 'hello world';
//这时我们打开浏览器,解析运行出结果为 hello world
?>

Of course, you can also output a string by assigning a string to a variable and outputting the value of the variable ($str):

<?php
$str='hello world';
//输出hello world
echo $str;

It is also possible to output tags containing html;

<?php
echo 'hello <br> world';
?>

insert image description here

Output multiple parameters:

<?php
echo 'This ','string ','was ','made ','with multiple parameters.';
//需要注意的是在PHP中单引号和双引号是有区别的:单引号将输出变量名称,而不是值
?> 

insert image description here

Regarding quotes, for example:

<?php
	$a='world';
	echo "hello $a";//输出hello world
	echo "<br>";//换行
	echo 'hello $a';//输出hello $a
?>

insert image description here

Double quotes can parse the variables in it, but single quotes cannot, and can only be output as they are;
if you want to output characters directly when double quotes are output, you need to use escape characters;
try to use single quotes in China in practical applications, because single quotes Quotation marks do not need to be parsed and are faster.

You can also change the string and output:

<?php
	$a='hello';
	echo $a{
    
    1};
	echo "<br>";
	$a{
    
    4}='w';
	echo $a;
	echo "<br>";
	$a{
    
    5}='!';
	echo $a;
	echo "<br>";
?>

Output result:

insert image description here

Output concatenated characters:

<?php
	$a='hello';
	$b='world';
	echo $a." " .$b;
?>
//在PHP中连接符是.

insert image description here

If you want to output a string containing single quotes, you can use escape characters at this time:

<?php
echo 'hello \'world \'';
//注意这里全部都是单引号
//输出hello'world'
?>

insert image description here

Also there are some escape characters in php

The escape characters in PHP are:

" \n" Newline

"\r" carriage return

"\t" horizontal tab character

"\" backslash

"$" dollar sign

"English slash' "single quotes

"English slash" " double quotes

The output syntax in PHP is also:

echo Can output one or more strings
print Can only output the value of simple type variables, such as int, string
print_r The value of complex type variables can be output and displayed in an easier-to-understand form. such as arrays, objects
printf The function is used to format the output string, mainly used to replace the format string starting with % in the string
sprintf Functions are also used for string formatting. This function is basically the same as the printf function, but it can save the converted result to a string variable instead of outputting it directly
var_dump Print the relevant information of the variable, including the type and value of the expression, and display its structure through indentation

Realize it:

<?php
print "hello world";
//输出hello world
?>
<?php
$a=array('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
print_r ($a);

Output result:

insert image description here

$number = 1;
$str = "hello world!";
printf("今天是星期%u ,看见纸上写着 %s.",$number,$str);

Output result:
insert image description here


<?php
$number = 1;
$str = "hello world!";
$txt = sprintf("今天是星期%u,看见纸上写着%s", $number, $str);
echo $txt;
?>

Output result:
insert image description here


<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>

Output result:

insert image description here

Tips: echo output is faster than print, echo is a PHP statement, no return value, print and print_r are PHP functions, the function has a return value.

The return value of print is 1 (int type), and the return value of print_r is true (bool type).

3. Data type

insert image description here
We can use var_dump to represent the data type:

	$a=5;
	var_dump($a);
	echo "<br>";
	$b="hello world";
	var_dump($b);
	echo "<br>";
	$c=3.14;
	var_dump($c);
	echo "<br>";
	$d=true;
	var_dump($d);

Output result:
insert image description here
or use the function of checking data type of PHP to judge the data type:
insert image description here
you can realize it:

$a=5;
$b="hello world!";
$c=3.14;
$d=314;
if(is_int($a)){
    
    
	echo "$a"."是int型数据"."<br>";
}else{
    
    
	echo "$a"."的数据类型是:".is_int($a);
}
if(is_string($b)){
    
    
	echo "$b"."是string型数据"."<br>";
}else{
    
    
	echo "$b"."的数据类型是:" .is_string($b);
}
if(is_float($c)){
    
    
	echo "$c"."是float型数据" ."<br>";
}else{
    
    
	echo "$c"."的数据类型是".is_float($c);
}
if(is_float($d)){
    
    
	echo "$d"."是float型数据" ."<br>";
}else{
    
    
	echo "$d"."的数据类型是:";
	var_dump($d);
}

Take a look at the results:
insert image description here

4. Pass by value and pass by reference

In general, php uses value transfer by default.

pass by value

It refers to copying the data value (data content) of a variable and then assigning it to another variable, that is, ordinary assignment between variables.

		$a=1;
		$b=$a;
		$b++;
		//b的值是2,a的值是1
		echo "a的值是$a"."<br>"."b的值是$b";

pass by reference

Passing by reference in PHP is to add & in front of the variable. Changing the new variable in passing by reference will affect the original variable.

		$a=1;
		$b=2;
		echo "传递前a的值是:".$a."<br>";
		echo "传递前b的值是:".$b."<br>";
		$b=&$a;
		echo "传递后a的值是:" . $a."<br>";
		echo "传递后b的值是:" . $b."<br>"; 

insert image description here

		$a=1;
		$b=&$a;
		$b=2;
		echo "a的值是$a"."<br>"."b的值是$b";

insert image description here

form pass value

There are two ways to pass values ​​in forms in PHP, post and get; let me give a few examples to see the difference between the two~

1.post

The first page is used to display to the user and collect user information:

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
		<form action="postdo.php" method="post">
		用户名:<input type="text" name="id">
		密码:<input type="password" name="pas1">
		邮箱:<input type="email" name="emai1">
		<input type="submit" value="提交">
		</form>
</body>
</html>

insert image description here
After the collection is complete, it will be displayed to the background:

<?php
//用于接收从post页面传递过来的用户信息,并显示出来
$name=$_POST['id'];
$pass=$_POST['pas1'];
$email=$_POST['emai1'];
echo $name;
echo "<br>";
echo $pass;
echo "<br>";
echo $email;
?>

insert image description here

2.get

			<form action="getdo1.php" method="get">
			用户名:<input type="text" name="id">
			密码:<input type="password" name="pas1">
			邮箱:<input type="email" name="emai1">
			<input type="submit" value="提交">
			</form>

insert image description here

<?php
	$name=$_get['id'];
	$pass=$_get['pass'];
	$emai=$_get['email'];
	echo $name . "<br>";
	echo $pass ."<br>";
	echo $email ."<br>";
?>

insert image description hereIn addition, use get for hyperlink transmission:

<a href="getdo.php? id=1">第一篇文章</a>
	<a href="getdo.php? id=2">第二篇文章</a>
	<a href="getdo.php? id=3">第三篇文章</a>
<?php
$id=$_GET['id'];
echo "您单击了第".$id."篇文章";
?>

insert image description here

brief summary

In terms of security, the data submitted by get can be seen in the url column, while the data submitted by post is invisible, so post is more secure.

From the point of view of the submission principle, the get submission is the submission of parameters one by one, and the post submission is the submission of all parameters as a whole.

In terms of the size of the submitted data, the get submission generally does not exceed 255 bytes, and the size of the post submission depends on the server.

In terms of flexibility, get is very flexible, as long as there is a page jump, parameters can be passed, post is not flexible, and post submission requires the participation of a form.

problem solving

When passing values ​​in the form, I found a problem related to single selection and multiple selection:
insert image description hereAfter searching the information, I found that the reason is:
because the names of the single selection boxes are all the same, and we need to obtain the value of the corresponding option, then we need to manually enter the HTML Add value to the page, and use the difference in value to judge the selected option:

					//将单选框的value值继续修改
					<input type="radio" name="sex" value="女"><input type="radio" name="sex" value="男">

Operation:
insert image description hereSimilarly, for the check box, it can select multiple options at the same time, while the receiving page uses $_POST to receive only one variable (a variable has only one value), so the option selected later will override the first selected option option, this problem can be solved using an array.

//信息收集页面
				   <input type="checkbox" name="hobby[]" value="书法">书法
				   <input type="checkbox" name="hobby[]" value="绘画">绘画
				   <input type="checkbox" name="hobby[]" value="演讲">演讲
//显示页面
                   var_dump($hobby)."<br>";

insert image description hereAnother solution is to use the isset() function to output the selection of the checkbox:

<form action="checkdo1.php" method="post">
	        <h3>复选框的演示</h3>
	        <input type="checkbox" name="check_a" value="a">摄影<br>//设置不同value值,当被勾选时相应值就会被进行post传递,用于后台收集
	        <input type="checkbox" name="check_b" value="b">书法<br>
			<input type="checkbox" name="check_c" value="c">绘画<br>
	        <br>
	        <input type="submit" value="提交">
	    </form>
<?php
 if(isset($_POST))//使用isset函数判断变量被定义的方式
    {
    
    
        if(isset($_POST['check_a']))
        {
    
    
            $check_a = $_POST['check_a'];
        }
        if(isset($_POST['check_b']))
        {
    
    
            $check_b = $_POST['check_b'];
        }
        if(isset($_POST['check_c']))
        {
    
    
            $check_c = $_POST['check_c'];
        }
        echo "<br>";
        if(isset($check_a) && $check_a == 'a')  
        {
    
    
            echo "摄影<br>";
        }
        if(isset($check_b) && $check_b == 'b')
        {
    
    
            echo "书法。<br>";
        }
        if(isset($check_c) && $check_c == 'c')
        {
    
    
            echo "绘画。<br>";
        }
    }
?>

insert image description here

Summarize

Life is like a marathon, what can make you go longer and farther is your resilience. wish youindomitable, invincible!

insert image description here
If there are any mistakes, please give me some pointers from seniors, thank you very much!

Guess you like

Origin blog.csdn.net/weixin_64122448/article/details/126752444