PHP入门级教程:教你快速学习PHP语言
烈火建站学院(Bkjia.Com)编程文档 本篇来介绍一些PHP入门级教程:快速学习PHP.
PHP的语法。
1、嵌入方法:
类似ASP的,当然您也可以自己指定。
2、引用文件:
引用文件的方法有两种:require 及 include。
require 的使用方法如 require(”MyRequireFile.php”); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。
include 使用方法如 include(”MyIncludeFile.php”); 。这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化。
3、注释方法:
以下为引用的内容: echo “这是第一种例子。n” ; // 本例是 C 语法的注释 /* 本例采用多行的 注释方式 */ echo “这是第二种例子。n” ; echo “这是第三种例子。n” ; # 本例使用 UNIX Shell 语法注释 |
4、变量类型:
以下为引用的内容: $mystring = “我是字符串” ; $NewLine = “换行了n” ; $int1 = 38 ; $float1 = 1.732 ; $float2 = 1.4E 2 ; $MyArray1 = array( “子” , “丑” , “寅” , “卯” ); |
这里引出两个问题,首先PHP变量以$开头,第二PHP语句以;结尾,可能ASP程序员会不适应。这两个遗漏也是程序上大多错误所在。
5、运算符号:
数学运算: 符号 意义
加法运算
- 减法运算
* 乘法运算
/ 除法运算
% 取余数
累加
– 递减
字符串运算:
运算符号只有一个,就是英文的句号。它可以将字符串连接起来,变成合并的新字符串。类似ASP中的&
以下为引用的内容: $a = “PHP 4″ ; |
这里也引出两个问题,首先PHP中输出语句是echo,第二类似ASP中的,PHP中也可以=变量? >。
逻辑运算:
符号 意义
> 大于
>= 大于或等于
== 等于
!= 不等于
&& 而且 (And)
and 而且 (And)
|| 或者 (Or)
or 或者 (Or)
xor 异或 (Xor)
! 不 (Not)
php的流程控制
1、if..else 循环有三种结构
第一种是只有用到 if 条件,当作单纯的判断。解释成 “若发生了某事则怎样处理”。语法如下:
if (expr) { statement } |
其中的 expr 为判断的条件,通常都是用逻辑运算符号当判断的条件。而 statement 为符合条件的执行部分程序,若程序只有一行,可以省略大括号 {}。
范例:本例省略大括号。
以下为烈火建站学院引用的内容: if ($state==1)echo “哈哈” ; ?> |
这里特别注意的是,判断是否相等是==而不是=,ASP程序员可能常犯这个错误,= 是赋值。
范例:本例的执行部分有三行,不可省略大括号。
以下为引用的内容: if ($state==1) { echo “哈哈 ; echo “ ” ; } ?> |
第两种是除了 if 之外,加上了 else 的条件,可解释成 “若发生了某事则怎样处理,否则该如何解决”。语法如下
if (expr) { statement1 } else { statement2 } 范例:上面的例子来修改成更完整的处理。其中的 else 由于只有一行执行的指令,因此不用加上大括号。
以下为引用的内容: if ($state==1) { echo “哈哈” ; echo “ ”; } else{ echo “呵呵”; echo “ ”; } ?> |
第三种就是递归的 if..else 循环,通常用在多种决策判断时。它将数个 if..else 拿来合并运用处理。
直接看下面的例子
以下为引用的内容: if ( $a > $b ) { echo “a 比 b 大” ; } elseif ( $a == $b ) { echo “a 等于 b” ; } else { echo “a 比 b 小” ; } ?> |
上例只用二层的 if..else 循环,用来比较 a 和 b 两个变量。实际要使用这种递归 if..else 循环时,请小心使用,因为太多层的循环容易使设计的逻辑出问题,或者少打了大括号等,都会造成程序出现莫名其妙的问题。
2、 for 循环就单纯只有一种,没有变化,它的语法如下
for (expr1; expr2; expr3) { statement }
其 中的 expr1 为条件的初始值。expr2 为判断的条件,通常都是用逻辑运算符号 (logical operators) 当判断的条件。expr3 为执行 statement 后要执行的部份,用来改变条件,供下次的循环判断,如加一..等等。而 statement 为符合条件的执行部分程序,若程序只有一行,可以省略大括号 {}。
下例是用 for 循环写的的例子。
以下为引用的内容: for ( $i = 1 ; $i echo “这是第”.$i.”次循环 ” ; } ?> |
3、 switch 循环,通常处理复合式的条件判断,每个子条件,都是 case 指令部分。在实作上若使用许多类似的 if 指令,可以将它综合成 switch 循环。
语法如下
switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; }
其中的 expr 条件,通常为变量名称。而 case 后的 exprN,通常表示变量值。冒号后则为符合该条件要执行的部分。注意要用 break 跳离循环。
以下为引用的内容: switch ( date ( “D” )) { case “Mon” : echo “今天星期一” ; break; case “Tue” : echo “今天星期二” ; break; case “Wed” : echo “今天星期三” ; break; case “Thu” : echo “今天星期四” ; break; case “Fri” : echo “今天星期五” ; break; default: echo “今天放假” ; break; } ?> |
这里需要注意的是break;别遗漏了,default,省略是可以的。
很明显的,上述的例子用 if 循环就很麻烦了。当然在设计时,要将出现机率最大的条件放在最前面,最少出现的条件放在最后面,可以增加程序的执行效率。上例由于每天出现的机率相同,所以不用注意条件的顺序。
构建数据库
在ASP中,如果是ACCESS数据库你可以直接打开ACCESS来编辑MDB文件,如果是 SQL SERVER你可以打开企业管理器来编辑SQL SERVER数据库,但是在PHP中,MY SQL的命令行编辑可能会令初学者感到很麻烦,不要紧,你下载一个PHPMYADMIN安装一下,以后建立编辑数据库可以靠它了。
下面说一下它的使用。
进入了phpmyadmin后,我们首先需要建立一个数据库,Language (*) 这里选择中文简体,然后在左边的 创建一个新的数据库 这里填写数据库名字,点击创建即可。
然后在左边下拉菜单中选择那个已经创建的数据库。在下面的
在数据库 shop 中创建一个新表 :
名字 :
字段数 :
中填写表名字和大致你认为的字段数(不够或者多了都不要紧,以后可以再添加或者缺省),按执行。
然后就可以开始建立表了。
第一栏是字段的名字;第二栏选择字段类型:
我们常用的是以下几个:
1)VARCHAR,文本类型
2)INT,整数类型
3)FLOAT,浮点数类型
4)DATE,日期型
5)大家或许会问,自动添加的ID在哪里?这个只要选择INT类型,在后面的额外中选择 auto_increment 就可以了。
- 共3页:
- 上一页
- 1
- 2
- 3
- 下一页

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

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
