那些年一起学习的PHP(三)_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 | 将参数认为是一个整数,大写的 | 略 |
在php中变量的声明类似于shell脚本语言,变量都是以$符号开始的,我们要注意以下几点:
1):$总是在变量前面,变量是有效的标示符。
2):变量是严格区分大小写,例如$Book与$book是不一样的。
3):PHP变量不需要显示声明,刚好与C语言相反。
4):声明之后便可以为变量赋值,赋值分为按值赋值,引用赋值引用赋值是按堆栈地址赋值
四:PHP中变量的的作用域
按照作用域区分变量有局部变量,全局变量,静态变量,PHP还有独特的超级全局变量。局部变量只能在声明的作用域中使用,全局作用变量在整个生命周期中都可以使用。静态变量是声明使用Static 修饰符,在函数退出后static 变量仍然在内存中存在。例如
funtion keep()
{
static $count=0;
$count++;
echo $count;
echo "
";
}
10:
11: keep();//输出1
12: keep();//输出2
13: keep();//输出3
14: keep();//输出4
15:
16: //你可能以为输出的值全部是1,但是刚好确实是1234,这里正是static的使用效果
17: ?>
五:超级全局变量($_SERVER,$_GET,$_POST,$_COOKIE),$_FILES,$_ENV ,$_SESSION
再来看超级全局全局变量。PHP提供很多有用的预定义变量,可以再执行脚本的任意位置访问,用于提供大量与环境相关的信息,还可以获得当前用户会话(session),操作环境,本地环境等等。例如可以使用
foreach($_SERVER as $var => $value)
{
//遍历输出所有系统超级变量
echo "$var => $value
";
}
可以看到输出好多系统变量
HTTP_HOST
=>
Localhost
等等系统信息
我们就可以通过
$_SERVER[“HTTP_HOST”]
来获得这些全局变量。$_SERVER全局变量包含WEB服务器,客户配置,当前信息等可以通过查找文档使用。
另外还可以通过GET方法获取传递的变量。$_GET超级全局变量包含试用GET方法专递的参数的有关信息。例如请求的URL地址是http://www.baidu.com/index.html?cat=apache&id=145 ,就可以使用超级全局变量访问如下变量:$_GET[‘cat']=”apache”; $_GET[‘id']=”145” ,默认的情况下要访问通过GET方法传递的变量,$_GET 超级全局变量是唯一的访问途径,不能用$cat, $id的方式引用GET的变量,后续了解关于安全访问外部数据的章节会详细说明。
另外使用POST的方法也可以传递变量。
具体如下:$_POST超级全局变量包含用POST方法传递参数有关信息。
考虑如下的请求表单:
通过目标脚本a.php就可以使用下面这些POST变量:
$_POST[‘email']=”zyl0395@126.com”;
$_POST[‘pswd']=”Bestyear”;
我们还可以使用超级全局变量保存COOKIE信息,$_COOKIE保存了HTTPcookie中的所有传到脚本的信息,这些cookie一般由以前执行的PHP脚本通过PHP函数setcookie()设置的。例如:
$value = ' somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* 一小时有效cookie */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);
?>
这里看不懂无所谓的,后来还会专门研究cookie的知识。
$_FILES是通过POST向服务器上传文件使用的变量,$_files主要用在当需要上传二进制文件的地方,录入上传一个abc.mp3文件,则服务器端需要获得该文件的相关信息,则通过变量$_files来取得。 共有五个元素:
1):$_FILES['userfile']['name']
客户端机器文件的原名称。
2):$_FILES['userfile']['type']
文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。
3):$_FILES['userfile']['size']
已上传文件的大小,单位为字节。
4):$_FILES['userfile']['tmp_name']
文件被上传后在服务端储存的临时文件名。
5):$_FILES['userfile']['error']
和该文件上传相关的错误代码。['error'] 是在 PHP 4.2.0 版本中增加的。
$_EVN 是PhP服务器使用的有关信息, $_SESSION 获得会话的有关信息
六:PHP 常量定义使用
常量是程序中无法更改的量,非常有用如:圆周率
定义:define(”PI” ,3.1415926)
使用 echo PI;
七:关于PHP中的逻辑符号,运算等级,表达式,流程控制,逻辑等等不再介绍,基本和C++语言相符,这里只是简单写一下没有的部分。例如 Include 在PHP中的作用。
include在PHP中也是引入包含文件的句子,基本语法是include(/path/to/file)要引用/user/local/lib/php/wjgilmore/init.inc.php 则要这样:
include "/user/local/lib/php/wjgilmore/init.inc.php ";
?>
有个地方需要注意,就是
include在判断句中是,必须要用大括号{}界定
,否则会错误,这个要注意.,还可以通过include引用一个远程文件.如果文件所在的服务器支持PHP,通过传递必要的键值对(类似于GET请求的做法,所包含的变量也会得到解析)
例如:include “http://www.123.com/index.html?background=red”;
如果只引用一次,则用
include_once
,首先会检验是否引用了这个文件,如果没有则引用,如果有则不执行
include_once(),
确保一次。
同样的方法require是请求文件,同样是require_once请求一次。后续用到时间详细解释。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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 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 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.

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 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 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.
