php学习笔记(1),php学习笔记
php学习笔记(1),php学习笔记
1、引用文件的方法有两种:require 及 include
require 的使用方法如 require("MyRequireFile.php");
。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。
include 使用方法如 include("MyIncludeFile.php");
。这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化。
2、注释
<?<span>php </span><span>echo</span> "这是第一种例子。\n"; <span>//</span><span> 本例是 C++ 语法的注释</span> <span>/*</span><span> 本例采用多行的 注释方式 </span><span>*/</span> <span>echo</span> "这是第两种例子。\n"<span>; </span><span>echo</span> "这是第三种例子。\n"; <span>#</span><span> 本例使用 UNIX Shell 语法注释</span> ?>
注释:解释的信息是what和why。
3、常量类型
PHP 在常量中定义了以下一些常量。
__FILE__
这个默认常量是 PHP 程序文件名。若引用文件 (include 或 require)则在引用文件内的该常量为引用文件名,而不是引用它的文件名。
__LINE__
这个默认常量是 PHP 程序行数。若引用文件 (include 或 require)则在引用文件内的该常量为引用文件的行,而不是引用它的文件行。
PHP_VERSION
这个内建常量是 PHP 程序的版本,如 '3.0.8-dev'。
PHP_OS
这个内建常量指执行 PHP 解析器的操作系统名称,如 'Linux'。
TRUE
这个常量就是真值 (true)。
FALSE
这个常量就是伪值 (false)。
E_ERROR
这个常量指到最近的错误处。
E_WARNING
这个常量指到最近的警告处。
E_PARSE
本常式为解析语法有潜在问题处。
E_NOTICE
这个常式为发生不寻常但不一定是错误处。例如存取一个不存在的变量。
这些 E_ 开头形式的常量,可以参考 error_reporting() 函数,有更多的相关说明。
当然在程序写作时,以上的默认常量是不够用。define() 的功能可以让我们自行定义所需要的常量。见下例
<?<span>php </span><span>define</span>("COPYRIGHT", "Copyright © 2000, netleader.126.com"<span>); </span><span>echo</span><span> COPYRIGHT; </span><span>echo</span> <span>__FILE__</span><span>; </span>?>
4、声明变量(区分大小写)
<?<span>php </span><span>/*</span><span>* * @file variable.php * @author suguolong * @date 2015/07/29 16:49:08 * @brief * *</span><span>*/</span> <span>/*</span><span> 定义字符串变量 </span><span>*/</span> <span>$mystring</span> = "我是字符串"<span>; </span><span>$WilsonPeng</span> = "真是认真的作者"<span>; </span><span>$NewLine</span> = "换行了\n"<span>; </span><span>/*</span><span> 定义整型变量 </span><span>*/</span> <span>$int1</span> = 38<span>; </span><span>$int2</span> = 49<span>; </span><span>$hexint</span> = 0x10<span>; </span><span>/*</span><span> 定义浮点变量 </span><span>*/</span> <span>$float1</span> = 1.732<span>; </span><span>$float2</span> = 1.4E+2<span>; </span><span>/*</span><span> 定义数组变量 </span><span>*/</span> <span>$MyArray1</span> = <span>array</span>("子", "丑", "寅", "卯"<span>); </span><span>$MyArray2</span> = <span>array</span><span>( </span>"地支" => <span>array</span>("子", "丑", "寅", "卯"), "生肖" => <span>array</span>("鼠", "牛", "虎", "兔"), "数字" => <span>array</span>(1, 2, 3, 4<span>) ); </span><span>/*</span><span> 类的定义 </span><span>*/</span> <span>class</span><span> foo { </span><span>function</span><span> do_foo () { </span><span>echo</span> "Doing foo.\n"<span>; } } </span><span>/*</span><span> 类的使用 </span><span>*/</span> <span>$bar</span> = <span>new</span><span> foo; </span><span>$bar</span> -><span> do_foo (); </span><span>$bar</span> -><span> do_foo (); </span><span>$bar</span> -><span> do_foo (); </span><span>/*</span><span> 定义布尔值 </span><span>*/</span> <span>$booleanval_true</span> = <span>true</span><span>; </span><span>$booleanval_false</span> = <span>false</span><span>; </span><span>/*</span><span> 使用变量 </span><span>*/</span> <span>echo</span> "boolean value of true: \n"<span>; </span><span>echo</span> <span>$booleanval_true</span><span>; </span><span>echo</span> "\n"<span>; </span><span>echo</span> "boolean value of false: \n"<span>; </span><span>echo</span> <span>$booleanval_false</span><span>; </span><span>echo</span> "\n"<span>; </span><span>/*</span><span> vim: set expandtab ts=4 sw=4 sts=4 tw=100: </span><span>*/</span> ?>
[suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$ php variable.php Doing foo. Doing foo. Doing foo. boolean value of true: 1 boolean value of false: [suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$
5、变量的使用
在 PHP 的程序执行时,系统会在内存中保留一块全局变量的区域。实际运用时,可以透过 $GLOBALS["变量名称"] 将需要的变量取出。
$GLOBALS 数组是 PHP 程序中比较特殊的变量,不必定义,系统会自动匹配相关的变量在里面。在函数中,也不必管 $GLOBALS 数组是否已经做全局定义,就可以直接使用了。
和 $GLOBALS 变量类似的还有 $php_errormsg 字符串变量。若 PHP 的配置文件 (php.ini/php3.ini) 中的 track_errors 选项打开的话,会有全局变量 $php_errormsg 可以看到错误的信息。
在 PHP 中,全局变量的有效范围 (scope) 仅限于主要程序中,不会影响到函数中同名的变量,也就是全局变量与局部变量互不侵犯。若要变量能通透到函数中,就要用到 $GLOBALS 数组或是使用 global 定义。
至于用户在 FORM 中输入的资料,要怎么处理呢?要是在 PHP 的配置文件中,track_vars 设为 On 时,直接使用变量名字就好了。如下例,next.php 在执行时,系统会自动产生两个变量 $username 及 $sex,直接使用就好了,比起传统的 CGI 要自己解析,PHP 实在是太神奇了。
<form action=<span>next</span>.php method=post><span> 姓名</span>: <input type=text name="username"><br><span> 性别</span>: <input type=text name="sex"><br> <input type=submit> </form>

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

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