第二章 PHP入门基础之php代码写法_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 |
对象 |
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 |
复制代码 代码如下:
$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
复制代码 代码如下:
六.字符串连接用.
复制代码 代码如下:
echo "the student name is :".$_POST['username'];
echo "
";
echo "welcome to "."school";
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
