Home Backend Development PHP Tutorial Php引见-基本语法

Php引见-基本语法

Jun 13, 2016 pm 12:53 PM
arr array echo gt php

Php介绍-基本语法

1、?

1.1php是什么:

PHP(Hypertext Preprocessor,超文本预处理器)是一种应用广泛、开放源代码的、功能强大的嵌入html中的脚本语言。

PHP是免费的。

效率高的,PHP消耗相当少的系统资源。

易学易用,功能强大。

跨平台的,可以运行在UNIXLINUXWINDOWS下。

PHP5是面向对象的。

1.2php的优势:

?

1.3、运行环境:

可运行在Window/Linux系统上,需要安装的软件:

1)Apache服务器的安装,是web服务器,默认不支持php。下载网址:www.apache.org

2)Php引擎的安装,是php的运行环境。下载网址:www.php.net

1.4、环境搭建:

见:附-wamp开发环境搭建.docx

?

?

2、Php基本语法

2.1php书写规范:

Php代码块的标记共有四套:

… ?>

一般使用第一种方式,后两种需要在php.inishort_open_tag = on;asp_tags = on;配置才能使用。

注:apache服务器处理页面的原理:

普通的htmljavascriptcss代码原样输出;

Php代码由php引擎解释,最终输出html代码;

一般情况下,apache*.htm*.html文件不作php解释,将原样输出;

所以,在普通的.htm.html页面中,不能写php代码。

示例:0101.php

2.2php注释:

三种注释方式:

1)//???? ????????????? 单行注释

2)/* */????????????? 多行注释

3)#???? ????????????? Unix风格注释

示例:0102.php

2.3php中的变量:

由美元符$后面跟变量名组成,且大小写敏感。变量名由字母、数字、下划线组成,且以字母或下划线开头。如:

$num = 1;

$_var = 2;

?>

示例:0103.php0104.php

2.4php中的常量:

define()函数来定义常量,常量前没有美元符$,值只能是标量(booleanintegerfloatstring),一个常量一旦被定义就不能再改变或取消。常量大小写敏感,一般都大写。

define(“CONSTANT”,”Hello Word”);

echo CONSTANT;

?>

示例:0105.php

2.5php支持八种原始类型:

四种标量类型:

布尔型(boolean)、整型(integer)、浮点型(float)(也作double)、字符型(string)

两种复合类型:

数组(array)、对象(object)

两种特殊类型:

资源(resource)NULL

1)、布(boolean)

00.0、空数组、空字符串转为布尔型时为false;

-1和其它非零值一样转为布尔型时为true;

echo gettype((bool) "");??????????????????? // bool(false)
echo gettype((bool) 1);??????????????????? // bool(true)
echo gettype((bool) -2);?????????????????? // bool(true)
echo gettype((bool) "foo");?????????????? // bool(true)
echo gettype((bool) 2.3e5);????????????? // bool(true)
echo gettype((bool) array(12)); ???? // bool(true)
echo gettype((bool) array());?????????? // bool(false)
?>

2)、整型(integer)

可以用十制、十六(数字前加0x)、八进制(数字前加0)符号指定。如:

$a = 1234; # 十进制数
$a = -123; #
一个负数
$a = 0123; #
八进制数(等于十进制的 83
$a = 0x1A; #
十六进制数(等于十进制的 26
?>

3)、浮点型(float)

$a = 1.234;
$a = 1.2e3;
$a = 7E-10;
?>

4)、字符型(string)

字符串除了可以用引号、双引号来定外,可以通定界符来定

定界符:字符串定界的方法使用定界符语法(“)。应该在之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。

$str = Example of string
spanning multiple lines
using heredoc syntax.
EOD;

示例:0106.php

5)、数(array)

Php中的数组实际上是一个有序图,一种把values映射到keys的类型。

array()语法结构:array([key=>]value,…)key可以是integerstring,如:

$arr = array(“foo”=>”bar”,12=>true);

echo $arr[“foo”];//bar

echo $arr[12];//1

?>

$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6];????// 5
echo $arr["somearray"][13];???// 9
echo $arr["somearray"]["a"];??// 42
?>

?

数组的初始化:

方法1

$arr=array();

$arr["reg"]="redcolor";

$arr["blue"]="bluecolor";

$arr["white"]=9000;

foreach($arr as $key=>$value)

{

?echo $key."---".$value;

}

?>

方法2

$arr=array(‘red’=>’regcolor’,’blue’=>’bluecolor’);
$arr=array(‘1’,’33’,’44’,’55’);

示例:0106.php

?

数组的相关函数:

each($arr) 返回当前元素,并向下移动数组。
list()
一般与each()搭配使用,将数组元素分解一系列的值。
count($ayy)
获得数组的个数。
unset($arr[0])
删除数组中的元素。
array_slice($ayy,int offset,[int length])
获得数组的子集。
array_unshift($ayy,key=>value)
向数组开头插入元素。
array_push($ayy,key=>value)
向数组结尾插入元素。
sort($arr)?
正向排序
rsort($arr)
反向排序

如:

while($el=each($arr))

{

? echo "
".$el["key"];

? echo "
".$el["value"];

}


$arr=array('df','aa','bb');

while(list($a,$b)=each($arr))

{

? echo "
".$a;

? echo "
".$b;

}

?

$arr = array(5 => 1, 12 => 2);
$arr[] = 56;
$arr["x"] = 42;

unset($arr[5]);
unset($arr);
?>

: unset() 函数允许取消一个数组中的键名。要注意数组将不会重建索引。如:

$a = array( 1 => 'one', 2 => 'two', 3 => 'three' );
unset( $a[2] );
/*
将产生一个数组,定义为
??$a = array( 1=>'one', 3=>'three');
??
而不是
??$a = array( 1 => 'one', 2 => 'three');

*/
$b = array_values($a);
// Now b is array(1 => 'one', 2 =>'three')
?>

示例:0107.php0108.php

6)、对(object)

象初始化:new语句将对象实例到一个变量中。

class foo
{
????function do_foo()
????{
????????echo "Doing foo.";
????}
}
$bar = new foo;
$bar->do_foo();
?>

示例:0109.php

7)、资(resource)

8)NULL

NULL类型只有一个值,就是大小写敏感的NULL。以下情况一个变量被认为是NULL

赋值为NULL

尚未被赋值

unset()

如:

$var = NULL;
?>

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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.

See all articles