Home Backend Development PHP Tutorial Chapter 2: How to write PHP code for getting started with PHP_PHP Tutorial

Chapter 2: How to write PHP code for getting started with PHP_PHP Tutorial

Jul 21, 2016 pm 03:21 PM
php web one code use getting Started Writing method several kinds exist Base Embed recommend of page style

一.在web页面嵌入PHP代码的几种风格
推荐使用标准风格或简短风格

复制代码 代码如下:

//标准风格
echo 'Hello World!';
?>
//简短风格
echo 'Hello World!';
?>


二.代码注释的四种方式
复制代码 代码如下:

//单行注释
/*
* 多行注释
*/
#shell风格注释
/**
* PHPdoc风格注释
*/
?>

三.向浏览器输出字符串的几种方法
复制代码 代码如下:

/*
* echo函数的功能:向浏览器输出字符串
* 函数返回值:void
*/
echo 'echo function!';
echo('
');
/*
* echo函数的功能:向浏览器输出字符串
* 函数返回值:int
*/
print 'print function';
echo('
');
echo print 'echo value of print function. ';
echo('
');
/*
* printf函数的功能:向浏览器输出字符串
* 函数返回值:所打印字符串的长度
*/
printf("a weekend have %d days",7);
echo('
');
echo printf("a weekend have %d days",7);
echo('
');
/*
* sprintf函数的功能:把字符串保存到内存中
* 函数返回值:保存的字符串本身
*/
sprintf('sprintf function');
echo('
');
echo sprintf('sprintf function');
echo('
');
?>

输出结果:
echo function test!
print function test.
print function test. 1
a weekend have 7 days
a weekend have 7 days. 23
sprintf function test
常用类型指示符

类型

描述

%b

整数,显示为二进制

%c

整数,显示为ASCII字符

%d

整数,显示为有符号十进制数

%f

浮点数,显示为浮点数

%o

整数,显示为八进制数

%s

字符串,显示为字符串

%u

整数,显示为无符号十进制数

%x

整数,显示为小写的十六进制数

%X

整数,显示为大写的十六进制数

四.标识符与变量
1.标识符的基本规则:
1) 标识符可以是任意长度,而且可以由任何字母、数字、下划线组成。
2) 标识符不能以数字开始。
3) 在PHP中,标识符是区分大小写的。
4) 一个变量名称可以与一个函数名称相同。
2.变量赋值:
复制代码 代码如下:

$sum = 0;
$total = 1.22;
$sum = $total;
echo $sum; //1.22
?>

3.变量的数据类型:
基本数据类型

类型

名称

Integer

整数

Float

单精度浮点数

Double

又精度浮点数

String

字符串

Boolean

布尔

Array

数组

Object

对象

4.类型强度
PHP是动态语言,是一种非常弱的类型语言,在程序运行时,可以动态的改变变量的类型。
5.类型转换:
隐式类型转换:
复制代码 代码如下:

$sum = 0;
$total = 1.22;
$sum = $total;
echo gettype ( $sum );//double
?>

显式类型转换:
复制代码 代码如下:

$sum = 100;
$total = ( string ) $sum;
echo gettype ( $sum );//string
?>

使用settype()函数进行类型转换,返回值1表示成功,空表示失败。
复制代码 代码如下:

$sum = 58;
echo settype ( $sum, "float" );
echo $sum; //58
echo gettype ( $sum ); //double
?>

6.检测变量的函数:

函数

功能

返回值

Gettype()

获取变量的类型

基本数据类型中的其中一种

Settype()

 设置变量的类型

Bool(1:true 0:false(or ''))

Isset()

用来判断一个变量是否存在

Bool

Unset()

释放给定的变量

Void

Empty()

检测一个变量的值是否为空

Bool

is_int() is_integer()

检测变量是否是整数

Bool

Is_string()

检测变量是否是字符串

bool

Is_numeric

检测变量是否为数字或数字字符串

bool

Is_null

检测变量是否为 NULL

bool

Intval()

获取变量的整数值

int

Isset()的基本使用
复制代码 代码如下:

$a = 10;
echo isset ( $a );//1
?>
echo isset ( $b );//''
?>

Usset()的基本使用
复制代码 代码如下:

$a = 10;
unset($a);
echo isset ( $a );//''
?>

Empty()的基本使用
复制代码 代码如下:

$a= 5;
$b =1;
$c = 0;
$d = "";
$e = array();
$f = null;
$g = "0";
$h = false;
echo empty($a);//''(false)
echo '
';
echo empty($b);//''(false)
echo '
';
echo empty($c);//1(true)
echo '
';
echo empty($d);//1(true)
echo '
';
echo empty($e);//1(true)
echo '
';
echo empty($f);//1(true)
echo '
';
echo empty($g);//1(true)
echo '
';
echo empty($h);//1(true)
echo '
';
echo empty($f);//1(true)
?>

is_int()的基本使用。类似的函数有:is_float()、is_double()、is_string()、is_bool()、is_array()、is_null()、is_long()、is_object()、is_resource()、is_numeric()、is_real()等。
复制代码 代码如下:

$a = 11;
$b = 1.23;
$c = 3.1415926;
$d = "hello";
$e = false;
$f = array();
$g = null;
echo is_int($a);//1
echo '
';
echo is_float($b);//1
echo '
';
echo is_double($c);//1
echo '
';
echo is_string($d);//1
echo '
';
echo is_bool($e);//1
echo '
';
echo is_array($f);//1
echo '
';
echo is_null($g);//1
echo '
';
echo is_numeric($a);//1
?>

Intval()函数的基本使用。类似的函数为:floatval()、strval()
复制代码 代码如下:

$a = 22.23;
echo gettype($a);//double
echo '
';
$b = intval($a);//类型转换后不改变$a原来的类型
echo gettype($a);//double
echo '
';
?>
$a = 22.23;
echo gettype($a);//double
echo '
';
settype($a,"integer");//类型转换后会改变$aa原来的类型
echo gettype($a);//integer
echo '
';
?>

7.变量的作用域

超级全局变量

变量名

作用

$GLOBALS

所有全局变量数组

$_SERVER

服务器环境变量数组

$_GET

通过GET方式传递给该脚本的变量数组

$_POST

通过POST方式传递给该脚本的变量数组

$_COOKIE

COOKIE变量数组

$_FILES

与文件上传相关的变量数组

$_ENV

环境变量数组

$_REQUEST

所用用户输入的变量数组

$_SESSION

会话变量数组


8. Constants
Once defined, they cannot be changed again.
Copy code The code is as follows:

define("TOTAL",100);
echo TOTAL;//100
echo '
';
define("TOTAL",200);
echo TOTAL;//100
?>

Methods to view PHP predefined constants
Copy the code The code is as follows:

phpinfo();
?>

Method to reference PHP predefined constants
Copy code Code As follows:

echo $_SERVER["SERVER_NAME"];//localhost
echo '
';
echo $_SERVER[ "SERVER_PORT"];//8090
echo '
';
echo $_SERVER["DOCUMENT_ROOT"];//D:/AppServ/www
echo '
';
?>

5. Three common ways to access form variables

Copy code The code is as follows:

echo $username;//Short style, easily confused with variable names, not recommended.
echo '
';
echo $_POST['username'];//Medium style, supported after version 4.1.0, recommended
echo '
' ;
echo $HTTP_POST_VARS['username'];//Long style, outdated, may be eliminated in the future
?>

Posttest.html
Copy code The code is as follows:





How to get form data



username:





6. For string concatenation.
Copy code The code is as follows:

echo "the student name is:".$_POST['username'];
echo "
";
echo "welcome to "."school";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324815.htmlTechArticle1. Several styles of embedding PHP code in web pages. It is recommended to use standard style or short style to copy the code. The code is as follows: ?php //Standard style echo 'Hello World!'; ? ? //Short style echo '...
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,

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

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

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