Table of Contents
Welcome to my home page

PHP随笔

Jun 23, 2016 pm 02:36 PM

http://www.w3school.com.cn/php/php_operators.asp  

网址PHP第一章(自学)
PHP 文件的文件后缀是 ".php"、”.php3" 或 ".phtml”.
PHP 脚本以   结束    PHP 中的每个代码行都必须以分号(;)结束。输出文本的基本指令有两种:echo  和 print ,在PHP中,注释用 // 和 /* */ 来编写注释,PHP中的变量都是以 $ 符号开始的例如:$var_name="Hello World!";
PHP 是一门弱类型语言,在声明变量的时候可以不设置它的类型,
PHP中的运算符: (1)并置运算符  (把两个变量连接在一起,需要用到   .  运算符)  
 例如: echo  $变量1 .”  ”. $变量2 ;   由于之间插入了空格,所以有两个并运算符。
PHP中的函数:strlen   用于计算字符串的长度     例如:echo    strlen(“123456!”);
Strops   用于在字符串内检索一段字符串或一个字符。如果找到匹配的位置返回第一个匹配的位置,如果没有找到返回false.
PHP中的运算符:算术运算符,赋值运算符,比较运算符,逻辑运算符    其中赋值运算符中:+=,-+,*=, /=,的意思例如:x+=y     相当于是:x=x+y,同理别的都是这个意思。
.=的意思是:x.=y   相当于  x=x.y (连接字符串)
PHP 中 Post  和Get的区别!


Name:
Age:


在welcome.php中这样调用

Welcome .

You are years old.

输出  :welcome  join  .
           You are 28 years old.

为什么使用 $_GET?
注释:在使用 $_GET 变量时,所有的变量名和值都会显示在 URL 中。所以在发送密码或其他敏感信息时,不应该使用这个方法。不过,正因为变量显示在 URL 中,因此可以在收藏夹中收藏该页面。在某些情况下,这是很有用的。
注释:HTTP GET 方法不适合大型的变量值;值是不能超过 100 个字符的。
$_REQUEST 变量
PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。
PHP 的 $_REQUEST 变量可用来取得通过 GET 和 POST 方法发送的表单数据的结果。
例子
Welcome .

You are years old!

$_POST 变量用于收集来自 method="post" 的表单中的值。
$_POST 变量
$_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值。
$_POST 变量用于收集来自 method="post" 的表单中的值。从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏览器的地址栏),并且对发送信息的量也没有限制。
例子


Enter your name:
Enter your age:


当用户点击提交按钮,URL 不会含有任何表单数据,看上去类似这样:
http://www.w3school.com.cn/welcome.php
"welcome.php" 文件现在可以通过 $_POST 变量来获取表单数据了(请注意,表单域的名称会自动成为 $_POST 数组中的 ID 键):
Welcome .

You are years old!
为什么使用 $_POST?
? 通过 HTTP POST 发送的变量不会显示在 URL 中。
? 变量没有长度限制。
不过,由于变量不显示在 URL 中,所有无法把页面加入书签。
$_REQUEST 变量
PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。
PHP 的 $_REQUEST 变量可用来取得通过 GET 和 POST 方法发送的表单数据的结果。
例子
Welcome .

You are years old!

表单验证
应该在任何可能的时候对用户输入进行验证。客户端的验证速度更快,并且可以减轻服务器的负载。
不过,任何流量很高以至于不得不担心服务器资源的站点,也有必要担心站点的安全性。如果表单访问的是数据库,就非常有必要采用服务器端的验证。
在服务器验证表单的一种好的方式是,把表单传给它自己,而不是跳转到不同的页面。这样用户就可以在同一张表单页面得到错误信息。用户也就更容易发现错误了。
PHP 的 date() 函数用于格式化时间或日期。
语法
date(format,timestamp)
参数 描述
format 必需。规定时间戳的格式。
timestamp 可选。规定时间戳。默认是当前的日期和时间。
PHP 日期 - 格式化日期
date() 函数的第一个参数规定了如何格式化日期/时间。它使用字母来表示日期和时间的格式。这里列出了一些可用的字母:
? d - 月中的天 (01-31)
? m - 当前月,以数字计 (01-12)
? Y - 当前的年(四位数)
您可以在我们的 PHP Date 参考手册中,找到格式参数中可以使用的所有字母。
可以在字母之间插入其他字符,比如 "/"、"." 或者 "-",这样就可以增加附加格式了:
echo date("Y/m/d");
echo "
";
echo date("Y.m.d");
echo "
";
echo date("Y-m-d");
?>
以上代码的输出类似这样:
2006/07/11
2006.07.11
2006-07-11

PHP的引用文件
通过 include() 或 require() 函数,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容。除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的。include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。
例子1.        include()函数



Welcome to my home page


Some text


一般我们用require() 函数,因为这样比较安全吧。
PHP中fopen()函数用于打开文件
此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件:


$file=fopen("welcome.txt","r");
?>

文件可能通过下列模式来打开:
模式 描述
r 只读。在文件的开头开始。
r+ 读/写。在文件的开头开始。
w 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。
w+ 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。
a 追加。打开并向文件文件的末端进行写操作,如果文件不存在,则创建新文件。
a+ 读/追加。通过向文件末端写内容,来保持文件内容。
x 只写。创建新文件。如果文件以存在,则返回 FALSE。
x+ 读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。
注释:如果 fopen() 无法打开指定文件,则返回 0 (false)。
fclose() 用于关闭一个文件。

$file = fopen("test.txt","r");
fclose($file); ?>

检测 End-of-file
feof() 函数检测是否已达到文件的末端 (EOF)。
在循环遍历未知长度的数据时,feof() 函数很有用。
注释:在 w 、a 以及 x 模式,您无法读取打开的文件!
if (feof($file)) echo "End of file";

逐行读取文件
fgets() 函数用于从文件中逐行读取文件。
注释:在调用该函数之后,文件指针会移动到下一行。
例子
下面的例子逐行读取文件,直到文件末端为止:
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))//判断文件的长度
  {
  echo fgets($file). "
";//逐行读取fgets()
  }
fclose($file);//关闭
?>

 

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles