Home php教程 php手册 php入门基础篇-语法

php入门基础篇-语法

Jun 13, 2016 am 10:14 AM
php one one time getting Started Can Base have friend of Simple grammar

这是一篇简单的php语法入门篇,有需要的朋友可参考一下。

一、双引号与单引号
变量可以在双引号中执行,但用 define 的常量在两种单、双引号中都是不可执行的:

 代码如下 复制代码

$var = 'sofish';
echo 'im $var'; // => 'im $var'
echo "im $var"; // => 'im sofish'

define('NAME', 'sofish');
echo 'im NAME'; // => 'im NAME'
echo 'im ' . NAME; // => 'im sofish'

在 JS 中引号内不可以渲染任何变量。

二、变量的定义
使用 $ 符,而不是 var 关键字;有真正的常量,用 define 函数,一经定义,一可改变。

 代码如下 复制代码

// JS 中我们用 var name = 'sofish';
$name = 'sofish';

// JS 中我们用 var NAME = sofish; 而且是可以改变的
define('NAME', 'sofish');

// 在这里会报错,因为 NAME 已经被定义,而 JS 可以随时改变
define('NAME', 'error');

三、数组
排序:

可以使用 shuffle(),在 JS 中我们通常使用 Math.random 来设定在某个范围内,访问数组的下标;如果用数字来排序,小数点会被忽略,所以如果想准确地用数字排序,把数字变成字符串,比如 1.2 应写成 '1.2'。

可以字符串当 key:

 代码如下 复制代码

$arr = array('name' => 'sofish', 'age' => '25', 'gender' => 'male');

自动增加索引值:

下面这两个数据是相同的:

 代码如下 复制代码
$arr = array('1' => 'sofish', '25', 'male')
$arr = array('1' => 'sofish', '2' => '25', '3' => 'male')

四、连字符
在 PHP 中使用 .,在 JS 中使用 +。

五、换行符
在双引号中使用 "n",'n' 是不会转成换行符的。如果你习惯使用单引号,那么可以使用 PHP_EOL:

 代码如下 复制代码

echo 'im sofish,' . PHP_EOL;
echo "25 years old, n";
echo 'male';

// 想象一下下面这一段,在 stackoverflow 最受欢迎的回答
// 个人不喜欢混用两种引号,不好看,但未必不好。lol?!
echo 'im sofish' . "n" . '25 ...'在 JS 中,两种引号内写 n 都是可行的。

六、if 语句
在 PHP 中另一个分支用 elseif 而在 JS 中使用 else if,中间有一个空格之差。

UPDATE: 2012.02.29 5:28 pm:Andor: "其实 if 分支的另一个分支用 elseif 和 else if 都是可以的"。

七、函数
在 PHP 中不可以使用 (function(){})() 这样的函数来直接运行一个匿名函数,但是可以创建一个参数有默认值的函数,如:

 代码如下 复制代码

function fn($name, $greet = 'good moring '){
    echo $greet . $name;
};

// 当不传值时显示默认值,结果 >> 'good morning sofish'

 代码如下 复制代码
fn('sofish', 'good morning ');

// 当传入值的时候替换默认值,结果 >> 'good afternoon sofish'
fn('sofish', 'good afternoon ');作用域,我还是比较喜欢 JS 中的闭包,在 PHP 中,我们来看一下下面的代码:

 代码如下 复制代码
$a = 'hello';
$b = function(){
    echo $a . ' sofish.';
};
$b();

我们的预期结果是,’hello sofish.’。不过,我里有2个 $b,结果往往另我们意外。在 PHP 中,变量不能在自定义函数内使用,函数内部也不能使用外部已定义的变量(WTF),除非使用 global 关键字。上面的函数修改如下,则可使用:

 代码如下 复制代码
$a = 'hello';
$b = function(){
    global $a;
    echo $a . ' sofish.';
};
$b();

我们通常说,要慎用全局变量。在这里就更需要了。

八、list() 函数
很有趣的函数,可以利用数组中的值给一组变量赋值。我们平时在 JS 中是这样使用一个数组的:

// 返回一个数组,然后,用 `[]` 下标来调用

 代码如下 复制代码
var cursorPos = (function(){
     // render ...
     return [posX, posY];
})();

在 PHP 中可以使用 list() 根据数组的内容给一组变量赋值,这样我们就可以用变量来变量名来访问我们要用的值,而不是使用下标:

// 用括号内的变量来访问函数 `cursorPos` 返回的值
list($posX, $posY) = cursorPos();另外,值得一提的另一个有趣函数 ———— isset(),判断一个值非 NULL。还有诸如 is_array / is_string 这样的函数,都是在 JS 中没有提供的。像类型判断,算是 JS 中的高组内容了,只有比较熟悉和有积累的程序员都知道怎么做比较合理。

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.

See all articles