Home  >  Article  >  Backend Development  >  PHP we learned together in those years (3)_PHP Tutorial

PHP we learned together in those years (3)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:42805browse

一:PHP数据的输出

PHP支持的丰富的数据类型。后来在学习中会反复使用,有其他语言的学习知识,比较记忆更容易了解PHP的独到之处。下面先阐述一下PHP的输出。PHP向浏览器的基本输出方式有Echo(),Print(),printf(),sprintf()我们可以对比一下一上四种输出方式。

Function Echo() print() printf() sprintf()
Return Void int:  ever return 1 int: string
Express Echo(string argument) print(argument) printf(string format) printf(string format)
Instruction 写入字符串即可 写入字符串,返回值是1,用来验证是否成功显示 支持输出格式字符串,格式参看下面讲述 同样格式字符串,但是不直接呈现浏览器

其实Echo()与print() 的区别是不太大的,使用哪一个完全取决于自己的喜好,后两者一样。什么叫格式化字符串输出呢?有C/C++语言时我们会明白这个意思,也就是输出时我们可以应该适当的格式化符号,让输出格式化。

二:PHP中使用到的格式化输出格式

这些格式化符号下表显示:
类型 描述 举例
%b 将参数认为是一个整数,显示其二进制数 printf(%d,10);=======>显示1010
%c 将参数认为是一个整数,显示其ASCII字符 printf(%c,65);======>显示A
%d 将参数认为是一个整数,显示其十进制 printf(%d,10);=======>10
%f 将参数认为是一个浮点数,显示其浮点数 printf(%f,2);========>2.00000
%o 将参数认为是一个整数,显示其八进制 Printf(%o,8)============10
%s 将参数认为是一个字符串,显示其字符串 printf(%s,”this  is a TV ”)=====>this is a TV
%u 将参数认为是一个整数,显示一个无符号十进制 printf(%u,-100)======>100
%x 将参数认为是一个整数,小写的十六进制
%X 将参数认为是一个整数,大写的
3: Points to note when declaring variables in PHP
The declaration of variables in PHP is similar to the shell script language. Variables all start with the $ symbol. We should pay attention to the following points:
1 ): $ always precedes a variable, which is a valid identifier.
2): Variables are strictly case-sensitive, for example, $Book and $book are different.
3): PHP variables do not need to be explicitly declared, which is just the opposite of C language.
4): After declaration, you can assign a value to the variable. Assignment is divided into assignment by value and assignment by reference. Reference assignment is assignment by stack address

Four: Scope of variables in PHP
According to the scope, variables are divided into local variables, global variables, static variables, PHP also has unique super global variables. Local variables can only be used in the declared scope, and global variables can be used throughout the life cycle. Static variables are declared using the Static modifier, and static variables still exist in memory after the function exits. For example,
Copy code The code is as follows:

funtion keep()
{
static $count=0;
$count++;
echo $count;
echo "
";
}
10:
11: keep() ;//Output 1
12: keep();//Output 2
13: keep();//Output 3
14: keep();//Output 4
15:
16: //You may think that the output values ​​are all 1, but it happens to be 1234. This is the effect of using static
17: ?>

5 :Super global variables ($_SERVER, $_GET, $_POST, $_COOKIE), $_FILES, $_ENV, $_SESSION
Let’s look at super global variables. PHP provides many useful predefined variables, which can be accessed anywhere in the execution of the script. They are used to provide a large amount of environment-related information, and can also obtain the current user session (session), operating environment, local environment, etc. For example, you can use
to copy the code . The code is as follows:

foreach($_SERVER as $var => $value)
{
//Traverse and output all system super variables
echo "$var => $value
";
}

You can see the output There are many system variables
HTTP_HOST
=>
Localhost
and other system information
. We can obtain these global variables through
$_SERVER["HTTP_HOST"]
. The $_SERVER global variable contains WEB server, client configuration, current information, etc. and can be used by searching documents.
In addition, the passed variables can be obtained through the GET method. The $_GET super global variable contains information about the parameters passed by the GET method. For example, if the requested URL address is http://www.baidu.com/index.html?cat=apache&id=145, you can use super global variables to access the following variables: $_GET['cat']=”apache”; $_GET ['id']=”145”, by default, you need to access the variables passed through the GET method. The $_GET super global variable is the only access method. You cannot use $cat, $id to reference GET variables. You will learn more later. The chapter on secure access to external data explains this in detail.
In addition, variables can also be passed using the POST method.
The details are as follows: The $_POST super global variable contains information about passing parameters using the POST method.
Consider the following request form:
Copy the code The code is as follows:


Email-adress:

Password:







You can use the following POST variables through the target script a.php:
$_POST['email']=”zyl0395@126.com”;
$_POST['pswd' ]=”Bestyear”;
We can also use super global variables to save COOKIE information. $_COOKIE saves all the information passed to the script in the HTTP cookie. These cookies are generally set by the PHP function setcookie() by previously executed PHP scripts. of. For example:
Copy code The code is as follows:

$value = '; <🎜 somewhere' >setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* Cookie valid for one hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);
?>


It doesn’t matter if you don’t understand here. I will study the knowledge of cookies later.
$_FILES is a variable used to upload files to the server through POST. $_files is mainly used where binary files need to be uploaded. If an abc.mp3 file is uploaded, the server needs to obtain relevant information about the file. Variable $_files to obtain. There are five elements in total:
1):$_FILES['userfile']['name']
The original name of the client machine file.
2):$_FILES['userfile']['type']
The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif".
3):$_FILES['userfile']['size']
The size of the uploaded file, in bytes.
4):$_FILES['userfile']['tmp_name']
The temporary file name stored on the server after the file is uploaded.
5):$_FILES['userfile']['error']
Error code related to the file upload. ['error'] was added in PHP 4.2.0.
$_EVN is the relevant information used by the PhP server, $_SESSION obtains the relevant information of the session

6: PHP constant definition usage
Constants are quantities that cannot be changed in the program. Very useful such as: Pi
Definition: define ("PI", 3.1415926)
Use echo PI;
Seven: About logical symbols, operation levels, expressions, flow control, logic, etc. in PHP Introducing again, it is basically consistent with the C++ language. Here I will simply write down the missing parts. For example, the role of Include in PHP.
Include is also a sentence that introduces files in PHP. The basic syntax is include (/path/to/file). To reference /user/local/lib/php/wjgilmore/init.inc.php, it is like this:
Copy code The code is as follows:

include "/user/local/lib/php/wjgilmore/ init.inc.php ";
?>

One thing to note is that
include in the judgment sentence must be delimited by braces {} , otherwise it will be wrong, please pay attention to this. You can also reference a remote file through include. If the server where the file is located supports PHP, the included variables will also be parsed by passing the necessary key-value pairs (similar to the GET request) )
For example: include “http://www.123.com/index.html?background=red”;
If it is only quoted once, use
include_once
, and it will first check whether it is quoted. This file is referenced if it does not exist. If it exists,
include_once() is not executed.
Ensure once.
The same method require is to request a file, and the same method is require_once to request once. It will take time to explain in detail later.

http://www.bkjia.com/PHPjc/325233.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325233.htmlTechArticle1: PHP data output PHP supports rich data types. Later, I will use it repeatedly in my studies. I have learning knowledge of other languages. It is easier to understand the uniqueness of PHP by comparing my memory. Below...
Statement:
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