Home Backend Development PHP Tutorial 第二章 PHP入门基础之php代码写法_PHP

第二章 PHP入门基础之php代码写法_PHP

Jun 01, 2016 pm 12:14 PM

一.在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.常量
一旦被定义之后,就不能再次更改。
复制代码 代码如下:
define("TOTAL",100);
echo TOTAL;//100
echo '
';
define("TOTAL",200);
echo TOTAL;//100
?>

查看PHP预定义的常量的方法
复制代码 代码如下:
phpinfo();
?>

引用PHP预定义常量的方法
复制代码 代码如下:
echo $_SERVER["SERVER_NAME"];//localhost
echo '
';
echo $_SERVER["SERVER_PORT"];//8090
echo '
';
echo $_SERVER["DOCUMENT_ROOT"];//D:/AppServ/www
echo '
';
?>

五.访问表单变量
常见的三种方式
复制代码 代码如下:
echo $username;//简短风格,容易与变量名混淆,不推荐使用。
echo '
';
echo $_POST['username'];//中等风格,4.1.0版后支持,推荐
echo '
';
echo $HTTP_POST_VARS['username'];//冗长风格,已过时,将来可能会被剔除
?>

Posttest.html
复制代码 代码如下:




获取表单数据的方式



username:





六.字符串连接用.
复制代码 代码如下:
echo "the student name is :".$_POST['username'];
echo "
";
echo "welcome to "."school";
?>
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles