Home Backend Development PHP Tutorial PHP learning officially sets sail (5)

PHP learning officially sets sail (5)

Dec 28, 2016 am 09:19 AM
php

Now let’s start the road of mixing php and html
php files can embed html code, but html files cannot embed php code, because html is a static file

Let’s talk about PHP forms and user input

PHP's $_GET and $_POST are used to obtain the value submitted by the form

Create a new php file index.php

<html>
<body>
<form action="index.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Copy after login

Now it is still a pure html code form Submit to yourself, the submission method is post

Now add the php code

<html>
<body> <form action="index.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
<?php
echo "Name:".$_POST[&#39;name&#39;];
echo "Age:".$_POST[&#39;age&#39;];
?>
Copy after login


php can be added anywhere in the file, no need to be in the html tag
Finally What is printed is the result of your text box input

If the action is empty, it will be submitted to the file itself by default

$_POST['name']; You can use double quotes or single quotes, no Quotation marks can also be used (but it will remind you)
Post submission method cannot be obtained with $_GET

Use $_GET below to obtain the value of the form

<html>
<body> <form action="" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
Copy after login
<?php
echo "Name:".$_GET["name"];
echo "Age:".$_GET["age"];
?>
Copy after login

Why use $_GET?
When using $_GET variables, all variable names and values ​​will be displayed in the URL. So this method should not be used when sending passwords or other sensitive information. However, because the variables appear in the URL, you can bookmark the page. In some cases this is useful.
HTTP GET method is not suitable for large variable values; the value cannot exceed 100 characters.
Generally used for paging, detailed information display, etc.
POST is generally used for submitting data

There is also a $_REQUEST request, which represents the client's request

PHP's $ The _REQUEST variable contains the contents of $_GET, $_POST and $_COOKIE.
PHP’s $_REQUEST variable can be used to obtain the results of form data sent through the GET and POST methods.

<html>
<body> <form action="" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
Copy after login
<?php
echo "Name:".$_REQUEST["name"];
echo "Age:".$_REQUEST["age"];
?>
Copy after login

This can be obtained by either get or post submission method, but use it as little as possible. Because sometimes you have to confirm how the other party submitted it, it is better to distinguish it clearly

Let’s talk about PHP Session variables
When running an application, you will open it, make some changes, and then Close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state. PHP sessions solve this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database. The working mechanism of Session is to create a unique ID (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or passed through the URL.

Before using session, you must first start the session, which is different from other languages

<?php session_start(); ?>
Copy after login
<html>
<body>
</body>
</html>
Copy after login

session_start() function must be located before the tag

index.php page

<?php session_start(); ?> 

 
Name:
test.php
Copy after login

test.php page

<?php
session_start();
echo $_SESSION[&#39;name&#39;];
?>
Copy after login

The seesion value stored in the index page can be displayed on the test page
Whether it is stored or For output, each page needs session_start();
$_SESSION['name']; It can also be single or double quotation marks or not. It seems that this is the case in php. I won’t repeat it in the future.


If you want to delete some session data, you can use the unset() or session_destroy() function.

<?php session_start(); ?>


Name:
test.php
Copy after login

Then when you go to test.php, you cannot output it

<?php
session_start();
if(isset( $_SESSION[&#39;name&#39;]))
echo $_SESSION[&#39;name&#39;];
else
echo "null";
?>
Copy after login

isset function checks whether a value is set (assigned), which is to determine whether a value is Empty

Say below
PHP Cookies

What are cookies? Cookies are often used to identify users. Cookies are small files that a server leaves on a user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.

How to create a cookie? The setcookie() function is used to set cookies.
setcookie() function must be located before the tag.

<?php 
setcookie("user", "Hello world", time()+3600);
?>
Copy after login

We will create a cookie named "user" and assign it the value "Hello world". Specifies that this cookie will expire after one hour:

Where are cookies generally stored on the computer?
For IE browser, save it in
C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files

Temporary Internet Files folder
You will find that you txt file named php project, open it
and you can see the content. However, some of the content is encrypted, but the first half of
user
Hello+world
localhost/MyPHP/ can still be viewed. The

gets the cookie value

<html> 
<body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; 
else echo "Welcome New!<br />"; 
?> 
</body> 
</html>
Copy after login

$_COOKIE is the one that gets the cookie value


Looking at other languages, getting get, post, and cookie are all Using objects, PHP is obviously much simpler, although it is process-oriented



Let’s talk about PHP’s processing of files

First create it in the project root directory A file 1.txt content hello world

Open the file The fopen() function is used to open the file in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file

<?php
$file=fopen("1.txt","r");
?>
Copy after login

$file这个变量是个资源变量,表示文件打开的状态
关于资源变量以后还会接触

文件可能通过下列模式来打开: 模式描述
r 只读。在文件的开头开始。
r+ 读/写。在文件的开头开始。
w 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。
w+ 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。
a 追加。打开并向文件文件的末端进行写操作,如果文件不存在,则创建新文件。
a+ 读/追加。通过向文件末端写内容,来保持文件内容。
x 只写。创建新文件。如果文件已存在,则返回 FALSE。
x+ 读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。

如果 fopen() 无法打开指定文件,则返回 0 (false)。


打开文件还不够,接着打印

<?php
$file=fopen("1.txt","r");
$data="";
while(!feof($file)) 
{ 
$data.=fgets($file); 
} fclose($file); echo $data; 
?>
Copy after login

feof() 函数检测是否已达到文件的末端 (EOF)。在循环遍历未知长度的数据时,feof() 函数很有用。
fgets() 函数用于从文件中逐行读取文件。
在调用该函数之后,文件指针会移动到下一行。
fclose 关闭文件

另外fread函数也可以读取文件

<?php
$file=fopen("1.txt","r");
$data="";
while(!feof($file)) 
{ 
$data.=fread($file,4096); 
} fclose($file); echo $data; 
?>
Copy after login


fread() 从文件指针 handle 读取最多 length 个字节。该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况。 

fread与fgets的区别 
fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止. 
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用. 


其实还有文件写入,文件上传下载这些
暂时先简要介绍在这里,以后我接触的时候再说

以上就是php学习正式起航(5)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles