search
HomeBackend DevelopmentPHP Tutorial 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
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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function