PHP基础教程(php入门基础教程)一些code代码_PHP
在此教程之前,我就不长篇一律的说什么PHP的常用了。 关于什么是变量呀什么是判断语句呀什么的,请自行查询相关资料此教程值针对于有编程基础,且对PHP陌生的人看。文章比较简单。主要看结构。详细的还请大家自己多多研究
PHP环境安装:
PHP通常组合是 :MySql+PHP+Apche 也有 IIS+PHP+MySQL或SqlServer
当然我们可以进行选择组合包来进行安装。 新手建议装AppServ或phpnow等。
iis下可以用这个安装运行一下就支持php了,mysql需要安装一下。
也可以进行自己安装各个部分。然后自己进行配置。
PHP各版本的下载地址:http://museum.php.net/php5/
Apche下载地址:http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe?download
MySQL下载地址:http://www.mysql.cn/
配置安装教程:http://wenku.baidu.com/view/c6118b1810a6f524ccbf85f9.html
或者 http://www.bitsCN.com/article/33062.htm
编写工具:建议用Notepad++或者dreamweaver cs4
====================================================================
语法:
PHP的语法很简单 --直接看代码: 这就是PHP代码的声明方式。 注: ?> 等这中写法也可以写,但是不建议这么写。
标记语句的结束:分号是标记一条语句的结束 ";" --每条语句结束后要用“;”分号表示结束.
=====================================================================
PHP中的注释: --详见教程中的code
php中的注释有单行注释: //这是注释
和大模块注释:/*这是注释*/
=====================================================================
变量:
PHP变量是松散的。但是它也区分大小写,这点大家要注意。 在使用它之前,无需声明 -根据变量声明方式,PHP会自动把变量转换成正确的数据类型.
在PHP中声明变量使用$关键字来声明 --所有的变量都是由$来标识的
变量命名规则:
变量名必须以字母或下划线 "_" 开头。
变量名只能包含字母数字字符以及下划线。
变量名不能包含空格。如果变量名由多个单词组成,那么应该使用下划线进行分隔(比如 $my_string),或者以大写字母开头(比如 $myString)。
注:(基本上所有的编程语言的变量命名规则都差不多!)
示例:
复制代码 代码如下:
//声明变量
$var_name = "snow";
//使用变量
echo $var_name;
/*
显示结果: snow
*/
?>
常量:
PHP中常量的声明:
在PHP中声明常量使用define函数来声明的 。直接看code
复制代码 代码如下:
/*
define函数有三个参数
第一个参数:指定常量名 --不得使用关键字,常量不能有$符号
第二个参数:指定常量的值 --只能是布尔、整数、浮点、字符串四个类型
第三个参数:指定此常量是否对大小写敏感 --true忽略大小写,false区分大小写
*/
define("Name","张三",true);
echo name;
/*显示结果:张三 --因为是true所以不区分大小写*/
?>
PHP中也有预定义常量 --大家可以查询PHP手册或者相关资料
=====================================================================
数组: --PHP的数组还是比较简单好用的。
PHP数组可以当作其它语言中的集合使用
PHP数组里可以存放PHP支持的任何类型。当然也可以存放 类对象等 --直接看code
复制代码 代码如下:
/*===================================================================*/
//数值数组
$nums = array(1,2,3);
//或者等同于
$nums[0] = 1;
$nums[1] = 2;
$nums[2] = 4;
echo $nums[2]."
";
/*输出:4*/
/*===================================================================*/
//关联数组 --其中的“=>”是PHP中的关联符号,就是指定键值对的。
$ns = array("name"=>"张三","age"=>22,"sex"=>"man");
//或者等同于
$ns["name"] = "张三";
$ns["age"] = 22;
$ns["sex"] = "man";
echo "姓名:".$ns["name"]."
年龄:".$ns["age"]."
性别:".$ns["sex"]."
";
/*输出:
姓名:张三
年龄:22
性别:man
*/
/*===================================================================*/
//多维数组 --数组里面还可以存放数组
$bs = array("张三"=>array("爱好"=>"计算机","年纪"=>"23","性别"=>"男"),"小红"=>array("爱好"=>"吃饭","性别"=>"女"));
//调一下格式,让大家看的清楚些
$bs = array
(
"张三"=>array
(
"爱好"=>"计算机",
"年纪"=>"23",
"性别"=>"男"
),
"小红"=>array
(
"爱好"=>"吃饭",
"性别"=>"女"
)
);
//或者等同于
$bs["小红"]["性别"] = 2; $bs["小红"]["爱好"] = 2; //....
//或
$bs["张三"] = array("爱好"=>"计算机","年纪"=>"23","性别"=>"男"); $bs["小红"] = array("爱好"=>"吃饭","性别"=>"女");
echo $bs["小红"]["性别"]."
";
/*输出:女*/
/*===================================================================*/
?>
=====================================================================
PHP运算符: --摘录w3school的教程
本部分列出了在 PHP 中使用的各种运算符:
算数运算符
运算符 | 说明 | 例子 | 结果 |
---|---|---|---|
+ | Addition | x=2 x+2 |
4 |
- | Subtraction | x=2 5-x |
3 |
* | Multiplication | x=4 x*5 |
20 |
/ | Division | 15/5 5/2 |
3 2.5 |
% | Modulus (division remainder) | 5%2 10%8 10%2 |
1 2 0 |
++ | Increment | x=5 x++ |
x=6 |
-- | Decrement | x=5 x-- |
x=4 |
运算符 | 说明 | 例子 |
---|---|---|
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
.= | x.=y | x=x.y |
%= | x%=y | x=x%y |
比较运算符
运算符 | 说明 | 例子 |
---|---|---|
== | is equal to | 5==8 returns false |
!= | is not equal | 5!=8 returns true |
> | is greater than | 5>8 returns false |
is less than | 5 | |
>= | is greater than or equal to | 5>=8 returns false |
is less than or equal to | 5 |
逻辑运算符
运算符 | 说明 | 例子 |
---|---|---|
&& | and | x=6 y=3 (x 1) returns true |
|| | or | x=6 y=3 (x==5 || y==5) returns false |
! | not | x=6 y=3 !(x==y) returns true |
程序判断语句:
和C#、java、C等判断语句一样。有if..else/else..if、switch语句 --直接看Code
复制代码 代码如下:
$name = "张三"; //声明变量
/*if..else只会语句只会执行其中一个,一个条件成立。就算后面的也成立,都会被忽略掉*/
//判断名字是否是张三
if($name == "张三")
{
echo "张三";
}
else if($name == "李四") //接着判断
{
echo "李四";
}
else //以上都不是就走进else
{
echo "其它";
}
print('
'); //打印输出
$num = 1;
/*
switch选择结构 可if的原理差不多。只是在case里要加break --当然也可以不加。
这样的话执行玩case 1后并不会跳出去,而是继续执行下一个case分支。直到遇到break才跳出去、。大家可以自己试下
*/
switch($num)
{
case 1:
echo "一";
break;
case 2:
echo "二";
break;
default: //默认分支。当条件都不成立的时候执行。
echo "其它";
}
/*
最终执行的结果是:
张三
一
*/
?>
PHP循环:
和其它强类型的编程语言一样。php也有while、do while、for、foreach --直接看code
复制代码 代码如下:
$index = 1;
while($index {
echo "第".$index."次"."
";
$index++; //累加
}
/*以上结果输出10次*/
echo '
';
$index = 1;
do
{
echo "第".$index."次"."
";
$index++;
}
while($index
/*以上结果输出1次*/
echo '
';
for($index = 1;$index {
echo "第".$index."次"."
";
}
/*以上结果输出3次*/
echo '
';
$index = array("1","2","3");
foreach($index as $temp) //遍历数组
{
echo "值:".$temp."
";
}
/*以上结果输出3次*/
?>
PHP函数:
php函数的声明很简单,只要前面加上关键字function后面跟函数名就行了。--具体格式直接看code
复制代码 代码如下:
/*PHP函数*/
//无参函数
function MyEcho()
{
echo "无参函数
";
}
//有参函数 --传入的参数也可以是类对象
function MyEcho2($str)
{
echo $str;
}
MyEcho(); //输出:无参函数
MyEcho2("嘻嘻哈哈!"); //输出:嘻嘻哈哈!
?>
PHP类:
php也像其它高级语言一样,支持面向对象编程。在这里我说基础部分php类的声明。有关于面向对象的编程方式,大家自行研究
php声明类的方式,也要加关键字 class --具体看code -(其中包括静态函数。函数调用等)
复制代码 代码如下:
class MyClass //类的声明
{
private $jum1; //定义私有变量
private $jum2;
static public $test = "测试静态方法"; //定义公有变量
function Calc() //类函数
{
return $this->jum1+$this->jum2; // "->" 符号是类调用的意思
}
function SetNum($Num1,$Num2) //有参类函数
{
$this->jum1 = $Num1;
$this->jum2 = $Num2;
return $this; //这里要返回类对象本身
}
static function Tt()
{
echo "
".MyClass::$test."
";
}
}
/*实现计算功能*/
$temp = new MyClass;
echo $temp->SetNum(2,8)->Calc(); //输出:10
MyClass::Tt(); //"::"静态调用 //输出:测试静态方法
?>
PHP表单处理:
在页面用户提交值的时候用 $_GET 和 $_POST 或 $_REQUEST (它包含了$_GET、$_POST和$_COOKIE)系统定义的变量来读取提交过来的值 --看code
复制代码 代码如下:
echo $_POST["xx"]."
"; //读取post值
echo $_REQUEST["xx"];
//用get读取值。自己试
?>
暂时就这么多了...如果有时间,我会写下PHP常用的应用。高级部分。(包括会话、cookie、面向对象、常用函数等等)

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

AI Hentai Generator
Generate AI Hentai for free.

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 popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

As a popular server-side scripting language, PHP can be used not only for the development of Web applications, but also for TCP/IP programming and network programming. In this article, we will introduce you to the basics of TCP/IP programming and how to use PHP for TCP/IP programming. 1. Basic knowledge of TCP/IP programming TCP/IP protocol is the standard protocol for communication on the Internet. It is composed of two parts: TCP protocol and IP protocol. The TCP protocol is responsible for establishing reliable connections

In web development, file uploading and downloading is a very common requirement. Whether users upload avatars or documents, or administrators ask users to download a file, this function is needed. As a powerful server-side language, PHP naturally provides powerful file operation functions and class libraries, allowing us to easily implement file upload and download functions. This article will introduce the basic process and common functions to implement file upload and download in PHP, and provide sample code. If you are a PHP beginner or are learning file operations

PHP is a widely used programming language, especially in web development, PHP occupies an important position. Among them, JSON is a common data format that can be used to store and transmit data. JSON extensions are provided in PHP to facilitate developers to operate and process JSON data. This article will introduce the basic usage and application scenarios of JSON extension. 1. Basic usage of JSON extension. Convert JSON string into PHP object or array. The json_decode() function in PHP can convert

For PHP beginners, it is very important to understand HTTP status codes. HTTP status code refers to the 3-digit code returned by the web server and is used to indicate the processing result of the client request. This article will introduce some common HTTP status codes and their meanings to help PHP beginners better understand the various HTTP status codes encountered during website development. 200OK200OK is one of the most common HTTP status codes, indicating that the request was successfully processed and a result was returned. When you visit a website, such as

PHP is a server-side scripting language that is used to develop dynamic websites, web applications, and web programs. PHP has a wide range of applications, and both beginners and experienced developers can benefit from it. This article will provide you with an introductory guide to the basic syntax of PHP. If you want to learn PHP programming and build a solid foundation from scratch, you've come to the right place. The basic structure of PHP. A PHP program contains the following three parts: <?php//PHP code?>& on both sides of the code

PHP is a very popular programming language that is often used in the field of Internet development. In PHP development, cache settings are a very important part. Caching can improve website performance and user experience, reduce server load, and is one of the common methods for website optimization. This article will introduce you to the introductory guide to setting up PHP cache. 1. What is cache? Caching is to store some frequently accessed data in memory so that it can be quickly obtained the next time it is accessed, avoiding repeated calculations or database queries and improving response speed. PHP, slow

In software development, version management is an extremely important link. Because writing code in a team inevitably requires merging everyone's code. Version management tools can help us track code changes and avoid conflicts when merging. Among them, Git is currently the most popular version management tool, a must-have for both personal development and team collaboration. This article will focus on Git to introduce you to the benefits of using version management tools, the basic concepts and basic operations of Git, and explain how to use Git to collaborate with your team for development. Why do we need versions
