Table of Contents
PHP编程基本语法快速入门手册,基本语法快速入门
您可能感兴趣的文章:
Home php教程 php手册 PHP编程基本语法快速入门手册,基本语法快速入门

PHP编程基本语法快速入门手册,基本语法快速入门

Jun 13, 2016 am 08:48 AM
php grammar

PHP编程基本语法快速入门手册,基本语法快速入门

php脚本的后面名为.php,代码放置在下面的括号里面:

<&#63;php
.......
&#63;>
Copy after login

echo可以打印信息,类似于printf。

<&#63;php
echo "hallo world";
&#63;>
Copy after login

每条语句后面用分号结尾;。

php支持三种注释方式:

<&#63;php
// 第一种

# 第二种

/*
这是
多行
注释
*/
&#63;>

Copy after login

在php中,函数、类、和关键词的大小写都是同一个东西:

<!DOCTYPE html>
<html>
<body>

<&#63;php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
&#63;>

</body>
</html>

Copy after login

不过所有变量都对大小写敏感,需要区分大小写。

<&#63;php
$color="red";
$Color="black";
echo "my car is " . $color . "<br>";
echo "my car is " . $Color . "<br>";
&#63;>
Copy after login

上面的例子同时指出了变量定义以及字符串拼接的语法。

变量命名规则:

PHP 变量规则:

  • 变量以 $ 符号开头,其后是变量的名称
  • 变量名称必须以字母或下划线开头
  • 变量名称不能以数字开头
  • 变量名称只能包含字母数字字符和下划线(A-z、0-9 以及 _)
  • 变量名称对大小写敏感($y 与 $Y 是两个不同的变量)
  • 变量会在第一次赋值时被创建。无需声明变量的类型。

变量会有三种不同的作用域:

PHP 有三种不同的变量作用域:

  1. local(局部)
  2. global(全局)
  3. static(静态)

函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。
函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。
下面的例子测试了带有局部和全局作用域的变量:

<&#63;php
$x=5; // 全局作用域

function myTest() {
 $y=10; // 局部作用域
 echo "<p>测试函数内部的变量:</p>";
 echo "变量 x 是:$x";
 echo "<br>";
 echo "变量 y 是:$y";
} 

myTest();

echo "<p>测试函数之外的变量:</p>";
echo "变量 x 是:$x";
echo "<br>";
echo "变量 y 是:$y";
&#63;>

Copy after login

运行结果:

测试函数内部的变量:

变量 x 是:
变量 y 是:10
测试函数之外的变量:

变量 x 是:5
变量 y 是:
比较奇怪的就是为什么全局变量不能在局部函数内访问。

其实可以访问,不过需要global关键字的帮助:

global 关键词用于访问函数内的全局变量。
要做到这一点,请在(函数内部)变量前面使用 global 关键词:

<&#63;php
$x=5;
$y=10;

function myTest() {
 global $x,$y;
 $y=$x+$y;
}

myTest();
echo $y; // 输出 15
&#63;>

Copy after login

PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。下标存有变量名。这个数组在函数内也可以访问,并能够用于直接更新全局变量。

<&#63;php
$x=5;
$y=10;

function myTest() {
 $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
} 

myTest();
echo $y; // 输出 15
&#63;>

Copy after login

通常,当函数完成执行后,会删除所有变量。不过,有时我需要不删除某个局部变量。实现这一点需要static:

<&#63;php

function myTest() {
 static $x=0;
 echo $x;
 $x++;
}

myTest();
myTest();
myTest();

&#63;>

Copy after login

php中echo和print都能使用,两者的唯一区别是print返回1,echo没有返回值。

strlen函数可以返回字符串的长度。

strpos函数用来确定另外一个字符串的位置:

<&#63;php
echo strpos("Hello world!","world");
&#63;>

Copy after login

使用define函数来定义常量:

<&#63;php
define("GREETING", "Welcome to W3School.com.cn!");
echo GREETING;
&#63;>
Copy after login

define函数还有第三个参数,用来指定是否大小写敏感。

php的if-else语句和其他语言大同小异,举个例子:

<&#63;php
$t=date("H");

if ($t<"10") {
 echo "Have a good morning!";
} elseif ($t<"20") {
 echo "Have a good day!";
} else {
 echo "Have a good night!";
}
&#63;>

Copy after login

switch-case语句:

<&#63;php
switch ($x)
{
case 1:
 echo "Number 1";
 break;
case 2:
 echo "Number 2";
 break;
case 3:
 echo "Number 3";
 break;
default:
 echo "No number between 1 and 3";
}
&#63;>
Copy after login

while、for语句和其他语言无差别,看看foreach吧:

<&#63;php 
$colors = array("red","green","blue","yellow"); 

foreach ($colors as $value) {
 echo "$value <br>";
}
&#63;>

Copy after login

php的真正力量来自于它的函数,它有1000个内置函数。

用户定义的函数声明以关单 "function" 开头:

function functionName() {
被执行的代码;
}
举个例子:

<&#63;php
function writeMsg() {
 echo "Hello world!";
}

writeMsg(); // 调用函数
&#63;>

// 含参数
<&#63;php
function familyName($fname,$year) {
 echo "$fname Zhang. Born in $year <br>";
}

familyName("Li","1975");
familyName("Hong","1978");
familyName("Tao","1983");
&#63;>

// 默认参数
<&#63;php
function setHeight($minheight=50) {
 echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // 将使用默认值 50
setHeight(135);
setHeight(80);
&#63;>

// 返回值
<&#63;php
function sum($x,$y) {
 $z=$x+$y;
 return $z;
}

echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
&#63;>

Copy after login

在 PHP 中,有三种数组类型:

  1. 索引数组 - 带有数字索引的数组
  2. 关联数组 - 带有指定键的数组
  3. 多维数组 - 包含一个或多个数组的数组

索引数组:

$cars=array("Volvo","BMW","SAAB");
<&#63;php
$cars=array("Volvo","BMW","SAAB");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
&#63;>

// count
<&#63;php
$cars=array("Volvo","BMW","SAAB");
echo count($cars);
&#63;>

// 变量索引数组
<&#63;php
$cars=array("Volvo","BMW","SAAB");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++) {
 echo $cars[$x];
 echo "<br>";
}
&#63;>

Copy after login

关联数组:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
Copy after login

$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";

<&#63;php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
&#63;>




// 遍历
<&#63;php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");

foreach($age as $x=>$x_value) {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
}
&#63;>

Copy after login

数组排序方法有下面这些:

  • sort() - 以升序对数组排序
  • rsort() - 以降序对数组排序
  • asort() - 根据值,以升序对关联数组进行排序
  • ksort() - 根据键,以升序对关联数组进行排序
  • arsort() - 根据值,以降序对关联数组进行排序
  • krsort() - 根据键,以降序对关联数组进行排序

比较难理解的是键值对排序。

<&#63;php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");
asort($age);
&#63;>

<&#63;php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");
ksort($age);
&#63;>

Copy after login

超全局变量,也就是预定义的全局变量,在哪里都能用,有特殊含义:

  • $GLOBALS:引用全局作用域中可用的全部变量
  • $_SERVER:保存关于报头、路径和脚本位置的信息。
  • $_REQUEST:用于收集 HTML 表单提交的数据。
  • $_POST:用于收集提交 method="post" 的 HTML 表单后的表单数据。也常用于传递变量。
  • $_GET:$_GET 也可用于收集提交 HTML 表单 (method="get") 之后的表单数据。
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

其他内容可以在表单处理中找到。

您可能感兴趣的文章:

  • PHP 基本语法格式
  • PHP教程 基本语法
  • PHP insert语法详解
  • PHP语法速查表
  • PHP语法自动检查的Vim插件
  • php正则表达式的基本语法总结
  • 前端必学之PHP语法基础
  • PHP语法小结之基础和变量
  • PHP基本语法总结
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

See all articles