Home Backend Development PHP Tutorial php与mysql三日通-第三天_PHP

php与mysql三日通-第三天_PHP

Jun 01, 2016 pm 12:43 PM
echo function variable Can us document program

一、基本函数

  欢迎来到本教程的第三课,也是最后一课。如果您已经学过第一课和第二课,那么您已经掌握了MySQL和PHP的安装及编程的基本知识。下面我们要介绍PHP的一些其他函数,这些函数可能会对您有用,使您的开发过程更加简单。首先我们来看看头文件。

  大家应该知道头文件的一些基本概念吧?头文件是一个外部文件,它的内容被包含到主程序中。方法也十分简单:在程序文件中引用头文件名,这个头文件就会包含进来了。在PHP中使用头文件,会涉及两个函数:include()和require()。这两个函数差别很小,却很重要,所以我们要认真研究一下。require()函数工作方式与XSSI相类似;不管在程序的哪个部分使用了这个函数,只有程序一开始运行,头文件的内容就被作为程序本身的一部分来处理。因此,如果您在一个条件判定语句中使用了require()函数,那么即使这个条件即使不为真,头文件也会被包含进来。

  而include()函数只是在执行到这一条语句时才会把头文件内容包含进来。如果程序没运行到这里,那PHP是不会管它的。这就意味着,您在条件判定部分使用include时,它会完全按照您希望的那样工作。

  还有,如果您用了require()函数,而您指定的头文件并不存在,那么程序将会停止运行并产生错误。如果您用了include(),程序会产生一个警告信息,但是会继续运行。您可以亲自试一下,运行下面的程序,然后把include()换成require(),再比较两个程序运行的结果。




include(\"emptyfile.inc\");
echo \"Hello World\";
?>

 

 我喜欢把头文件的后缀名起成.inc,这样就可以把头文件和一般的程序区分开来。如果您也这么做,那么请您修改Web服务器软件的配置文件,使它能够把.inc文件也当作PHP文件来处理。否则,黑客们也许会猜到您的头文件名,然后用浏览器把头文件内容以纯文本格式显示出来。此时如果您的头文件中有些机密信息(如数据库口令等)那就糟糕了。

  那么,您用头文件来做什么呢?很简单!把对所有程序都通用的那些内容放到头文件里。象HTML文件头啦,脚注啦,数据库连接代码啦,还有您自己定义的一些函数什么的。把下面的文字拷贝到一个文件中,保存为header.inc。


$db = mysql_connect(\"localhost\", \"root\");
mysql_select_db(\"mydb\",$db);
?>


<br><?php echo $title ?><br>


 

  然后再创建另外一个文件,名字是footer.txt,该文件可以包含一些程序结束时用到的一些文字和标记。

  现在,我们再来创建一个文件,这个文件里面是真正的PHP程序代码。试一下下面的代码,当然,您要确认MySQL数据库服务器正在运行。


$title = \"Hello World\";
include(\"header.inc\");
$result = mysql_query(\"SELECT * FROM employees\",$db);
echo \"

\n\";
echo \"\n\";
while ($myrow = mysql_fetch_row($result)) {
printf(\"\n\", $myrow[1], $myrow[2], $myrow[3]);
}
echo \"
名字 职位
%s %s %s
\n\";
include(\"footer.inc\");
?>

 

  看到发生了什么事了吗?头文件里的内容被合并到程序中,PHP把所有的代码都执行了一遍。注意在包含header.inc头文件之前$title是如何定义的。在header.inc中的代码可以访问到它的值。这样,网页的标题就被改掉了。现在,您可以在任何程序中使用header.inc头文件了,您所要做的不过是在每个主程序中为$title变量取一个合适的值。

  头文件、HTML、条件判定语句,还有循环语句,这些东西加在一些,您就可以用最简练的代码,写出功能各异的各种复杂程序来。在与函数同时使用时,头文件更能发挥它的效力,我们后面就会看到。

  接下去,我们会介绍精彩的部分:数据校验。>>

 

二、 数据校验

  想象一下这样的情形:我们把数据库都设计妥当了,现在请用户输入信息来写到数据库中去。假设您有一个字段是要求数字类型的信息,比如价格;而某个可爱的用户,却在这一栏里输入了文字信息,使得您的应用程序的执行过程出现了故障。对您在SQL语句中提供的文字类型的数据,MySQL数据库拒不接受,并向您提出了“严正抗议”。

  怎么办呢?您要用数据校验来防止以上状况发生。

  简单地讲,数据校验是指我们对数据(通常是用户经由HTML表格传过来的)进行检查,看看它是否遵从一定的规则。规则可以是多种多样的,比如某一数据元素不能为空,或者要求某一数据项的内容必须满足一定的要求(例如前面的例子中要求必须是数字而不是文字,或者要求电子邮件地址中一定要包含一个“@”字等等)。

  数据校验既可以在服务器一端作,也可以在客户端来作。PHP是用来作服务器一端的数据校验的,而JavaScript或其他客户端脚本编程语言则能够提供客户端的数据校验功能。本文说的是PHP,所以我们在这里着重介绍服务器端的校验。如果您想找一些现成的、在客户端运行的数据较验程序,那您可以去网猴程序库看看。

  暂时把数据库放在一边不谈,我们先来说说PHP的数据校验方法。如果您愿意(或者说,您想记录我们要校验的那些数据的话),您可以在前面所建的员工数据库的里加入其他字段,很简单,用MySQL的ALTER 语句就行了。

  有好几个PHP功能都可以用来作数据校验的工作,有些很简单,有些则复杂一些。其中strlen()是比较简单的一个函数,它能够告诉我们一个变量的长度。

  更复杂一点儿的是ereg(),这个函数可以处理完整的常规表达式来进行复杂的校验。我不想就常规表达式讲得太深,因为许多书都是专门写这个问题的。不过我会在下一页中给出一些简单的例子。

  我们先从一个简单的例子开始吧。下面这个程序要检查一个变量是否存在。




if ($submit) {
if (!$first || !$last) {
$error = \"对不起,您必须填写所有的栏目!\";
} else {
// 处理表格输入内容
echo \"谢谢!\";
}
}
if (!$submit || $error) {
echo $error;
?>


\">
第一栏: \">

第二栏: \">



} // if结束
?>

 

  这段程序中关键的地方是嵌套的条件判定语句。第一层检查用户是否按了发送数据的按钮。如果是,程序接着检查$first和$last两个变量是否都存在。那个 || 符号表示“或”,而 ! 符号表示“非”。那一句程序用一般语言描述就是“如果$first不存在或者$last不存在,那么就把 $error变量置成下面的值。”

  接下来,我们再进一步,检查一段文字的长度。这对用户口令的检查是很有必要的,因为您不想让某些懒惰的用户输入只有一、两个字的口令,可能会要求他们输入六位长的口令。

  我们已经讲到strlen()这个函数了。它只是简单地返回一个数字,该数字等于被测变量中所包含的字符个数。这里,我修改一下上面的程序,检查一下$first与$last的长度。

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

Video Face Swap

Video Face Swap

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

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)

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

See all articles